Logo

dev-resources.site

for different kinds of informations.

How to attach extra data to a GraphQL response on Apollo Server

Published at
12/20/2024
Categories
webdev
javascript
tutorial
graphql
Author
wirkijowski
Author
11 person written this
wirkijowski
open
How to attach extra data to a GraphQL response on Apollo Server

Let's say that we want to include a unique request identifier to each GraphQL response.

We can do that by adding a requestId field to the Query type, then resolving that field to some unique identifier we set in the context for each request. This isn't a perfect solution though, since we have to include that field on every single request on our client and it slightly increases the size of the request sent to the server.

There is a better way!

We can create a small plugin (middleware) that attaches our custom data to the response body's extensions field.

Apollo Sandbox - Screenshot

Based on what the "Creating Apollo Server Plugins" documentation page tells us, our plugin should look like this:

// extensionsPlugin.js

export const extensionsPlugin = () => {
    return {
        requestDidStart: async () => {
            return {
                async willSendResponse(requestContext) {
                    requestContext.response.body.singleResult = {
                        ...requestContext.response.body.singleResult,
                        extensions: {
                            requestId: requestContext.contextValue.requestId
                        },
                    };
                },
            }
        }
    }
};
Enter fullscreen mode Exit fullscreen mode

Feel free to use console.log(requestContent.response) to learn how the data is structured.

Keep in mind that only the extensions key of body.singleResult will work out of the box, because it's part of the GraphQL standard. We cannot add requestId directly to body.singleResult.

We don't have to worry about any preexisting extensions because Apollo Server does not actually use any by itself. Things may get a bit more complicated if you are using multiple plugins that affect the extensions field. If that's the case, just add ...requestContext.response.body?.extensions,
to your extensions object to include the original fields.

And now we just have to implement it!

This example uses the ulid package to generate IDs that are compact and time-sortable.

// main.js

import { ulid } from 'ulid';
import { extensionsPlugin } from "./extensionsPlugin.js";

// ...

const server = new ApolloServer({
    // ...
    plugins: [extensionsPlugin()],
    // ...
})

const { url } = await startStandaloneServer(server, {
    // ...
    context: async () => {
        // ...
        const requestId = ulid();

        return {
            requestId,
        }
    },
    // ...
})
Enter fullscreen mode Exit fullscreen mode

and thats it!

Why does it work? Context is built for each request separately (contextual) and is always available to all resolvers handling the request. It's best to set all needed variables in context, because it's created before any plugin hooks are fired (e.g.: requestDidStart). We add the requestId to our context and make it available everywhere, then our plugin pulls it from the context and attaches it to the response body right before it's sent back.

We can also use this to include some extra data, like time till current session expiration, request processing time or an identifier of a specific API server behind a gateway or load balancer.

Got an idea on what else could we attach to our response? Do share in the comments :)

graphql Article's
30 articles in total
Favicon
GraphQL Transforming API Development
Favicon
A Beginner’s Guide to Building GraphQL APIs with Apollo Server
Favicon
AWS Cognito + GraphQL Directive = ACL with minimal effort
Favicon
How to Add GitHub Contribution Stats to Your React App
Favicon
Netlify + FalkorDB: GRAPH Database Integration for Netlify Just Got Easier
Favicon
Setup GraphQL Mock Server
Favicon
Deploy graphql project
Favicon
Applications and Advantages of GraphQL in Modern Web Applications
Favicon
Automatically Generate REST and GraphQL APIs From Your Database
Favicon
Which One Should You Choose NEST JS or EXPRESS JS?
Favicon
Simplify Content Management with spurtCMS Powerful and Flexible
Favicon
Building REST APIs vs GraphQL: Which One is Right for Your Project?
Favicon
API Design Best Practices in 2025: REST, GraphQL, and gRPC
Favicon
GraphQL vs REST: When to Choose Which for Your Node.js Backend
Favicon
Understanding the Differences Between GraphQL and REST API Gateways
Favicon
Rails GraphQL Auth - JWT, Email & Security
Favicon
Essential APIs Every Developer Should Know: A Quick Guide
Favicon
From REST to GraphQL: Why and How I Made the Switch
Favicon
An Introduction to GraphQL: Only the Data You Need
Favicon
Effortless API Scaling: Unlock the Power of AWS AppSync
Favicon
Handling Errors in GraphQL APIs💡✨
Favicon
GraphQL query complexity + NestJS + Dataloader
Favicon
My Experience with AsyncThunk in Redux Toolkit
Favicon
GraphFusion is Now Open Source – Join Us in Building the Future of AI Knowledge Graphs 🚀
Favicon
Integrating Contentful in React: A Beginner’s Guide to Content Modeling and Fetching Data with GraphQL
Favicon
Extending your GraphQL service: Federation or Schema Stitching
Favicon
15 Best GraphQL Tools for 2025
Favicon
Introduction to GraphQL
Favicon
How to attach extra data to a GraphQL response on Apollo Server
Favicon
Using WordPress as a Data Entry Site to Power a Central API

Featured ones: