Unable to query fields of union type even if all types in union are object types
See original GitHub issueconst Good = new GraphQLObjectType({
name: 'Good',
fields: {
title: {type: GraphQLString}
}
})
const Product = new GraphQLUnionType({
name: 'Product',
types: [Good],
resolveType(root) {
return Good
}
})
{ errors:
[ { message: 'Cannot query field title on Product',
locations: [ { line: 14, column: 13 } ] } ] }
Issue Analytics
- State:
- Created 8 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Unions and interfaces - Apollo GraphQL Docs
All of a union's included types must be object types (not scalars, input types, etc.). Included types do not need to share any...
Read more >Union types in GraphQL - Dev - Discuss Dgraph
The member types of a Union type must all be Object base types; Scalar, Interface and Union types must not be member types...
Read more >UNION, INTERSECT, and EXCEPT - Amazon Redshift
The two expressions must contain the same number of output columns with compatible data types; otherwise, the two result sets can't be compared...
Read more >Unions - TypeGraphQL
Be aware that when the query/mutation return type (or field type) is a union, we have to return a specific instance of the...
Read more >TypeScript discriminated union and intersection types
Understanding union and the intersection types helps you learn how better to implement them when necessary in TypeScript.
Read more >
Top Related Medium Post
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This is intentional, when querying against a Union you must narrow the type by using a fragment.
This ensures that your query will be resilient to the evolution of the type schema over time. When this client has shipped, and you introduce a new type into your
Product
union, your client can safely ignore those additional typed items. If you were to query a field without first specifying the concrete Object type, then a new type could be added which happens to have atitle
field and now your existing client is overfetching and potentially rendering incorrectly.If
title
is a critical part of what aProduct
is, such that you want to guarantee that you can always querytitle
regardless of what concrete Object type is in use, then you should use an Interface instead of a Union.Found the solution —
GraphQLInterfaceType
should be used to union object types.