Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do 3 dots/periods/ellipsis in a relay/graphql query mean?

The relay docs contain this fragment:

query RebelsRefetchQuery {   node(id: "RmFjdGlvbjox") {     id     ... on Faction {       name     }   } } 

What does this ... on Faction on syntax mean?

like image 363
jbrown Avatar asked Jan 16 '16 15:01

jbrown


People also ask

What is 3 dots GraphQL?

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.

How does relay work GraphQL?

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.

Should I use Relay for GraphQL?

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.

What is a GraphQL fragment?

A GraphQL fragment is a reusable unit of logic that can be shared between multiple queries and mutations.


1 Answers

There are two uses of ... related to fragments.

Incorporating a fragment by reference

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.

Inline 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.

like image 138
Greg Hurrell Avatar answered Sep 17 '22 22:09

Greg Hurrell