GraphQL module#72 Flashcards
How many endpoints does GraphQL expose?
GraphQL exposes a single endpoint.
What does the QL stand for anyway
Query Language, which is really just a string.
What is one of the biggest differences between GraphQL and REST?
GraphQL unlike REST does not rely on HTTP verbs in order to determine the request type.
With REST you cannot pick and choose what the server returns. Using a GraphQL query though, you can.
How does GraphQL reduce demands on the network and processing power?
By requiring the client to specify exactly what information they want to be returned, it reduces processing and payload sizes.
What’s considered an “argument” within a GraphQL query?
As in JavaScript it’s the item that is passed in parenthesis. Example:
{ person(id: "1") { name } } id: "1" is the parameter/argument
Can subsets of data also have arguements?
Yep, as long as the API permits. Example:
{ person(id: "1") { name friends(limit: 100) } }
What are aliases in GraphQL and how are they written?
Aliases are a way of returning data with a naming convention of your own choosing. It is a custom request that give your data a certain name. Example:
{ owner: person(id: "1") { fullname: name } } Which returns the custom name for "name" as "fullname"
{ "data": { "owner": { "fullname": "Tony" } } }
What are fragments in GraphQL?
Fragments allow for the specification of the structure of a dataset 1 time. Example
{ owner: person(id: "1") { ...personFields } first_employee: person(id: "2") { ...personFields } }
fragment personFields on person {
fullname: name
}
Why and how are variables used in GraphQL?
They are used when we need to dynamically specify a value in a query.
Translation: Sometimes values will change so the best way to represent them is with a variable.
Here’s a standard query followed by a the same query written with variables:
{ owner: person(id: "1") { fullname: name } }
query GetOwner($id: String) { owner: person(id: $id) { fullname: name } }
{
“id”: “1”
}
What is the equivalent form of a named query or a query that is using variables in the request?
It is equivalent to a named function. We do this to differentiate it from other queries when there are many to our application.
Variables can be specified as required in our program. How is it written?
By using the ! Bang symbol. Here’s the example:
query GetOwner($id: String = “1”)
When using GraphQL directives can be set. What are the used for and how to write them?
Directives let you include or exclude a field based on whether a variable returns true or false.
Here we have a variable that ends up false so this field will be excluded.
query GetPerson($id: String) { person(id: $id) { fullname: name, address: @include(if: $getAddress) { city street country } } }
{
“id”: “1”,
“getAddress”: false
}
There are two kinds of directives available to us, what are they?
@include (if true)
@skip(if:Boolean)