Graphql-code-generator: TypeError: Cannot read property 'onType' of undefined

Created on 18 Dec 2018  路  11Comments  路  Source: dotansimha/graphql-code-generator

Describe the bug
TypeError: Cannot read property 'onType' of undefined

To Reproduce
Steps to reproduce the behavior:
codegen.yml:

schema: 
  - http://localhost:4000/gql
  - src/gql/my-client-schema.graphql
documents: src/gql/**/*.{ts,tsx}
overwrite: true
generates:
  ./src/generated/gqlTypesAndComponents.tsx:
    plugins:
      - time
      - add: "// THIS IS A GENERATED FILE, DO NOT EDIT IT!"
      - typescript-common:
          interfacePrefix: "I"
      - typescript-client
      - typescript-react-apollo

Client schema:

type AuthState {
  authenticated: Boolean
  token: String
}

type Query {
  getAuthState: AuthState
}

type Mutation {
  changeAuthState: AuthState
}

Mutations:

const SET_LOCAL_AUTH_STATE = gql`
  mutation ChangeAuthState {
    changeAuthState @client {
      ...authFragments
    }
  }
  fragment authStateFragment on AuthState {
    authenticated
    token
  }

Queries:

const GET_LOCAL_AUTH_STATE = gql`
  query getAuthState {
    getAuthState @client {
      ...authStateFragment
    }
  }
  fragment authStateFragment on AuthState {
    authenticated
    token
  }
`;

defaults on Apollo:

{
  auth: {
    __typename: 'AuthState',
    authenticated: false,
    token: null
  }
}

Expected behavior
To generate TS and React Components

Environment:

  • OS: MacOs Mojave
  • Codegen: 0.15.0
  • Node: v9.11.2

All 11 comments

Hi @alexandru-tomescu !
I managed to reproduce it, and I added a more readable exception and a test in the codegen for this.

The issue here is the name of the fragment you used. Your fragment name is called authStateFragment, but on your mutation you used authFragments.

@dotansimha thank you for the quick response, but i changed it and i have the same error....

const SET_LOCAL_AUTH_STATE = gql`
  mutation ChangeAuthState {
    changeAuthState @client {
      ...authStateFragment
    }
  }
  fragment authStateFragment on AuthState {
    authenticated
    token
  }
`

@alexandru-tomescu Can you try to use the latest alpha (for all packages)? 0.16.0-alpha.ced38a7e

@dotansimha i will do that tonight, i can't do it now.

@dotansimha sorry, but i don't know how to install that specific tag/commit

@alexandru-tomescu it's a version number, you can replace all codegen related packages from 0.15.0 to 0.16.0-alpha.ced38a7e and run yarn

i will do that asap :)

@dotansimha it's working. thank you !

"@graphql-codegen/cli": "^1.0.6",
"@graphql-codegen/typescript": "^1.0.6",
"@graphql-codegen/typescript-operations": "^1.0.6",

still got the same, not very descriptive and useful error

TypeError: Cannot read property 'onType' of undefined

Problem was indeed an unmatching name of a fragment but fixed it with a little bit discomfort due to a lot of fragments and many places used.

I think if the error included at least the mistaken name of the fragment that is not found, it'd be more useful. Or if that is somewhat difficult at the current stage, at least the error could say that the problem comes from an unmatching fragment name.

@zenVentzi We fixed that in 1.0.7 - now you should get the error and the name of the fragments that caused that.

Okay. That.was.fast. Thank you!

Was this page helpful?
0 / 5 - 0 ratings