Search code examples
typescriptgraphqlgraphql-codegen

GraphQL Code Generator - Graphql Document Validation Failed


I keep getting an error when I run yarn run graphql-codegen --config codegen.yml

The error says

Found 1 error

  ✖ src/generated/graphql.tsx
    AggregateError: GraphQL Document Validation failed with 1 errors
        at Object.checkValidationErrors (/home/tsatsralt/code/st/workspade/web/node_modules/@graphql-tools/utils/index.js:960:1
5)
        at Object.codegen (/home/tsatsralt/code/st/workspade/web/node_modules/@graphql-codegen/core/index.cjs.js:102:15)
        at async process (/home/tsatsralt/code/st/workspade/web/node_modules/@graphql-codegen/cli/bin.js:992:56)
        at async Promise.all (index 0)
        at async /home/tsatsralt/code/st/workspade/web/node_modules/@graphql-codegen/cli/bin.js:999:37
        at async Task.task (/home/tsatsralt/code/st/workspade/web/node_modules/@graphql-codegen/cli/bin.js:778:17)

codegen.yml

overwrite: true
schema: "http://localhost:4000/graphql"
documents: "src/graphql/**/*.graphql"
generates:
  src/generated/graphql.tsx:
    plugins:
      - "typescript"
      - "typescript-operations"
      - "typescript-react-apollo

src/graphql/mutations/login.graphql

mutation Login($loginPassword: String!, $loginEmail: String!) {
  login(password: $loginPassword, email: $loginEmail) {
    errors {
      field
      message
    }
    user {
      ...RegularUser
    }
  }
}

User Resolver

@Resolver(User)
export class UserResolver {
  @Mutation(() => UserResponse)
  async login(
    @Arg("email") email: string,
    @Arg("password") password: string,
    @Ctx() { req }: Context
  ): Promise<UserResponse> { logic }

I finished ben awad's 14 hour full-stack tutorial and decided to do a similar project. but I ran across this problem. Couldn't find anything useful on the internet, thus i decided to ask here.

also i have all dependencies needed


Solution

  • Well, The problem was actually pretty stupid. Let me explain my mistake:

    debugging tip with graphql-codegen:

    codegen doesn't really tell you what exactly caused the error. try putting
    console.error(error) at where error occured. In my case, I put this at

    /home/tsatsralt/code/st/workspade/web/node_modules/@graphql-tools/utils/index.js
    

    And the error pretty much told me what was wrong. The error I got was

     Variable "$input" of type "RegisterInput" used in position expecting type "RegisterInput!".
    

    I used a fragment named RegisterInput and forgot to put exclamation mark at register.graphql file.

    Hope my mistake helped you as well.