GraphQL 2 (19.02.2023 3M) Flashcards
What thing does allow to filter and transform data in GraphQL?
This thing is arguments.
In GraphQL, every field and nested object can get its own set of arguments. You can even pass arguments into scalar fields, to implement data transformations once on the server, instead of on every client separately.
See images with examples.
What is the purpose of aliaces?
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.
See the example image.
What is purpose of fragments in GraphQL?
Let’s say we had a relatively complicated page in our app, which lets 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 once - one for each side of the comparison.
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. Fragment are always bound to the type specified after ‘on’,
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.
How to use variables in fragments?
It is possible for fragments to access variables declared in the query or mutation
Describe the operation name concept. What operation types do exist?
You can give your operations meaningful names.
See example image. There operation type is query.
But in GraphQL are three operation types: query, mutation, subscription. Each of this type of operation may be named.
But there is query shorthand syntaxt that omits operation type and operation name:
{
human(id: “1000”) {
name
height(unit: FOOT)
}
}
Why operation names are needed? Why its use is encouraged?
The operation name is a meaningful and explicit name for your operation. It is only required in multi-operation documents.
But its use is encouraged because it is very helpful for debugging and server-side logging. When something goes wrong (you see errors either in your network logs, or in the logs of your GraphQL server) it is easier to identify a query in your codebase by name instead of trying to decipher the contents.
What mechanism does allow to provide dynamic arguments to GraphQL operations?
Since GraphQL operations are typically strings you of course can use string interpolation to change arguments dynamically. But it requires client-side operations and overall it’s considered bad practice.
Instead, you can use variables. Variables are similar to function parameters. See the example image.
When you send the request to GraphQL server you pass operation and variables in the separate, transport-specific (usually JSON) variables dictionary.
The variable definitions are the part that looks like ($episode: Episode). Variables must be prefixed by $. Variables list is comma-separated ($episode: Episode, $withFriends: Boolean!)