question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to query fields of union type even if all types in union are object types

See original GitHub issue
const 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:closed
  • Created 8 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

19reactions
leebyroncommented, Jul 5, 2015

This is intentional, when querying against a Union you must narrow the type by using a fragment.

{
  product {
    ... on Good {
      title
    }
  }
}

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 a title field and now your existing client is overfetching and potentially rendering incorrectly.

If title is a critical part of what a Product is, such that you want to guarantee that you can always query title regardless of what concrete Object type is in use, then you should use an Interface instead of a Union.

1reaction
vslinkocommented, Jul 5, 2015

Found the solution — GraphQLInterfaceType should be used to union object types.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found