8000 How can I save DateTime objects using nexus? · Issue #1173 · graphql-nexus/nexus · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
How can I save DateTime objects using nexus? #1173
Open
@SecretariatV

Description

@SecretariatV

Hi.
I made server using Prisma, GraphQL, Nexus and MongoDB. I would like save datetime object to DB.
So, I declared DateTime using graphql-scalars and nexus.

export const DateTime = asNexusMethod(DateTimeResolver, "date");

const Mutation = objectType({
  name: "Mutation",
  definition(t) {
    t.field("create", {
      type: "Table",
      args: {
        point: nonNull(floatArg()),
        status: nonNull(intArg()),
        startedAt: nonNull("DateTime"),
      },
      resolve: async (_, args, context: Context) => {
        return context.prisma.crash.create({
          data: {
            point: args.point,
            status: args.status,
            startedAt: args.startedAt,
          },
        });
      },
    });
  },
});

const Table= objectType({
  name: "Table",
  definition(t) {
    t.nonNull.string("id");
    t.nonNull.float("point");
    t.int("status");
    t.nonNull.field("created", { type: "DateTime" });
    t.nonNull.field("startedAt", { type: "DateTime" });
  },
});

const schemaWithoutPermissions = makeSchema({
  types: [Table, Query, Mutation, DateTime],
  outputs: {
    schema: __dirname + "/../schema.graphql",
    typegen: __dirname + "/generated/nexus.ts",
  },
});

Query

{  
  "point": 33.22,
  "sta
4EAD
tus": 1,
  "startedAt": 2024-12-03T10:15:30Z
}

However, if I try to save StartedAt, an error occurs.

{
  "errors": [
    {
      "message": "Variable \"$startedAt\" of non-null type \"DateTime!\" must not be null.",
      "locations": [
        {
          "line": 1,
          "column": 101
        }
      ]
    }
  ]
}

What is the solution?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0