You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: site/docs/Learn-Schema.md
+225Lines changed: 225 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,6 +11,7 @@ next: /docs/schema/
11
11
* Type system
12
12
* Type language
13
13
* Basics (Schema, Objects & Fields)
14
+
* Arguments (TODO)
14
15
* Scalars & Enums
15
16
* Lists & NonNull (mention error handling)
16
17
* Interfaces & Unions
@@ -60,3 +61,227 @@ The language is pretty readable, but let's go over it so that we can have a shar
60
61
- `[Episode]` represents an _array_ of `Episode` objects. This means that you can always expect an array, with zero or more items, when you query the `appearsIn` field.
61
62
62
63
Now you know what a GraphQL object type looks like, and how to read the basics of the GraphQL type language.
64
+
65
+
### The Query and Mutation types
66
+
67
+
Most types in your schema will just be normal object types, but there are two types that are unique within a schema:
68
+
69
+
```graphql
70
+
schema {
71
+
query: Query
72
+
mutation: Mutation
73
+
}
74
+
```
75
+
76
+
Every GraphQL service has exactly zero or one each of the `Query` and `Mutation` types. These types are mostly the same as a regular object type, but they are special because they define the _entry point_ of every GraphQL query. So if you see a query that looks like:
77
+
78
+
```graphql
79
+
query {
80
+
hero {
81
+
name
82
+
}
83
+
droid(id: "2001") {
84
+
name
85
+
}
86
+
}
87
+
```
88
+
89
+
That means that the GraphQL service needs to have a `Query` type with `hero` and `droid` fields:
It'simportanttorememberthatotherthanthisspecialstatus, the `Query` and `Mutation` typesarethesameasanyotherGraphQLobjecttype, andtheirfieldsworkexactlythesameway.
Inthefollowingquery, the `name` and `appearsIn` willresolvetoscalartypes:
107
+
108
+
```graphql
109
+
{
110
+
hero {
111
+
name
112
+
appearsIn
113
+
}
114
+
}
115
+
```
116
+
117
+
We know this because those fields don't have any sub-fields - they are the leaves of the query.
118
+
119
+
GraphQL comes with a set of default scalar types out of the box:
120
+
121
+
-`Int`: A signed 32‐bit integer.
122
+
-`Float`: A signed double-precision floating-point value.
123
+
-`String`: A UTF‐8 character sequence.
124
+
-`Boolean`: `true` or `false`.
125
+
-`ID`: The ID scalar type represents a unique identifier, often used to refetch an object or as the key for a cache. The ID type is serialized in the same way as a String; however, defining it as an `ID` signifies that it is not intended to be human‐readable.
126
+
127
+
In most GraphQL service implementations, there is also a way to specify custom scalar types. For example, we could define a `Date` type:
128
+
129
+
```graphql
130
+
scalarDate
131
+
```
132
+
133
+
Then it's up to our implementation to define how that type should be serialized, deserialized, and validated. For example, you could specify that the `Date` type should always be serialized into an integer timestamp, and your client should know to expect that format for any date fields.
134
+
135
+
### Enumeration types
136
+
137
+
Also called _Enums_, enumeration types are a special kind of scalar that is restricted to a particular set of allowed values. This allows you to:
138
+
139
+
1. Validate that any arguments of this type are one of the allowed values
140
+
2. Communicate through the type system that a field will always be one of a finite set of values
141
+
142
+
Here's what an enum definition might look like in the GraphQL schema language:
143
+
144
+
```graphql
145
+
enumEpisode {
146
+
NEWHOPE
147
+
EMPIRE
148
+
JEDI
149
+
}
150
+
```
151
+
152
+
This means that wherever we use the type `Episode` in our schema, we expect it to be exactly one of `NEWHOPE`, `EMPIRE`, or `JEDI`.
153
+
154
+
Note that GraphQL service implementations in various languages will have their own language-specific way to deal with enums. In languages that support enums as a first-class citizen, the implementation might take advantage of that; in a language like JavaScript with no enum support, these values might be internally mapped to a set of integers. However, this should not leak out to the client, which will operate entirely in terms of the string names of the values.
155
+
156
+
### Lists and Non-Null
157
+
158
+
Object types, scalars, and enums are the only kinds of types you can define in GraphQL. But when you use the types in other parts of the schema, or in your query variable declarations, you can apply additional _type modifiers_ that affect validation of those values. Let's look at an example:
Listsworkinasimilarway: Wecanuseatypemodifiertomarkatypeasa `List`, whichindicatesthatthisfieldwillreturnanarrayofthattype. Intheschema language, this is denoted by wrapping the type in square brackets, `[` and `]`. It works the same for arguments, where the validation step will expect an array for that value.
172
+
173
+
The Non-Null and List modifiers can be combined. For example, you can have a List of Non-Null Strings:
174
+
175
+
```graphql
176
+
myField: [String!]
177
+
```
178
+
179
+
This means that the _list itself_ can be null, but it can't have any null members. For example, in JSON:
180
+
181
+
```js
182
+
myField: null // valid
183
+
myField: ['a', 'b'] // valid
184
+
myField: ['a', null, 'b'] // error
185
+
```
186
+
187
+
Now, let's say we defined a Non-Null List of Strings:
188
+
189
+
```graphql
190
+
myField: [String]!
191
+
```
192
+
193
+
This means that the list itself cannot be null, but it can contain null values:
194
+
195
+
```js
196
+
myField: null // error
197
+
myField: ['a', 'b'] // valid
198
+
myField: ['a', null, 'b'] // valid
199
+
```
200
+
201
+
You can arbitrarily nest any number of Non-Null and List modifiers, according to your needs.
202
+
203
+
### Interfaces
204
+
205
+
Like many type systems, GraphQL supports interfaces. An _Interface_ is an abstract type that includes a certain set of fields that a type must include to implement the interface.
206
+
207
+
For example, you could have an interface `Character` that represents any character in the Star Wars trilogy:
208
+
209
+
```graphql
210
+
interface Character {
211
+
id: String!
212
+
name: String
213
+
friends: [Character]
214
+
appearsIn: [Episode]
215
+
}
216
+
```
217
+
218
+
This means that any type that _implements_`Character` needs to have these exact fields, with these arguments and return types.
219
+
220
+
For example, here are some types that might implement `Character`:
221
+
222
+
```graphql
223
+
typeHuman : Character {
224
+
id: String!
225
+
name: String
226
+
friends: [Character]
227
+
appearsIn: [Episode]
228
+
homePlanet: String
229
+
}
230
+
231
+
typeDroid : Character {
232
+
id: String!
233
+
name: String
234
+
friends: [Character]
235
+
appearsIn: [Episode]
236
+
primaryFunction: String
237
+
}
238
+
```
239
+
240
+
You can see that both of these types have all of the fields from the `Character` interface, but also bring in extra fields, `homePlanet` and `primaryFunction`, that are specific to that particular type of character.
241
+
242
+
Interfaces are useful when you want to return an object or set of objects, but those might be of several different types. For example, in the following query:
243
+
244
+
```graphql
245
+
queryHeroForEpisode($ep: Episode!){
246
+
hero(episode: $ep) {
247
+
name
248
+
}
249
+
}
250
+
```
251
+
252
+
The `hero` field returns the type `Character`, which means it might be either a `Human` or a `Droid` depending on the `episode` argument. In the query above, you can only ask for fields that exist on the `Character` interface, and to ask for a field on the concrete type, you need to use a fragment:
253
+
254
+
```graphql
255
+
queryHeroForEpisode($ep: Episode!){
256
+
hero(episode: $ep) {
257
+
name
258
+
...onDroid {
259
+
primaryFunction
260
+
}
261
+
}
262
+
}
263
+
```
264
+
265
+
Learn more about this in the [conditional fragments](XXX) section in the query guide.
266
+
267
+
### Union types
268
+
269
+
Union types are very similar to interfaces, but they don't get to specify any common fields between the types.
270
+
271
+
XXX no example in SWAPI
272
+
273
+
```graphql
274
+
unionSearchResult = Photo | Person
275
+
276
+
typePerson {
277
+
name: String
278
+
age: Int
279
+
}
280
+
281
+
typePhoto {
282
+
height: Int
283
+
width: Int
284
+
}
285
+
```
286
+
287
+
Inthiscase, ifyouqueryafieldthatreturnsthe `SearchResult` uniontype, youneedtouseaconditionalfragmentto be able to query any fields at all.
0 commit comments