chapter five

5 Implementing Schema Resolvers

 

This chapter covers:

  • Using Node.js drivers for PostgreSQL and MongoDB
  • Seeding local databases with test data
  • Using the GraphQL schema language to build a schema
  • Creating resolver functions to make a schema executable
  • Using an interface to communicate with a GraphQL service
  • Creating an executable schema object using constructor and type helper objects
  • Creating custom object types and handling errors
  • Working with asynchronous calls to the database service

In the previous chapter, we designed the database models for AZdev and came up with the initial structure of its GraphQL schema. In this chapter, we’ll use Node.js database drivers and the GraphQL.js implementation to expose the entities in the databases through the use of resolver functions. To make a GraphQL schema executable under a GraphQL API service, you need to implement resolver functions.

Warning

You need a modern version of Node.js installed in your OS to follow along from this point. If you don’t have Node or if you have an old version of it (anything less than 13.2), download the latest from nodejs.org and use that.

Some familiarity with the Node.js runtime is needed. You don’t need to be an expert in Node but if you have never worked with it before you should probably learn its basics before proceeding with this chapter. I wrote a short introductory book on Node which you can get at az.dev/node-intro.

5.1  Node.js Drivers for PostgreSQL and MongoDB

5.1.1  Installing Dependencies

5.1.2  Environment Variables

5.1.3  Connecting To Databases

5.1.4  Loading Seed Data

5.2  Setting up a GraphQL Runtime

5.2.1  Creating the Schema Object

5.2.2  Creating Resolver Functions

5.2.3  Executing Requests

5.3  Communicating Over HTTP

5.4  Building a Schema Using Constructor Objects

5.4.1  Auto-restarting Node

5.4.2  The Top-level Query Type

5.4.3  Field Arguments

5.4.4  Custom Object Types

5.4.5  Custom Errors