-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Union Type fields of same name with differing types are incorrectly considered invalid #2781
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
@jefflewis I'm not sure what causing it, because it seems like the error is coming from |
Ok so the actual error is:
Which makes sense, because In GraphQL validation fails and it makes sense, I think the suggestion of GraphQL is correct, you should add type aliases: query ListItems {
ListItems {
... on Product {
id
productName: name
}
... on ProductVariant {
id
name
}
}
} Or, change your schema to match that. Also, if |
@dotansimha Are you suggesting that having differing field definitions for types in union is invalid GraphQL? If so, can you point me to that part of the spec. If not, then I'm lost as to how this is invalid? I can select these from my API with no issues. and I can use the resulting JSON to create types manually. Also, what do you mean be implementing them as interfaces instead of unions? The GQL schema type is that of union since the API may respond with one of a number of types for a given field (given an item, it can be a Product or ProductVariant). |
I also encountered this issue, which is why I digged into it. The formal specification that describes this, is: https://graphql.github.io/graphql-spec/draft/#sec-Field-Selection-Merging. The last paragraph states that even if two fields can never be encountered at the same time they can still not appear on the same object. That specific part was introduced by this merge request: graphql/graphql-spec#162 This issue has some recent comments about the validation rule: graphql/graphql-js#53 |
Dang, just stumbled upon this issue while trying to use fragments on a union typed field with types that share a field name but each is its own type: fragment myFrangment on Entity {
... on Player{
type
...playerFragment
}
... on Coach{
type
...coachFragment
}
}
Any way to ignore this error? |
I am also stumbling onto this. I don't mind using the alias, but the problem I then have is that they generated Typescript type does not have the alias and now I need to do custom typing to fix it. e.g. from the example above: query ListItems {
ListItems {
... on Product {
id
productName: name
}
... on ProductVariant {
id
name
}
}
} the type CustomProductType = {
productName: String
} & GeneratedProductType |
This is really common when fetching GraphQL types from a CMS. {
page {
bodyBlocks {
... on ImageBlockRecord {
title
}
... on TextBlockRecord {
title
}
}
}
} Is there any workaround that's not alias? Because changing the field name is quite messy, duplicating the 'context/block' name in the field, e.g: |
Found out that you can skip this validation rule by adding this to your config: {
skipDocumentsValidation: {
ignoreRules: ['OverlappingFieldsCanBeMergedRule'],
},
}, This is a codegen config setting of which you can select specific rules to ignore :-) |
I tried enabling this
have you managed to take this option into account? I saw you provided a https://codesandbox.io/p/devbox/vibrant-lake-y7dmnw but even in this one, I can see the error despite of the config... Thank you |
@abernier Yes it does not work in that example but later on I discovered that you can apply that config in the Anyway, try adding this skipDocumentsValidation the root of the config file:
Do note the
|
Uh oh!
There was an error while loading. Please reload this page.
Describe the bug
The codegen should allow for the differing type of name between the to union types.
To Reproduce
See example at codesandbox.
https://codesandbox.io/s/graphql-codegen-issue-template-hxp3g
codegen.yml
config file:Expected behavior
Successful generation of types.
Environment:
The text was updated successfully, but these errors were encountered: