question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Apollo Server 2 Hapi context

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
maxnachlingercommented, Aug 28, 2018

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.

1reaction
maxnachlingercommented, Feb 26, 2019

@obedparla route provides options for the graphql route in Hapi, have a look here - https://github.com/apollographql/apollo-server/blob/ad4240295469821c24aaf2f1032b12882dd3b6f9/packages/apollo-server-hapi/src/ApolloServer.ts#L59 - route is if type hapi.RouteOptions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

API Reference: apollo-server - Apollo GraphQL Docs
This API reference documents the exports from the apollo-server package. class ApolloServer. The core of an Apollo Server implementation.
Read more >
apollo-server-hapi - npm
This package has only been tested with @hapi/hapi 20.1.2 and higher; that is the minimum version of Hapi that supports Node 16. A...
Read more >
apollo-server-hapi16 - npm Package Health Analysis - Snyk
apollo -server-hapi16. v0.0.2. Node.js GraphQL server for Hapi v16 For more information about how to use this package see README.
Read more >
GraphQL Code Libraries, Tools and Services
GitHubapollographql/apollo-client ... JavaScript GraphQL client, designed to work well with React, React Native, Angular 2, or just plain JavaScript.
Read more >
apollo-server | Yarn - Package Manager
The apollo-server package is part of Apollo Server v2 and v3, which are now ... A TypeScript GraphQL Server for Express, Koa, Hapi,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found