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

Apollo Server 2 Hapi context #1563

Closed
musab opened this issue Aug 22, 2018 · 11 comments
Closed

Apollo Server 2 Hapi context #1563

musab opened this issue Aug 22, 2018 · 11 comments

Comments

@musab
Copy link

musab commented Aug 22, 2018

How can I get my request object passed into my context? I am trying to verify the user is authenticated.

I am using the hapi pkg yar to manage the session.

For example in my root path, when I do the following

  app.route({
    method: 'GET',
    path: '/',
    options: {
      auth: 'simple'
    },
    handler(request) {
      return 'Logged in' + JSON.stringify(request.yar.get('auth'))
    }
  })

I see Logged in{"username":"admin","isValid":true,"id":"1","name":"John"}

But the following returns undefined:

  const server = new ApolloServer({
    schema: query,
    tracing: true,
    context: async ({ request }) => {
      return {
        request
      }
    }
  })


const resolvers = {
  Query: {
    rules: async (_, args, context) => {
      console.log(context.request.yar.get('auth'))
      return mock.rules
    }
  }
}

Here's a Github gist of my whole index.ts file https://gist.github.com/apollomusa/1afff80b29600c863705dc2cc174e86a

@musab
Copy link
Author

musab commented Aug 23, 2018

Anyone experiencing? For some reason the request object that Apollo Server receives does not hold the same values the request object Hapi receives

@ILovePing
Copy link

I had a similar issue with express
have u solved ur problem?

@ILovePing
Copy link

@apollomusa I set user to req.session in express, but couldn't get user in apolloServer context definition.
const server = new ApolloServer({ schema, context: ({req}) => { const { user } = req.session; return { user }; }, });

@musab
Copy link
Author

musab commented Aug 27, 2018

@ILovePing similar issue with me, the request object in apolloServer context differs so for example

request.auth.isAuthenticated will be true for all other paths I setup in Hapi. But false when viewed within the apolloServer context:

  const server = new ApolloServer({
    schema: makeExecutableSchema(schema),
    playground: config.playground,
    context: async ({ request }) => {
      return {
        isAuthenticated: request.auth.isAuthenticated
      }
    }
  })

@musab
Copy link
Author

musab commented Aug 27, 2018

Still trying to find a solution

@ILovePing
Copy link

@apollomusa Currently,I just use a variable to store user for the first /graphql Get request

let user = null;
//...some other code here
public configureGraphQL(app: any, httpServer: any) {
    const typeDefs = this.graphQLFactory.mergeTypesByPaths('./**/*.graphql');
    const schema = this.graphQLFactory.createSchema({ typeDefs });
    app.use('/graphql', (req, res, next) => {
      if (req.session.user){
        user = req.session.user;
      }
      next();
    });
    const server = new ApolloServer({
      schema,
      context: () => {
        return { user };
      },
     });
    server.applyMiddleware({ app,
       cors: true,
       bodyParserConfig: true,
      });
    server.installSubscriptionHandlers(httpServer);
  }

@ILovePing
Copy link

This may has some other problems but helps now.
And I still couldn't figure out why req.session is different in the second /graphql Post request

@maxnachlinger
Copy link
Contributor

You can specify route options when you call await apolloServer.applyMiddleware(), for example:

  await apolloServer.applyMiddleware({
    app: server, // a hapi server instance
    path: '/my/path/to/graphql',
    route: {
      auth: 'simple',
    },
  });

@apollomusa - Hope that helps a bit.

@obedparla
Copy link

@maxnachlinger Your commend it's not clear. What does route does? I tried finding that option in the API reference documentation but there's no mention of applyMiddleware accepting a route option anywhere.

@maxnachlinger
Copy link
Contributor

@obedparla route provides options for the graphql route in Hapi, have a look here -

- route is if type hapi.RouteOptions.

@maxnachlinger
Copy link
Contributor

maxnachlinger commented Mar 9, 2019

@musab if you found an answer would you mind closing this issue?

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

4 participants