1

Trying to add authentication to my appolo projects. I have the following structure

Class AuthenticatedDataSource extends RemoteGraphQLDataSource {
  willSendRequest({request, context }) {
     request.http.headers.set('required-auth', context.requiredAuth);
     request.http.headers.set('authenticated', context.authenticated);
    request.http.headers.set('user-role', context.userRole);
  }
}
const gateway = new ApolloGateway({
  supergraphSdl,
  debug: false,
  buildService({
   return new AuthenticatedDataSource({ url });
  },

});

const server = new ApolloServer({
  gateway,
  subscriptions: false,
  plugins: [ApolloServerPluginDrainHttpServer({
    httpServer
  })],
  context: async({ req }) => {
    const token = req.headers['authorization'] || '';
    const domain = req.headers['domain'] || '';
    console.log('req url: ', req.headers.originalUrl)
    const user = await getUserRole(token, domain);
    return { ...user };
  },
 });

The flow is the following, the client sends a domain and auth token. The server fetches a configuration, gets all require information about authentication and adds it to context so all services will get the information about user role, and authentication status. But the problem is that, the call is taking to long and the server does multiple calls with empty domain and token. Log looks like this

Info
2022-07-25T23:35:00.054339915Zreq url: undefined
Info
2022-07-25T23:35:00.054390450ZFetching appConfigs for domain:
Info
2022-07-25T23:35:00.066468331Zreq url: undefined
Info
2022-07-25T23:35:00.066975920ZFetching appConfigs for domain:

can someone help me to understand what I am doing wrong?

Danis
  • 1,978
  • 3
  • 16
  • 27
  • I don't know if we encountered the same issue but for me the endless call to the context creation was due to the GraphQL playground schema polling. Check this link https://community.apollographql.com/t/apollo-server-context-function-executed-every-2-seconds/1710/5 – Oneb May 10 '23 at 19:08

0 Answers0