Skip to content

Commit 557d1b9

Browse files
Sashko Stubailoleebyron
Sashko Stubailo
authored andcommitted
Sketch out some headings
1 parent 22a4a37 commit 557d1b9

File tree

1 file changed

+122
-1
lines changed

1 file changed

+122
-1
lines changed

site/docs/Learn-Queries.md

Lines changed: 122 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,129 @@ next: /docs/queries/
1010

1111
* A simple query
1212
* Fields, arguments, and aliases
13-
* Variables
1413
* Fragments
1514
* Comments
15+
* Variables
1616
* Directives (skip & include)
1717
* A simple mutation
18+
19+
### A simple query
20+
21+
Before we dive into all of the features of the query language, let's start with a very simple query, and look at the result we might get when we run it:
22+
23+
<script data-inline>
24+
import MiniGraphiQL from '../_core/MiniGraphiQL';
25+
import { StarWarsSchema } from './_swapiSchema';
26+
renderHere(<MiniGraphiQL schema={StarWarsSchema} query={ `
27+
{
28+
hero {
29+
name
30+
}
31+
}
32+
`} />);
33+
</script>
34+
35+
You can see immediately that the query has exactly the same shape as the result. This is essential to GraphQL, because you always get back what you expect, and the server knows exactly what fields the client is asking for.
36+
37+
The field `name` returns a `String` type, in this case the name of the main hero of Star Wars, `"R2-D2"`.
38+
39+
Oh, one more thing - the query above is **interactive**. That means you can change it as you like and see the new result. Try adding an `appearsIn` field to the `hero` object in the query, and see the new result.
40+
41+
### Nested data
42+
43+
In the previous example, we just asked for a simple string, but fields can also refer to objects. In that case, you can make a _sub-selection_ - GraphQL queries can traverse related objects and their fields, letting clients fetch lots of related data in one request, instead of making several roundtrips as one would need in a classic REST architecture.
44+
45+
<script data-inline>
46+
import MiniGraphiQL from '../_core/MiniGraphiQL';
47+
import { StarWarsSchema } from './_swapiSchema';
48+
renderHere(<MiniGraphiQL schema={StarWarsSchema} query={ `
49+
{
50+
hero {
51+
name
52+
# Queries can have comments!
53+
friends {
54+
name
55+
}
56+
}
57+
}
58+
`} />);
59+
</script>
60+
61+
Filtering fields and traversing related objects is pretty nice, but we are just getting started.
62+
63+
### Arguments
64+
65+
If the only thing we could do was traverse objects and their fields, GraphQL would already be a very useful language for data fetching. But when you add the ability to pass arguments to fields, things get much more interesting.
66+
67+
In a system like REST, you can only pass a single set of arguments - the query parameters and URL segments in your request. But in GraphQL, every field and nested object can get its own set of arguments, making GraphQL a complete replacement for making multiple API fetches.
68+
69+
<script data-inline>
70+
import MiniGraphiQL from '../_core/MiniGraphiQL';
71+
import { StarWarsSchema } from './_swapiSchema';
72+
renderHere(<MiniGraphiQL schema={StarWarsSchema} query={ `
73+
# XXX we should add an example of an argument on a nested field
74+
# people often get confused thinking you can only pass arguments
75+
# to top-level fields, since that's how it works in REST
76+
{
77+
hero(episode: EMPIRE) {
78+
name
79+
}
80+
}
81+
`} />);
82+
</script>
83+
84+
Arguments can be of many different types. In the above example, we have used an Enumeration type, which represents one of a finite set of options (in this case, episides from the original Star Wars trilogy). GraphQL comes with a default set of types, but a GraphQL server can also declare its own custom types, as long as they can be serialized into your transport format.
85+
86+
Read more about the GraphQL type system here. XXX this page doesn't exist yet
87+
88+
### Aliases
89+
90+
If you have a sharp eye, you may have noticed that, since the result object fields match the name of the field in the query but don't include arguments, you can't directly query for the same field with different arguments. That's why you need _aliases_ - they let you rename the result of a field to anything you want.
91+
92+
<script data-inline>
93+
import MiniGraphiQL from '../_core/MiniGraphiQL';
94+
import { StarWarsSchema } from './_swapiSchema';
95+
renderHere(<MiniGraphiQL schema={StarWarsSchema} query={ `
96+
{
97+
empireHero: hero(episode: EMPIRE) {
98+
name
99+
}
100+
jediHero: hero(episode: JEDI) {
101+
name
102+
}
103+
}
104+
`} />);
105+
</script>
106+
107+
In the above example, the two `hero` fields would have conflicted, but since we can alias them to different names, we can get both results in one request.
108+
109+
### Fragments
110+
111+
Let's say we had a relatively complicated page in our app, which let us look at two heroes side by side, along with their friends. You can imagine that such a query could quickly get complicated, because we would need to repeat the fields at least twice - one for each side of the comparison.
112+
113+
That's why GraphQL includes reusable units called _fragments_. Fragments let you construct sets of fields, and then include them in queries where you need to. Here's an example of how you could solve the above situation using fragments:
114+
115+
<script data-inline>
116+
import MiniGraphiQL from '../_core/MiniGraphiQL';
117+
import { StarWarsSchema } from './_swapiSchema';
118+
renderHere(<MiniGraphiQL schema={StarWarsSchema} query={ `
119+
{
120+
leftComparison: hero(episode: EMPIRE) {
121+
...comparisonFields
122+
}
123+
rightComparison: hero(episode: JEDI) {
124+
...comparisonFields
125+
}
126+
}
127+
128+
fragment comparisonFields on Character {
129+
name
130+
appearsIn
131+
friends {
132+
name
133+
}
134+
}
135+
`} />);
136+
</script>
137+
138+
You can see how the above query would be pretty repetitive if the fields were repeated. The concept of fragments is frequently used to split complicated application data requirements into smaller chunks, especially when you need to combine lots of UI components with different fragments into one initial data fetch.

0 commit comments

Comments
 (0)