timqian / gql-generator

Generate queries from graphql schema, used for writing api test.
MIT License
375 stars 93 forks source link

Cannot read property 'getFields' of undefined #27

Open empty-jack opened 5 years ago

empty-jack commented 5 years ago

Error Example:

gqlg --schemaFilePath ./schema.graphql --destDirPath ./gqlg-new --depthLimit 2
description: Mutation
/usr/local/lib/node_modules/gql-generator/index.js:177
    const field = gqlSchema.getType(description).getFields()[type];
                                                ^

TypeError: Cannot read property 'getFields' of undefined
    at /usr/local/lib/node_modules/gql-generator/index.js:177:49
    at Array.forEach (<anonymous>)
    at generateFile (/usr/local/lib/node_modules/gql-generator/index.js:176:20)
    at Object.<anonymous> (/usr/local/lib/node_modules/gql-generator/index.js:193:3)
    at Module._compile (internal/modules/cjs/loader.js:776:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
    at Module.load (internal/modules/cjs/loader.js:643:32)
    at Function.Module._load (internal/modules/cjs/loader.js:556:12)
    at Function.Module.runMain (internal/modules/cjs/loader.js:839:10)
    at internal/main/run_main_module.js:17:11

If GQL Schema looks like this:

schema {
  query: RootQueryType
  mutation: RootMutationType
}

...
query: RootQueryType
  mutation: RootMutationType
}

type RootQueryType {
  asset(id: ID): Asset
  ...

* That's really popular example of schemas.

Then you will see error as shown above cause description filed in index.js get values from the hardcoded list: Query,Mutation,Subscription and doesn't check custom query names.

mahald commented 4 years ago

Just run into the same issue. Now just added a step to replace RootQueryType with Query and RootMutationType with Mutation in the File and all works fine. Would really be nice to be supported.

justenau commented 2 years ago

I created a PR to solve this issue, hopefully it will get approved soon.