GraphQL is a query language for APIs and was originally created to help client-side applications dictate the data they need before they request it.
GraphQL is a query language for making requests to APIs. With GraphQL, the client tells the server exactly what it needs and the server responds with the data that has been requested.
Here's an example query of attempting to retrieve some
user information of a certain id.When querying the
user with the example above, we can specify what fields we want to be returned from our API. Above, we've stated we're interested in receiving the
id, name, and listings information of the user. For the listings that are to be returned, we're only interested in receiving the id and title of each listing.GraphQL is a typed language. Before we tell our GraphQL API how we want each field in our API to be resolved, we'll need to tell GraphQL the type of each of the fields.
GraphQL allows for some significant benefits. GraphQL APIs are:
- Intuitive: The client can specify exactly what data it needs thus making the code intuitive and easy to understand.
- Performant: Since no useless data needs to be transferred, reduced latency will make an app feel faster which is especially important for slower internet connections.
- Typed: GraphQL uses a type system of its own to validate requests. This integrates beautifully with TypeScript to create a robust statically typed application.
GraphQL APIs are also:
- self-documenting.
- encourage the use of GraphQL IDEs.
- and consist of a single endpoint.
GraphQL isn't tied to any specific technology. This is because GraphQL is a specification, not a direct implementation. The community has created server implementations and client libraries to create and consume a GraphQL API for a variety of different technologies.
Server libraries
- Apollo Server | Node Frameworks - Github
- GraphQL Java | Java - Github
- GraphQL Ruby | Ruby - Github
Client libraries
- Apollo Client | React, Vue, etc. - Github
- GraphQL Python | Python - Github
- GraphQL.Client | .NET - Github
SCALAR TYPES
The properties of a GraphQL object must resolve to concrete data at some point. This is where scalar types come in. Scalar types represent basic data types that do not have any more nested properties.
field: Boolean!
field: Int!
field: Float!
field: String!
field: ID!GraphQL comes with a set of default scalar types out of the box:
Boolean:trueorfalse.
Int: A signed 32‐bit integer.
Float: A signed double-precision floating-point number (i.e. a decimal number).
String: A UTF‐8 character sequence.
ID: Used to represent a unique identifier (i.e. not intended to be human-readable) but gets serialized asString.
Every GraphQL API has a schema which completely describes all the possible data we can request. The schema is the blueprint of a GraphQL API. When a request comes in, it is validated against this schema and processed accordingly. This is where the GraphQL Schema comes in.
How do fields in the schema get processed? That's due to the second piece of a GraphQL API - the resolvers. GraphQL resolvers are functions that turn a GraphQL operation or request into data.
Query and Mutation represent the entry points of every GraphQL query.
The purpose of the Query type is to define all the possible entry points for fetching data (i.e. queries). The purpose of the Mutation type is to define all the possible entry points for manipulating data (i.e. mutations).
FIELD RESOLVERS
At this point, we've come to understand that a GraphQL schema lays out the blueprint of how a GraphQL API is to be shaped. But how do these fields return values or make the changes we expect them to?
We've mentioned this earlier but this is where GraphQL resolvers come i
GraphQL resolvers are functions or methods that resolve the value of GraphQL field.
Every field in a GraphQL API is referenced with a resolver function responsible in returning the value of that field.
Here's an example of resolver functions for a root level
listings query and deleteListing mutation fields. The listings field is to return a list of listings while the deleteListing field is to delete a listing and return the deleted listing.GraphQL resolvers are functions or methods that resolve the value of GraphQL field.
What are the
obj, args, ctx parameters defined above?A GraphQL
resolver receives four positional arguments.obj- the object returned from the resolver on the parent field. For rootQueryandMutationobject types, this argument is often not used and undefined.
args- the arguments provided to the field.
context- a value provided to every resolver and which usually holds important context information (e.g. state of currently logged in user).
info- used usually only in advanced cases but contains information about the execution state of the query - such as thefieldName,schema,rootValue, etc.