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

graphql along with existing hapijs routes #97

Closed
BhaskaranR opened this issue Aug 16, 2016 · 9 comments
Closed

graphql along with existing hapijs routes #97

BhaskaranR opened this issue Aug 16, 2016 · 9 comments
Assignees
Milestone

Comments

@BhaskaranR
Copy link

Hi,
I currently have some rest services which i don’t want to convert to graphql. For the new services i would like to use GraphQL.

I tried registering both hapijs rest api routes and graphql. I could still access the rest api but could not access graphql route unless the auth strategy is commented. I need the auth for my existing routes. How do I configure auth for graphql route?

server.auth.strategy('jwt', 'jwt', true,
{ key: config.get("jwtSecret"),
validateFunc: validate,
verifyOptions: { ignoreExpiration: true }
});

server.register({
 register: new apollo.ApolloHAPI(),
   options: graphqlOptions,
   routes: { prefix: '/graphql' },
});

server.register({
   register: new apollo.GraphiQLHAPI(),
   options: { endpointURL: '/graphql' },
});

   server.route(user.routes);
   server.route(device.routes);
   server.route(posts.routes);

@helfer
Copy link
Contributor

helfer commented Aug 16, 2016

Can you describe the problem a bit more accurately? Do you not want to authenticate the requests made to the graphql route? If that's your issue, I think it might be a HAPI configuration problem, not an issue with Apollo Server.

@BhaskaranR
Copy link
Author

Do you not want to authenticate the requests made to the graphql route? no, but the existing rest api routes take in auth, which break the graphql route. I get a 401 for the graphql route. how can i specify config : { auth : false }for graphql route?

{"statusCode":401,"error":"Unauthorized","message":"Missing authentication"}

@BhaskaranR
Copy link
Author

@helfer any pointer please? do you have any example that uses both rest service and graphql with the auth strategy?

@BhaskaranR
Copy link
Author

@helfer I have hit a road block with this. Can you please let me know if the apollo server plugin takes the config : { auth : false } or the auth strategy set by hapi server?? I tried setting in the options, routes etc. nothing works. Am I doing something wrong?

@helfer
Copy link
Contributor

helfer commented Aug 17, 2016

@BhaskaranR I don't know, because I didn't write that part. Maybe @nnance can help?

If I were you, I would try send the authentication token with the graphql request as well, because presumably you already have it on the client? Alternatively you can run two separate servers.

PS: I see you already found this issue, so I think you have your answer: outmoded/discuss#162

PPS: It seems this would be easy if we used server.route instead of server.register. @nnance what's the reason we need to use register vs. route?

@nnance
Copy link
Contributor

nnance commented Aug 18, 2016

@BhaskaranR I am working on some improvements to the HAPI implementation and this is one area I am working on improving. I will reach out when I have more detail

@SimonDegraeve
Copy link

I see only three ways to allow full control on HAPI routes:

  1. Get the route configuration from the plugin constructor.
import { Server } from 'hapi';
import { ApolloHAPI as Apollo } from 'apollo-server';

const server = new Server();
server.connection({ port: 3000 });

server.register({
  register: new Apollo({
    route: {
      // Route configuration
    }
  }),
  options: { schema: myGraphQLSchema },
  routes: { prefix: '/graphql' },
});
  1. Export a handler creator and let people build their own route if they want to.
import { Server } from 'hapi';
import { createApolloHandler } from 'apollo-server';

const server = new Server();
server.connection({ port: 3000 });

const apolloHandler = createApolloHandler({ schema: myGraphQLSchema });

server.route({
  method: 'POST',
  path: '/',
  config: {
    handler: apolloHandler,
    // Route configuration
  },
});
  1. Change the shape of the options passed to the register function.
import { Server } from 'hapi';
import { ApolloHAPI as Apollo } from 'apollo-server';

const server = new Server();
server.connection({ port: 3000 });

server.register({
  register: new Apollo(),
  options: { 
    route: {
      // Route configuration
    },
    schema: myGraphQLSchema },
  routes: { prefix: '/graphql' },
});

The 3) is the closest to the HAPI ecosystem but require a breaking change.

@helfer
Copy link
Contributor

helfer commented Aug 27, 2016

@SimonDegraeve @nnance I think we'll bump the version for the next release, so a breaking change won't be an issue. Let's make sure we make the API as close to perfect as we can!

@helfer helfer added this to the 0.3 milestone Aug 27, 2016
@nnance nnance mentioned this issue Sep 8, 2016
4 tasks
@nnance
Copy link
Contributor

nnance commented Sep 8, 2016

@SimonDegraeve @BhaskaranR I have opened a PR that should resolve this issue. Feedback welcome. #127

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

5 participants