dotansimha / dotansimha/graphql-code-generator

"Cannot read property 'name' of undefined" for invalid schema when skipDocumentsValidation: true

Open
#7,252 0 comments 0 reactions 0 assignees View on GitHub
core kind/enhancement
Dominant language
TypeScript
Stars
11.3k
Forks
1.4k
Avg merge
1d 1h
Merged PRs (30d)
23

Description

**Describe the bug**

Running `graphql-codegen` configured with documents that refer to unknown query field(s) results in error(s) like the following:
```
AggregateError: GraphQL Document Validation failed with 1 errors;
Error 0: GraphQLDocumentError: Cannot query field "undefinedField" on type "Query".
```
Re-running the CLI after enabling `skipDocumentsValidation: true` in the config instead results in:
```
TypeError: Cannot read property 'name' of undefined
at .../node_modules/graphql/type/introspection.js:612:53
at Array.some ()
at Object.isIntrospectionType (.../node_modules/graphql/type/introspection.js:612:29)
at Object.Field (.../node_modules/@graphql-codegen/typescript/index.js:390:25)
at Object.enter (.../node_modules/graphql/utilities/TypeInfo.js:387:27)
at Object.visit (.../node_modules/graphql/language/visitor.js:200:21)
at .../node_modules/@graphql-codegen/typescript/index.js:395:38
```

**To Reproduce**
1. Open [codesandbox](https://codesandbox.io/s/graphql-codegen-invalid-throws-w2qd9)
2. Allow `graphql-codegen` to run (restart container if necessary)
3. Error is output in console

You can verify that it's caused by the querying of an unknown field by opening `query.graphql`, changing `charactersX` to `characters` and then re-running the CLI. This time it should succeed without any errors.

**Expected behavior**
No errors - the `schema.ts` file should be updated, but with types for the unknown query missing.

**Environment:**
See codesandbox

**Additional context**

Adding a guard against the `undefined` seems to be enough to avoid the error and allow the CLI to successfully generate otherwise working output, see this commit for a bare-bones fix: https://github.com/mogelbrod/graphql-code-generator/commit/99f9bd05a04a9ea2d7d9fda5de32252c4a2fa399

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.