apollo-server-integrations / apollo-server-integration-hapi

MIT License
3 stars 3 forks source link

@apollo/server graphql playground not launches #24

Closed bharat-vaggu closed 1 year ago

bharat-vaggu commented 1 year ago

apollo server 4 graphql playground is not launches when I integrate with hapi. I have config that all routes should have jwt token. How can I skip to validate jwt for graphql playground lanuches. It was working with apollo server 3 (in this case I was not using @as-integrations/hapi). When I started using @as-integrations/hapi with apollo server 4, it is not working. Can somebody please help.

trevor-scheer commented 1 year ago

@bharat-vaggu can you share a reproduction or errors you're seeing? It sounds like you might be blocking the route.

arimus commented 1 year ago

@bharat-vaggu

Two things. First, make sure that you have enabled the plugin for the playground in Apollo. I'm assuming that you have done this, but adding the example here anyways:

const apolloServer = new ApolloServer({
  typeDefs: mergedSchemas,
  resolvers,
  plugins: [
    process.env.NODE_ENV === 'production'
      ? ApolloServerPluginLandingPageDisabled() // disable plugin in production mode
      : ApolloServerPluginLandingPageGraphQLPlayground() // enable plugin in non-production mode
  ]
});

And then you can configure your routes specifically for Apollo. As you can see in the example below, the routes are both using a 'jwt' auth strategy, but the getRoute mode is set to 'try'. You can also disable the auth altogether by setting it to false. Either way, the playground should then render.

await server.register({
  plugin: hapiPlugin,
  options: {
    apolloServer,
    path: '/graphql',
    getRoute: {
      options: {
        auth: {
          strategy: 'jwt',
          mode: 'try'
        }
      }
    },
    postRoute: {
      options: {
        auth: {
          strategy: 'jwt',
        },
        payload: {
          maxBytes: 100 * 1024 * 1024
        }
      }
    },
  } as HapiApolloPluginOptions<any>
});
bharat-vaggu commented 1 year ago

if I set getRoute mode to 'try', it's working. Thank you!

bharat-vaggu commented 1 year ago

I have configured like below

await server.register({ plugin: hapiPlugin, options: { apolloServer, path: '/graphql', getRoute: { options: { auth: { strategy: 'jwt', mode: 'try' } } }, postRoute: { options: { auth: { strategy: 'jwt', }, payload: { maxBytes: 100 1024 1024 } } }, } as HapiApolloPluginOptions });

But when I call graphql apis with GET method without authorization token, it is not giving authotization error instead it is allowing application. How can we handle this?

Note - It was working with Apollo Server 3. I know, All graphql call should be with POST method, but safer side it should throw an error with GET method when token is not passed or expired

arimus commented 1 year ago

@bharat-vaggu The "try" means that auth is optional for GET methods.

You can check inside your GraphQL context handler (same config level as getRoute) to only allow certain calls with more granularity. Example:

      context: async ({ request, h }) => {
        const user: User = this.getFromContext('user');
        const authenticated = !!this.getFromContext('jwt');

        // example - don't allow schema request to guest user
        // if (!user && (request.payload as any).operationName === 'IntrospectionQuery') {
        //   console.log('denying anonymous user from schema introspection');
        //   throw Boom.unauthorized('Authentication required');
        // }

        // example - reject all unauthenticated requests
        // if (!user) {
        //   throw Boom.unauthorized('Authentication required');
        //  return h.response().code(401);
        //}

        return { user, authenticated };
      }

Hope this helps. Sorry for the late reply, got lost in a flood of email.

arimus commented 1 year ago

@bharat-vaggu Closing this for now. Re-open or open a new if you still have issues you need assistance with!