filipstefansson / nexus-validate

🔑 Add argument validation to your GraphQL Nexus API.
35 stars 8 forks source link

Typescript error #17

Closed zaniluca closed 2 years ago

zaniluca commented 2 years ago

Issue and Screenshot

When adding the validation VSCode keeps giving this typescript error screenshot

Object literal may only specify known properties, and 'validate' does not exist in type 'NexusOutputFieldConfig<"Mutation", "signup">'.ts(2345)

The code is this and it works fine when running, it just gives the error in the editor without going away even after restarting the ts server and the editor.

Code

import { extendType, nonNull, stringArg } from "nexus";
import { Context } from "../context";

type AuthInput = {
  email: string;
  password: string;
};

export const SignupMutation = extendType({
  type: "Mutation",
  definition(t) {
    t.nonNull.field("signup", {
      type: "User",
      args: {
        email: nonNull(stringArg()),
        password: nonNull(stringArg()),
      },
      validate: ({ string }) => ({
        email: string().required().email().max(255),
        password: string().required().min(6).max(255),
      }),
      async resolve(_root, args: AuthInput, ctx: Context) {
        ...
      },
    });
  },
});

Versions

    "nexus": "^1.1.0",
    "nexus-prisma": "^0.35.0",
    "nexus-validate": "^1.2.0",
    "yup": "^0.32.11"
zaniluca commented 2 years ago

I found the error, i wasn't properly adding the generated types from nexus in my tsconfig.json. Seems like that having the generated types in node_modules/@types doesn't work.

Solution

Set the value you specified in your schema in the tsconfig

export const schema = makeSchema({
  ...
  // Where the generated files are placed
  outputs: {
    typegen: join(__dirname, "./generated/nexus-typegen/index.d.ts"),
    schema: join(__dirname, "./generated/schema.graphql"), // <--- This path
  },
  ...

Like so

{
  "typeRoots": [
    "./src/graphql/generated/nexus-typegen"
  ]
}