Gusto / apollo-federation-ruby

A Ruby implementation of Apollo Federation
MIT License
216 stars 72 forks source link

Fowarding headers from Supergraph to subgraphs #255

Closed KMoscIszko closed 1 year ago

KMoscIszko commented 1 year ago

Hi. That's my blocker while developing backend. Passing headers from supergraph to subgraphs. Through logger I can observe that the node gateway is receiving headers but once I debug Rails graphql controller, It's not getting anything. graphql_controller.rb

   def execute
      variables = prepare_variables(params[:variables])
      query = params[:query]
      operation_name = params[:operationName]
      context = {
        current_user: current_user,
        session: session,
        tracing_enabled: ApolloFederation::Tracing.should_add_traces(request.headers),

      }
      result = UsersSchema.execute(query, variables: variables, context: context, operation_name: operation_name)
      render json: result
    rescue StandardError => e
      raise e unless Rails.env.development?
      handle_error_in_development(e)
    end

gateway.ts

 const { ApolloServer } = require('apollo-server');
const { ApolloGateway, RemoteGraphQLDataSource } = require('@apollo/gateway');
const { readFileSync } = require('fs');
const supergraphSdl = readFileSync('./supergraph.graphql').toString();
const { ApolloLogPlugin } = require('apollo-log');
const plugins = [ApolloLogPlugin];

const gateway = new ApolloGateway({
    supergraphSdl,
});

const server = new ApolloServer({
    gateway,
    plugins,
    context: ({ req }) => {
        return {
          serverRequest: req,
          Headers: {
            "Authorization": req.headers.authorization
          }
        };
    }
});

server.listen().then(({ url }) => {
    console.log(`🚀 Gateway ready at ${url}`);
}).catch(err => {console.error(err)});

The headers are passed in "rawHeaders":["Authorization","T9J5mDf9ddZIYQVpao/2vbPXFxyK26EliA2G4gp1BEbL9xY5UE/7ElTMjeg9xNEdJFx82cy--lEBj3HbqBbPR77D0--qyIdK/N3rIN2w8SzBQwEMg="

sethc2 commented 1 year ago

This is probably a better question for https://github.com/apollographql/federation as this is not related to the apollo-federation-ruby gem.

Apollo also has a support article detailing how it should work - https://support.apollographql.com/hc/en-us/articles/6135650062355-How-do-I-forward-headers-from-gateway-to-subgraph-.

You can try something like this perhaps.

class AuthenticatedDataSource extends RemoteGraphQLDataSource {
  willSendRequest({ request, context }) { // this context is whatever you returned in constructing the ApolloServer
    request.http.headers.set("Authorization", context.authorizationHeader);
  }
}

const gateway = new ApolloGateway({
  supergraphSdl,
  buildService({ name, url }) {
    return new AuthenticatedDataSource({ url });
  },
});

const server = new ApolloServer({
    gateway,
    plugins,
    context: ({ req }) => {
        return {
          serverRequest: req,
          authorizationHeader: req.headers.authorization
        };
    }
});
KMoscIszko commented 1 year ago

@sethc2 Yeah, I got it, that's line req.headers.authorization I needed, I came to this conclusion a moment before you posted, yet thanks :D