• Login
  • Get Started
Back to Blog

GraphQL Resolvers - Every Field is Backed by a Resolver

44ff59556ca0de121e683ebddc65c28c
With the rising tide of GraphQL taking over the internet, it’s no surprise that a number of companies like GitHub, PayPal, and Airbnb have adopted this technology. GraphQL is a data query and manipulation language and was released by Facebook in 2015 to provide a modern approach for developing web APIs.

By design, GraphQL’s functionality is separated into two parts: structure and behavior, and one of the key features that determines the server’s behavior are called resolver functions. Resolver functions boiled down are functions that resolve a value for a type or field in a schema. In this blog, hold your nose because we will be diving into the anatomy, functionality, and results of GraphQL resolvers.

Anatomy of a GraphQL Resolver

A GraphQL schema describes the shape of your data graph by defining types with fields, and it specifies what queries and mutations are available. Every field in a GraphQL schema is essentially backed by a resolver, and that resolver is then responsible for grabbing the data for its respective field. Using resolvers improves data processing and efficiency because instead of getting invoked on every request, it is only invoked when a user requests the data.

Below, we have declared a User type and a schema with a Query type. Let’s look at the example below of a resolver function for the user field on the Query type:

const UserType = new GraphQLObjectType({
name: 'User',
fields: {
id: { type: GraphQLID },
name: { type: GraphQLString },
},
})

const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
fields: {
user: {
type: UserType,
args: {
id: { type: GraphQLID },
},
resolve: (root, args, context, info) => {
const { id } = args // the `id` argument for this field is declared above
return fetchUserById(id) // hit the database
},
},
},
}),
})


The resolver type signature contains four parameters: root, args, context, and info, and can return an object or promise. Let’s look a little deeper at the arguments that get passed into the resolver and what they can contain.

While learning GraphQL fundamentals, you will most likely be working more with the root and args parameters. We will go through a simple example of querying for a user id and name by passing in two arguments into said parameters.

Execution Flow

In the example below, the query is requesting three fields: user, id, and name. Each field will invoke a resolver function, which totals to three resolvers. Let’s examine them in action:

0b706c5f1679cb8ecbac2f4d56a1a9e7

Steps of Execution Flow

In the example provided, we are returning an object with the requested data. Not all data will arrive in this format though - just something to keep in mind.

After your GraphQL server retrieves the data, you’re welcome to send it to your front-end for display. In our simple example, we worked with user names and IDs. Because GraphQL serves as an alternative to REST, it can also query numerous kinds of data such as home rental listings, authors and books at a library, restaurant information, and user reviews, but with a single endpoint. There is a sea of endless possibilities and the GraphQL world is your oyster.

Conclusion

This article unveils one of the many flexible and rich components that the GraphQL query language offers and how it’s changing the web service architecture standards. Even though resolvers are just functions, they play an important role by implementing the API. Understanding this piece strengthens the foundation for adopting GraphQL into your application.

 

References