Back to Blog
Lesson 15 of the GraphQL: Your First GraphQL Schema & Server course
API ArchitectureAugust 2, 20264 min read

Creating Basic Resolvers: Linking Schema Fields to Data

Learn to build a GraphQL resolver map. We show you how to link your schema definitions to the actual JavaScript functions that fetch your API's data.

GraphQLResolversJavaScriptBackendAPI Architecture
Close-up shot of a chain link fence with selective focus, creating a moody atmosphere.

Previously in this course, we used defining the typeDefs for your Apollo Server project to establish the structure of our API. While your typeDefs define the "what," they don't know how to fetch the data.

That is the job of resolvers. Without them, your GraphQL server is just a static blueprint; resolvers are the engine that executes the logic to retrieve the data your clients request.

What is a Resolver Map?

In GraphQL, a resolver map is a JavaScript object that mirrors the structure of your schema. Every field you define in your typeDefs must have a corresponding function in your resolver map if you want that field to return data.

Think of it like a directory: when a query arrives, GraphQL looks at the requested field, checks your resolver map, and executes the associated function to get the value.

Anatomy of a Resolver

A basic resolver is simply a function that returns a value (or a promise that resolves to a value). Here is a simple example of a resolver map:

JAVASCRIPT
const resolvers = {
  Query: {
    hello: () => CE9178">'Hello, world!',
    version: () => CE9178">'1.0.0'
  }
};

If your schema defined type Query { hello: String, version: String }, the resolver map above tells Apollo Server exactly how to satisfy those fields.

Linking Schema Fields to Functions

Close-up of a computer screen displaying colorful programming code with depth of field.

To make this work in your project, you need to ensure the keys in your resolvers object match the types and fields in your typeDefs. If you have a Query type in your schema, you must have a Query property in your resolvers object.

Let’s advance our project by implementing a basic resolver structure.

Worked Example: Building a Resolver Map

Suppose your typeDefs look like this:

JAVASCRIPT
const typeDefs = CE9178">`#graphql
  type Query {
    serverStatus: String
    uptime: Int
  }
`;

To provide the data, you would define your resolver map like this:

JAVASCRIPT
const resolvers = {
  Query: {
    // This function runs when a client queries CE9178">'serverStatus'
    serverStatus: () => {
      return "Online";
    },
    // This function runs when a client queries CE9178">'uptime'
    uptime: () => {
      return 120; // Imagine this comes from a system call
    }
  }
};

In this setup, every time a client asks for serverStatus, the serverStatus function executes and returns the string "Online".

Practice Exercise

  1. Open your project from installing apollo-server and graphql packages.
  2. Create a new file called resolvers.js.
  3. Export a resolvers object that contains a Query field.
  4. Add a function called greetings that returns the string "Welcome to my GraphQL API!".
  5. Import this resolvers object into your main server file (where you initialize ApolloServer).

Common Pitfalls

Close-up of a rusty sewer manhole cover in a grassy Boston park.

  • Mismatched Names: If your schema field is called userProfile but your resolver function is named userprofile (lowercase 'p'), GraphQL won't be able to link them. Names are case-sensitive.
  • Missing Resolvers: If you define a field in your schema but forget to include a function for it in your resolver map, that field will return null (unless the field is marked as non-nullable, in which case the server will throw an error).
  • Returning Promises: Resolvers are often asynchronous. If your resolver returns a promise, GraphQL will wait for that promise to resolve before returning the data to the client. Ensure you handle your async/await logic correctly.

Frequently Asked Questions

Do I need a resolver for every single field?

Yes. Every field in your schema, from root queries down to nested object fields, requires a resolver function. If you don't provide one, GraphQL uses a default resolver that looks for a property with the same name on the parent object.

Can a resolver return an object?

Absolutely. If your schema defines a field as an object type (e.g., user: User), your resolver should return an object that contains the data for that type.

Does the order of fields in the resolver map matter?

No. JavaScript objects are unordered, and GraphQL looks up resolvers by key name, not by the order in which they appear in the file.

Recap

Team members presenting a project in a modern office setting with a focus on collaboration.

Resolvers are the bridge between your schema and your data sources. By creating a resolver map that mirrors your typeDefs, you tell GraphQL exactly how to fulfill requests. Remember: match your names exactly, ensure every field is covered, and remember that these are just standard JavaScript functions.

Up next: We will learn how to open the Apollo Sandbox to execute your first real queries against the server we’ve just wired up.

Similar Posts