GraphQL Docs Flashcards
studying GraphQL docs
What is an operation type?
an operation type is either a query, mutation, or subscription and describes what type of operation you’re intending to do.
What is an operation name?
similar to a function name - helps with debugging and server-side logging
**only required in multi-operation documents.*
What is the one important distinction between queries
and mutations
?
query fields are executed in parallel
mutation fields run in series, one after the other
ex. if we send two incrementCredits
mutation fields in one request, the first is guaranteed to finish before the second begins, ensuring that we don’t end up with a race condition with ourselves.
True or False: GraphQL can be used with any backend framework or programming langauge.
True
GraphQL type language keywords
- type
- schema (query, mutation)
- enum
- interface
- query
- mutation
- union
- fragment
- … on <Type> (inline fragment)</Type>
- input* cant have args on their fields
TRUE OR FALSE: GraphQL queries always end at scalar values
TRUE.
How to think about each field in a GraphQL query
think of every field in a GraphQL query as a method or function of the previous type, which returns the next type.
Each field on a type is backed by a function called a resolver that is provided by the GraphQL server developer.
execution completes when a field returns a scalar value (number or string).
If a field returns an object value, the query keeps executing until every field on that object produces a scalar.
Root type or Query type
the type at the top level of every GraphQL server that represents all possible entry points into the GraphQL API
on Frontend defined as:
~~~
type Query {
human(id: ID!): Human
}
~~~
on BE Server side: (resolver)
~~~
Query: {
human(obj, args, context, info) {
return context.db.loadHumanByID(args.id).then(
userData => new Human(userData)
)
}
}
~~~
What arguments does a resolver function recieve?
-
obj
- the previous object, which for a field on the root Query type is often not used. -
args
- the arguments provded to the field in the GraphQL query -
context
- the value which is provided to every resolver and holds important contextual information like the currently logged in user, or access to a database **usually async, returns a Promise, Task, Deferred depending on implementation language* -
info
- a value which holds field-specific information relevant to the current query as well as the schema details.
Trivial resolvers
ex.
~~~
Human: {
name(obj, args, context, info) {
return obj.name
}
}
~~~
**NOTE: many GraphQL libraries will let you omit resolvers this simple and will assume if a resolver isn’t provided for a field, that a property of the same name should be read and returned.*
List resolvers
ex.
~~~
Human: {
starships(obj, args, context, info) {
return obj.starshipIDs.map(
id => context.db.loadStarshipByID(id).then(
shipData => new Starship(shipData)
)
)
}
}
~~~
returns a list
of Promises… GraphQL will wait for all Promises concurrently betfore continuing… when it has a list of objects
, it will concurrently continue yet again to load the name field on each of these items.