dotansimha / dotansimha/graphql-code-generator

Codegen failing by accessing unspecified field in union type

Open
#7,851 1 comment 1 reaction 0 assignees View on GitHub
core
Dominant language
TypeScript
Stars
11.3k
Forks
1.4k
Avg merge
1d 1h
Merged PRs (30d)
23

Description

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used [patch-package](https://github.com/ds300/patch-package) to patch `@graphql-codegen/visitor-plugin-common@2.7.4` for the project I'm working on.

There was an issue where it tried to access an `id` field that wasn't specified in the query on a union type which resulted in the codegen failing. This small little failsafe seems to do the trick, however I dont know what implications this would have. The error message for this was `Cannot read properties of undefined (reading 'type')`

Here is the diff that solved my problem:

```diff
diff --git a/node_modules/@graphql-codegen/visitor-plugin-common/index.js b/node_modules/@graphql-codegen/visitor-plugin-common/index.js
index 5b53cbf..53bb97e 100644
--- a/node_modules/@graphql-codegen/visitor-plugin-common/index.js
+++ b/node_modules/@graphql-codegen/visitor-plugin-common/index.js
@@ -3077,6 +3077,9 @@ class PreResolveTypesProcessor extends BaseSelectionSetProcessor {
}
return fields.map(field => {
const fieldObj = schemaType.getFields()[field.fieldName];
+ if(typeof fieldObj === "undefined") {
+ return
+ }
const baseType = pluginHelpers.getBaseType(fieldObj.type);
let typeToUse = baseType.name;
const useInnerType = field.isConditional && graphql.isNonNullType(fieldObj.type);
@@ -3111,6 +3114,9 @@ class PreResolveTypesProcessor extends BaseSelectionSetProcessor {
}
else {
const fieldObj = schemaType.getFields()[aliasedField.fieldName];
+ if(typeof fieldObj === "undefined") {
+ return
+ }
const baseType = pluginHelpers.getBaseType(fieldObj.type);
let typeToUse = this.config.scalars[baseType.name] || baseType.name;
if (graphql.isEnumType(baseType)) {
```

This issue body was [partially generated by patch-package](https://github.com/ds300/patch-package/issues/296).

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.