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-Queries.md
+122-1Lines changed: 122 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -10,8 +10,129 @@ next: /docs/queries/
10
10
11
11
* A simple query
12
12
* Fields, arguments, and aliases
13
-
* Variables
14
13
* Fragments
15
14
* Comments
15
+
* Variables
16
16
* Directives (skip & include)
17
17
* 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:
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.
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.
# 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.
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:
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