Lädt...


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


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

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
                        },
                    };
                },
            }
        }
    }
};

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 ({req, }) => {
        // ...
        const requestId = ulid();

        return {
            requestId,
        }
    },
    // ...
})

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 :)

...

🔧 Intro to GraphQL, Part 2: Exploring a GraphQL Endpoint | Learning GraphQL


📈 35.95 Punkte
🔧 Programmierung

🔧 Intro to GraphQL, Part 1: What is GraphQL | Learning GraphQL


📈 35.95 Punkte
🔧 Programmierung

📰 Dyson V11 Absolute Extra: Extra harte oder extra sanfte Reinigung jetzt günstiger bei eBay


📈 28.8 Punkte
📰 IT Nachrichten

🔧 Using Apollo Client for GraphQL Data Management in React


📈 28.13 Punkte
🔧 Programmierung

🔧 Apollo Client for GraphQL State Management in React: Simplifying Data Fetching and Caching


📈 28.13 Punkte
🔧 Programmierung

🔧 Struggling to Mock graphQL Data? Open Source apollo-mock-http to the Rescue.


📈 28.13 Punkte
🔧 Programmierung

🎥 The Chatty API vs the Reserved One: GraphQL vs REST #graphql #rest #api #data


📈 26.69 Punkte
🎥 IT Security Video

🔧 GraphQL in Your Ember App with Glimmer Apollo


📈 25.4 Punkte
🔧 Programmierung

🔧 2 - GraphQL Core Concepts: Schema, Resolvers, Query, Apollo


📈 25.4 Punkte
🔧 Programmierung

🔧 Boosting Self-Hosted GraphQL API Performance With Apollo Router and Federation


📈 25.4 Punkte
🔧 Programmierung

🔧 Boosting Self-Hosted GraphQL API Performance With Apollo Router and Federation


📈 25.4 Punkte
🔧 Programmierung

🔧 How to Consume GraphQL API with Apollo Client in a Reactjs Application


📈 25.4 Punkte
🔧 Programmierung

🔧 Setup Apollo Client para requisições graphQL em React


📈 25.4 Punkte
🔧 Programmierung

🔧 Learn GraphQL and Apollo Client With a Simple React Project


📈 25.4 Punkte
🔧 Programmierung

🔧 Setup Apollo Client for graphQL requests in React


📈 25.4 Punkte
🔧 Programmierung

🔧 Adding Apollo GraphQL to Your React Project


📈 25.4 Punkte
🔧 Programmierung

🔧 How to Integrate GraphQL in Next JS using Apollo Client


📈 25.4 Punkte
🔧 Programmierung

🔧 Mocking Apollo GraphQL Queries in React Testing


📈 25.4 Punkte
🔧 Programmierung

🔧 Apollo Connectors enables developers to turn REST APIs into GraphQL endpoints


📈 25.4 Punkte
🔧 Programmierung

📰 Apollo Federation öffnet die Microservices-Welt für GraphQL


📈 25.4 Punkte
📰 IT Nachrichten

🔧 Building Modern Web Apps with React, TypeScript, GraphQL and Apollo Client


📈 25.4 Punkte
🔧 Programmierung

📰 FULL-STACK REACT WITH GRAPHQL AND APOLLO BOOST


📈 25.4 Punkte
📰 Alle Kategorien

🔧 Planning to Integrate Apollo GraphQL with Jetpack Compose? Here's a Complete Guide


📈 25.4 Punkte
🔧 Programmierung

🔧 Integrating GraphQL with Apollo in React: A Complete Guide


📈 25.4 Punkte
🔧 Programmierung

🔧 Apollo GraphQL Integration in Jetpack Compose


📈 25.4 Punkte
🔧 Programmierung

🔧 Cookies Setup in Apollo Studio for NodeJS GraphQL Servers


📈 25.4 Punkte
🔧 Programmierung

🔧 GraphQL Federation with Ballerina and Apollo - Part II


📈 25.4 Punkte
🔧 Programmierung

🔧 GraphQL Federation with Ballerina and Apollo Studio


📈 25.4 Punkte
🔧 Programmierung

🔧 Apollo GraphQL - Setup ReactJS


📈 25.4 Punkte
🔧 Programmierung

🔧 Curious Use Cases of GraphQL (and The Future of GraphQL)


📈 23.97 Punkte
🔧 Programmierung

🎥 Intro to GraphQL, Part 1: What is GraphQL


📈 23.97 Punkte
🎥 Video | Youtube

🎥 Intro to GraphQL, Part 2: Exploring a GraphQL Endpoint


📈 23.97 Punkte
🎥 Video | Youtube

matomo