menu

Apollo

A community of developers, designers and others who love Apollo and GraphQL. 🚀

Channels
# All channels
view-forward
# General
view-forward
# Apollo Angular
view-forward
# Announcements
view-forward
# Apollo Android
view-forward
# Apollo Client
view-forward
# Apollo iOS
view-forward
# Apollo Link
view-forward
# Apollo Link Rest
view-forward
# Local State
view-forward
# Apollo Platform
view-forward
# Apollo Server
view-forward
# Apollo Tooling
view-forward
# Contributing
view-forward
# Docs
view-forward
# Events
view-forward
# GraphQL Tools
view-forward
# Jobs
view-forward
# Random
view-forward
# React Apollo
view-forward
# Showcase
view-forward
# Subscriptions
view-forward
# Testing
view-forward
# Vue Apollo
view-forward
Team

Sharing context across services through Gateway is not working

July 17, 2019 at 4:53pm

Sharing context across services through Gateway is not working

July 17, 2019 at 4:53pm (Edited 11 months ago by @abernix)

https://www.apollographql.com/docs/apollo-server/federation/implementing/

If i use this code to pass the app token from gateway to underlying services. it throws me an error of " Error: Apollo Server requires either an existing schema, modules or typeDefs "

Source Code Sample :

import { ApolloServer } from "apollo-server";
import { ApolloGateway, RemoteGraphQLDataSource } from "@apollo/gateway";
const gateway = new ApolloGateway({
serviceList: [
{ name: "products", url: "http://localhost:4001" }
// other services
],
buildService({ name, url }) {
return new RemoteGraphQLDataSource({
url,
willSendRequest({ request, context }) {
// pass the user's id from the context to underlying services
// as a header called `user-id`
request.http.headers.set("user-id", context.userId);
}
});
}
});
const server = new ApolloServer({
gateway,
context: ({ req }) => {
// get the user token from the headers
const token = req.headers.authorization || "";
// try to retrieve a user with the token
const userId = getUserId(token);
// add the user to the context
return { userId };
}
});
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});

September 9, 2019 at 9:44pm
Well, just add schema or typeDefs/resolvers to ApolloServer config
  • reply
  • like

September 10, 2019 at 10:31pm
ignore my comment; the docs disagreed with me
Edited
  • reply
  • like

March 23, 2020 at 2:15pm
I am having this exact issue
  • reply
  • like