GraphQL 3 (24.02.2023 3M) Flashcards
What are benifits of using variables?
- In our client code, we can simply pass a different variable rather than needing to construct an entirely new operation.
- This is also in general a good practice for denoting which arguments in our query are expected to be dynamic.
What types can variables have?
All declared variables must be either scalars, enums, or input object types. So if you want to pass a complex object into a field, you need to know what input type that matches on the server.
How to make varible optional or required?
Variable definitions can be optional (nullable) or required (not-nullable).
For example, $episode is optional, $withFriends is required
($episode: Episode, $withFriends: Boolean!)
Notice: if the field you are passing the variable into requires a non-null argument, then the variable has to be required as well.
Describe default values for variables.
Default values can also be assigned to the variables in the operation by adding the default value after the variable declaration.
See the example image.
When a default value is provided for the variable. You can omit value in the variables dictionary.
When default values are provided for all variables, you can call the query without passing any variables.
If a value for a variable with a default parameter is passed as part of the variables dictionary, it will override the defaults.
What directives are intended for?
Directives allow us to dynamically change the structure and shape of our queries (operations) using variables.
Directives can be useful to get out of situations where you otherwise would need to do string manipulation to add and remove fields in your query.
What two directives are required to be presented by any spec-compliant GraphQL server implementation? Are they only possible directives?
- @include(if: Boolean) only include the body of directive in the result if the argument is true.
- @skip(if: Boolean) Skip the body of directive if the argument is true.
- No, different servers can have their own set of directives, that procced as server desire
Give an example of @include directive.
See the image
Give an example of @skip directive.
See the image.