Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

schema stitching question. modify initial request and pass it to remoteServer #737

Closed
lonyele opened this issue Dec 26, 2017 · 8 comments
Closed

Comments

@lonyele
Copy link

lonyele commented Dec 26, 2017

I don't know if it's right to ask here... but I couldn't find anywhere.
There is one api gateway graphql/express server(get all the request from clients) and
multiple microservice graphql/express servers(auth, business logic1 , business logic2, business logic3 etc) that are connected to this gateway using schema stitching.(mergeSchema)

  1. if client requests with Bearer token, normally I can retrieve token from headers and use context to pass down authorization token and use it at the resolvers. But How can I pass this to remote server?
    It seems like modifying request object doesn't affect anything.(it only affects the object in gateway server)
    It seems like copying graphql specific data only and send it to remoteServer.(Therefore, I couldn't get the Authorization header at my remoteServer)
    so far, I couldn't find a way to pass my jwt token from api gateway to other remote servers.

Am I thinking something wrong?

@booboothefool
Copy link

booboothefool commented Jan 22, 2018

@lonyele I think I was just able to solve this using Apollo Link with setContext.

In your gateway:

  const ContextLink = setContext((request, previousContext) => ({
    headers: {
      authorization: previousContext.graphqlContext.headers.authorization,
    },
  }));
  const AuthLink = new HttpLink({
    uri: 'http://localhost:3001/graphql',
    fetch,
  });
  const AuthSchema = makeRemoteExecutableSchema({
    schema: await introspectSchema(AuthLink),
    link: ApolloLink.from([ContextLink, AuthLink]),
  });

  const mergedSchema = mergeSchemas({
    schemas: [AuthSchema, ...otherSchemas],
  });

Now Auth microservice has token available in headers.

@lonyele
Copy link
Author

lonyele commented Jan 30, 2018

I don't know why I couldn't see this earlier, but It seems like It would work with your method. I'll try this soon. Thanks a lot!!

@lonyele lonyele closed this as completed Jan 30, 2018
@lucasconstantino
Copy link

How about something from the mergedSchemas altering headers on the response from above? @booboothefool any ideas how could I accomplish this?

@acrogenesis
Copy link

acrogenesis commented Aug 20, 2018

When trying to do this I always get "message": "Cannot read property 'Authorization' of undefined", I've tried with caps and without, also just matching all headers headers: previousContext.graphqlContext.headers, but it still doesn't pass.

Edit:

This fixed it:

schema.ts:

const ContextLink = setContext((request, previousContext) => ({
    headers: {
      authorization: previousContext.graphqlContext.headers.authorization,
    }
  }));

server.ts:

app.use(
    '/graphql',
    bodyParser.json(),
    graphqlExpress(async request => ({
      schema: await makeMergedSchema(),
      context: {
        headers: request ? request.headers : null
      }
    }))
  );

@sgil-carecloud
Copy link

what if you want to pass in a whole context? For example, authorization server returns policies that are tied to the user, i dont want to send the token down to my stitched serve, i want to pass down the user object i set at my context.

@j-mcgregor
Copy link

This might sound dumb but where is setContext() coming from?

@digibeuk
Copy link

digibeuk commented Apr 18, 2019

This might sound dumb but where is setContext() coming from?

I struggled with this also, he is using apollo-link-context

import { setContext } from "apollo-link-context";
import { ApolloLink } from 'apollo-link';

@xpolb01
Copy link

xpolb01 commented Nov 12, 2019

How do you use it in the service that you have stitched?
Something like this:
context: ({ headers }) => {}

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 20, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants