Fragments
Fragments are a handy feature to help to improve the structure and reusability of your GraphQL code. A fragment is a collection of fields on a specific type.
Example for the following type:
1 | type User { |
Here, we could represent all the information that relates to the user’s physical address into a fragment:
1 | fragment addressDetails on User { |
Parameterizing Fields with Arguments
Specify default values for arguments.
1 | type Query { |
This olderThan
argument can now be passed into the query using the following syntax:
1 | { |
Named Query Results with Aliases
GraphQL lets you send multiple queries in a single request. However, since the response data is shaped after the structure of the fields being requested, you might run into naming issues when you’re sending multiple queries asking for the same fields:
1 | { |
Result:1
2
3
4
5
6
7
8{
"first": {
"name": "Alice"
},
"second": {
"name": "Sarah"
}
}
More types
Object & Scalar Types
- Scalar types represent concrete units of data.
String
,Int
,Float
,Boolean
, andID
. - Object types have fields that express the properties of that type and are composable.
Enums1
2
3
4
5
6
7
8
9enum Weekday {
MONDAY
TUESDAY
WEDNESDAY
THURSDAY
FRIDAY
SATURDAY
SUNDAY
}
Interface
An interface can be used to describe a type in an abstract way. It allows you to specify a set of fields that any concrete type, which implements this interface, needs to have.
1 | interface Node { |
Union Types
Union types can be used to express that a type should be either of a collection of other types. They are best understood by means of an example.
1 | type Adult { |
1 | { |