Skip to content

Instantly share code, notes, and snippets.

@suciuvlad
Last active August 14, 2018 08:25
Show Gist options
  • Select an option

  • Save suciuvlad/e083e6dd7d20dcf48ec9530d2ba83160 to your computer and use it in GitHub Desktop.

Select an option

Save suciuvlad/e083e6dd7d20dcf48ec9530d2ba83160 to your computer and use it in GitHub Desktop.
apollo server 2
app.use(
'/graphql',
bodyParser.json(),
graphqlExpress(async (req, res) => {
//afterware to attach the headers to current request object
const addCookiesLink = new ApolloLink((operation, forward) => {
return forward(operation).map(response => {
const context = operation.getContext();
const cookies = context.response.headers.get('set-cookie');
if (!cookies) return response;
const responseCookies =
cookies instanceof Array
? cookies.map(Cookie.parse)
: [Cookie.parse(cookies)];
const casinoSession = responseCookies.find(
cookie => cookie.key == '_casino_session'
);
casinoSession &&
casinoSession.value &&
res.cookie(casinoSession.key, casinoSession.value, {
expires: casinoSession.expires,
httpOnly: true
});
return response;
});
});
const makeAPIServiceLink = () =>
addCookiesLink.concat(
createHttpLink({
uri: config.API_GRAPHQL_SERVER_URL,
credentials: 'include',
headers: {
cookie: req.header('Cookie') // no access to req object to send the headers
},
fetch
})
);
const APIServiceLink = makeAPIServiceLink();
const APIServiceSchemaDefinition = await introspectSchema(APIServiceLink);
const APIServiceExecutableSchema = makeRemoteExecutableSchema({
schema: APIServiceSchemaDefinition,
link: APIServiceLink
});
const schema = mergeSchemas({
schemas: [APIServiceExecutableSchema, ....],
});
return {
schema: schema,
tracing: true,
cacheControl: {
defaultMaxAge: 3500
},
graphiql: true,
context: { req, res }
};
})
);
app.get('/graphiql', graphiqlExpress({ endpointURL: '/graphql' }));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment