graphql-nexus / nexus

Code-First, Type-Safe, GraphQL Schema Construction
https://nexusjs.org
MIT License
3.4k stars 275 forks source link

asNexusMethod not working #1036

Open KrishGarg opened 2 years ago

KrishGarg commented 2 years ago

I was following the tutorial at howtographql and I was in the 'Creating Custom Scalers' part and it didn't work.

I had the custom scaler registered by this:

// /src/graphql/scalars/Date.ts
import { asNexusMethod } from "nexus";
import { GraphQLDateTime } from "graphql-scalars";

export const GQLDate = asNexusMethod(GraphQLDateTime, "dateTime");

and

// /src/schema.ts
import { makeSchema } from "nexus";
import { join } from "path";

import * as types from "./graphql";

export const schema = makeSchema({
  types,
  outputs: {
    schema: join(__dirname, "..", "schema.graphql"),
    typegen: join(__dirname, "..", "nexus-typegen.ts"),
  },
  contextType: {
    module: join(__dirname, "./context.ts"),
    export: "Context",
  },
});

and they are being exported from index.ts in graphql folder. Even the types for this scalar are being generated and even my IDE isn't mad, but when I used this t.nonNull.dateTime("createdAt");, it gave me the compilation error,

TSError: ⨯ Unable to compile TypeScript:
src/graphql/Link.ts:22:15 - error TS2339: Property 'dateTime' does not exist on type 'Omit<OutputDefinitionBlock<"Link">, "nonNull" | "nullable">'.

22     t.nonNull.dateTime("createdAt");

I am using nexus version 1.2.0-next.17, and not the stable 1.1.0 because of #1025.

yukha-dw commented 2 years ago

I believe you have to put GQLDate on types such as:

makeSchema({
types: [
  GQLDate,
  anythingElse
]
})
fwolf-embark commented 2 years ago

I was getting the same error on 1.3.0, and had added GQLDate to my types.

In my case, I was able to circumvent this problem by casting the objectType.definition's t argument to type any. Albeit not an ideal solution, it got me moving.