Giter VIP home page Giter VIP logo

Comments (6)

leebyron avatar leebyron commented on March 29, 2024 20

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.

from graphql-js.

vslinko avatar vslinko commented on March 29, 2024 1

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

from graphql-js.

xpepermint avatar xpepermint commented on March 29, 2024

Can you please write an example? I get cannot query field {name} error no matter what I do when I call on common fields. Should I always call with fragments?

{
  product {
    title // common fields (all types)
    ... on Good {
      // other fields
    }
  }
}

from graphql-js.

xpepermint avatar xpepermint commented on March 29, 2024

@vslinko Thanks to this I know what you mean now.

from graphql-js.

jasonkuhrt avatar jasonkuhrt commented on March 29, 2024

It would be great to be able to opt out of this intentional safety check. In some apps where e.g. the same developer or team works the full stack then the better tradeoff for them might be that they can select on common fields of the union type. Yes it will crash if the union is extended by the server, but the queries are flatter/easier to make. Assuming there is only one right answer here for all apps and teams is not ideal IMO.

from graphql-js.

0Dmitry avatar 0Dmitry commented on March 29, 2024

Looks like if all types in a union implement an interface, containing the common fields, you can then query those common fields by narrowing to that interface.

interface CommonFields {
  name: String
}

type TypeA implements CommonFields {
  name: String
  fieldA: String
}

type TypeB implements CommonFields {
  name: String
  fieldB: String
}

union TypeAll = TypeA | TypeB

Then you can query a field of type TypeAll like this

{
  field {
    ...on CommonFields {
      name
    }
}

from graphql-js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.