Giter Site home page Giter Site logo

Comments (4)

sachindshinde avatar sachindshinde commented on May 22, 2024

Yep, you can place the structured context object in the extensions property of the JSON body in HTTP GraphQL requests and responses.

The steps are as follows:

  • In the gateway's server constructor, you pass in a context function that extracts the context object from the gateway request and adds it to the gateway context, e.g.
const server = new ApolloServer({
  gateway: /* gateway constructor */ ,
  subscriptions: false,
  context: ({ req }) => {
    const contextObject = /* Parse the context object from req.body */ ;
    // Add the context object to the gateway context
    return { contextObject };
  },
});
  • In the gateway constructor, you pass in a buildService function that creates a RemoteGraphQLDataSource sub-class that defines a willSendRequest() callback to take the context object out of the gateway context and embed it in request.extensions and a didReceiveResponse() callback to take the context object out of response.extensions and add it to the gateway context. This looks like:
const server = new ApolloServer({
  gateway: new ApolloGateway({
    serviceList: /* service list */ ,
    buildService: ({ url }) => {
      return new ContextObjectDataSource({ url });
    },
  }),
  subscriptions: false,
  context: /* context function */ ,
});

class ContextObjectDataSource extends RemoteGraphQLDataSource {
  willSendRequest({ request, context }) {
    if (context.contextObject) {
      request.extensions = {
        ...request.extensions,
        contextObject: context.contextObject,
      };
    }
  }

  didReceiveResponse({ response, context }) {
    if (response.extensions && response.extensions.contextObject) {
      context.contextObject = response.extensions.contextObject;
    }
    return response;
  }
};
  • In the implementing service's server constructor, you pass in a plugin that defines a requestDidStart() callback to take the context object out of request.extensions and add it to the implementing service context and a willSendResponse() callback to take the context object out of the implementing service context and embed it in response.extensions. This looks like:
const server = new ApolloServer({
  schema: /* implementing service schema */ ,
  plugins: [{
    requestDidStart: ({ request, context }) => {
      if (request.extensions && request.extensions.contextObject) {
        context.contextObject = request.extensions.contextObject;
      }

      return {
        willSendResponse: ({ context, response }) => {
          response.extensions = {
            ...response.extensions,
            contextObject: context.contextObject,
          };
        },
      };
    },
  }],
});

from federation-jvm.

joffrey-bion avatar joffrey-bion commented on May 22, 2024

Hi, thanks a lot for the detailed answer.

I'm sorry it was probably not clear in my question, but I had seen it was possible in the NodeJS version.
My question was about whether this was possible with this apollofederation-jvm project, in Java, sorry for the confusion 😥

from federation-jvm.

joffrey-bion avatar joffrey-bion commented on May 22, 2024

Wait, maybe I got confused.

I thought this project was meant to build a federation gateway instead of the nodeJS federation gateway.
But I believe I was wrong and this is not the case. Is this project instead meant to be used for downstream GraphQL servers, just so that they expose their schema in a federation-compliant way?

If this is the case, then please feel free to close this issue as irrelevant.

from federation-jvm.

sachindshinde avatar sachindshinde commented on May 22, 2024

But I believe I was wrong and this is not the case. Is this project instead meant to be used for downstream GraphQL servers, just so that they expose their schema in a federation-compliant way?

Yep, that's exactly right, it's a thin layer around graphql-java to make it federation-compliant.

If this is the case, then please feel free to close this issue as irrelevant.

Sounds good, I'll close out the issue. If you end up having any troubles with Apollo Gateway, feel free to file an issue on their GitHub page.

from federation-jvm.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.