DEV Community

Cover image for How to attach extra data to a GraphQL response on Apollo Server
Sebastian Wirkijowski
Sebastian Wirkijowski

Posted on

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: () => {
            return {
                willSendResponse(requestContext) {
                    requestContext.response.body.singleResult = {
                        ...requestContext.response.body.singleResult,
                        extensions: {
                            ...requestContext.response.body?.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.

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.

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

Top comments (0)