The relay docs contain this fragment:
query RebelsRefetchQuery { node(id: "RmFjdGlvbjox") { id ... on Faction { name } } }
What does this ... on Faction
on syntax mean?
These three dots instruct GraphQL to assign the fields from the fragment to the current selection set. This change to the selection set in the liftInfo fragment causes every query that is using this fragment to select less data. Fragments are a pretty slick feature of the GraphQL query language.
Relay is a framework for managing and declaratively fetching GraphQL data. It allows developers to declare what data each component needs via GraphQL, and then aggregate these dependencies and efficiently fetch the data in fewer round trips.
Relay is recommended to use in the frontend to have more structured, modular, future-proofed applications that can scale easily to millions of users. The first thing that needs to be implemented in order to use Relay is to make a Relay-compatible GraphQL server. That's what we're going to do now.
A GraphQL fragment is a reusable unit of logic that can be shared between multiple queries and mutations.
There are two uses of ...
related to fragments.
query Foo { user(id: 4) { ...userFields } } fragment userFields on User { name }
Has the effect of composing the fields from the fragment into the embedding query:
query Foo { user(id: 4) { name } }
Note that fragments may compose other fragments.
From the docs:
If you are querying a field that returns an interface or a union type, you will need to use inline fragments to access data on the underlying concrete type.
https://graphql.org/learn/queries/#inline-fragments
These can be used to compose fields in a type-dependent way. For example:
query Foo { profile(id: $id) { url ... on User { homeAddress } ... on Business { address } } }
In this example, the server will determine whether to return the homeAddress
or address
field at runtime, based on whether the requested object is a User
or a Business
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With