Please enable JavaScript.
Coggle requires JavaScript to display documents.
GRAPHQL (In deep (Schemas and Types (Scalar types (Int: A signed 32‐bit…
GRAPHQL
Introductions
-
a new API standard that provides a more efficient, powerful and flexible alternative to REST
-
-
-
-
-
In deep
Queries and Mutations
-
-
-
Fragments
Fragments let you construct sets of fields, and then include them in queries where you need to
-
-
-
Mutations
While query fields are executed in parallel, mutation fields run in series, one after the other.
Schemas and Types
Scalar types
-
-
-
-
ID: The ID scalar type represents a unique identifier, often used to refetch an object or as the key for a cache. The ID type is serialized in the same way as a String
-
-
-
-
-
-
Interfaces
-
type Human implements Character {
id: ID!
name: String!
friends: [Character]
appearsIn: [Episode]!
starships: [Starship]
totalCredits: Int
}
type Droid implements Character {
id: ID!
name: String!
friends: [Character]
appearsIn: [Episode]!
primaryFunction: String
}
Union types
-
{
search(text: "an") {
__typename
... on Human {
name
height
}
... on Droid {
name
primaryFunction
}
... on Starship {
name
length
}
}
}
-
Validation
By using the type system, it can be predetermined whether a GraphQL query is valid or not. This allows servers and clients to effectively inform developers when an invalid query has been created, without having to rely on runtime checks.
Execution
After being validated, a GraphQL query is executed by a GraphQL server which returns a result that mirrors the shape of the requested query, typically as JSON
Root fields & resolvers
Query: {
human(obj, args, context, info) {
return context.db.loadHumanByID(args.id).then(
userData => new Human(userData)
)
}
}
obj The previous object, which for a field on the root Query type is often not used.
-
context A value which is provided to every resolver and
holds important contextual information like the currently logged in user,
or access to a database.
info A value which holds field-specific information relevant to the current query as well as the schema details, also refer to type GraphQLResolveInfo for more details.
Introspection
It's often useful to ask a GraphQL schema for information about what queries it supports. GraphQL allows us to do so using the introspection system!
Best practice
HTTP
GraphQL is typically served over HTTP via a single endpoint which expresses the full set of capabilities of the service.
-
Versioning
GraphQL only returns the data that's explicitly requested, so new capabilities can be added via new types and new fields on those types without creating a breaking change. This has led to a common practice of always avoiding breaking changes and serving a versionless API
Nullability
in a GraphQL type system, every field is nullable by default.
Pagination
By passing first, last, offset, we can paginate the list of records
-