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
On this page, you'll learn in detail about how to query a GraphQL server.
@@ -124,6 +124,33 @@ fragment comparisonFields on Character {
124
124
125
125
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.
126
126
127
+
## Operation name
128
+
129
+
Up until now, we have been using a shorthand syntax where we omit both the `query` keyword and the query name, but in production apps it's useful to use these to make our code less ambiguous.
130
+
You'll need these optional parts to a GraphQL operation if you want to execute something other than a query or pass dynamic variables.
131
+
132
+
Here’s an example that includes the keyword `query` as _operation type_ and `HeroNameAndFriends` as _operation name_ :
133
+
134
+
```graphql
135
+
# { "graphiql": true }
136
+
queryHeroNameAndFriends {
137
+
hero {
138
+
name
139
+
friends {
140
+
name
141
+
}
142
+
}
143
+
}
144
+
```
145
+
146
+
The _operation type_ is either _query_, _mutation_, or _subscription_ and describes what type of operation you're intending to do.
147
+
148
+
The _operation name_ is a meaningful and explicit name for your operation. It can be very useful for debugging and server-side logging reasons.
149
+
When something goes wrong either in your network logs or your GraphQL server, it is easier to identify a query in your codebase by name instead of trying to decipher the contents.
150
+
Think of this just like a function name in your favorite programming language.
151
+
For example, in JavaScript we can easily work only with anonymous functions, but when we give a function a name, it's easier to track it down, debug our code,
152
+
and log when it's called. In the same way, GraphQL query and mutation names, along with fragment names, can be a useful debugging tool on the server side to identify
When default values are provided for all variables, you can call the query without passing any variables. If any variables are passed as part of the variables dictionary, they will override the defaults.
184
211
185
-
## Operation name
186
-
187
-
One thing we also saw in the example above is that our query has acquired an _operation name_. Up until now, we have been using a shorthand syntax where we omit both the `query` keyword and the query name, but in production apps it's useful to use these to make our code less ambiguous.
188
-
189
-
Think of this just like a function name in your favorite programming language. For example, in JavaScript we can easily work only with anonymous functions, but when we give a function a name, it's easier to track it down, debug our code, and log when it's called. In the same way, GraphQL query and mutation names, along with fragment names, can be a useful debugging tool on the server side to identify different GraphQL requests.
190
-
191
-
192
212
## Directives
193
213
194
214
We discussed above how variables enable us to avoid doing manual string interpolation to construct dynamic queries. Passing variables in arguments solves a pretty big class of these problems, but we might also need a way to dynamically change the structure and shape of our queries using variables. For example, we can imagine a UI component that has a summarized and detailed view, where one includes more fields than the other.
0 commit comments