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-hapi - multiple instances #931

Closed
MikeSchaap opened this issue Mar 31, 2018 · 9 comments
Closed

apollo-server-hapi - multiple instances #931

MikeSchaap opened this issue Mar 31, 2018 · 9 comments
Labels
📝 documentation Focuses on changes to the documentation (docs)

Comments

@MikeSchaap
Copy link

MikeSchaap commented Mar 31, 2018

First off, thanks for all the time and effort you guys (and the community) put into developing Apollo! It's really easy to get started and it works great!

I'm just hitting a few bumps with my specific implementation and i'm just wondering if I miss interpreted the documentation and i'm doing it all wrong or if this is something that hasn't been tried before and documented.

When i'm trying to achieve is having 3 different GraphQL endpoints each with their own set of schemas. Why? Because we want to show a different Model for user type A, B & Admin. I know we can just return different objects back from the business layer and just keep the fields empty for user types which are not allowed to see it. But the company i'm working for doesn't want to expose any information about the models to user types which aren't allowed to see it. (So they don't allow to document each field and show which user type has access it).

This is why i want to run multiple (3 in this case) instances of GraphQL each with their own set of schemas (and different models per endpoint). I haven't found anything about this in the docs and when i tried to register the plugin in hapi.js multiple times it gave an error that the plugin was already registered. I managed to 'fix' this by adding the multiple: true variable to the plugin. But i was wondering if this is 'safe' to do? Will i run into conflicts when running multiple instances of GraphQL on the same hapi.js server?

Would love to hear from you guys.

Kind regards,

Mike

@ghost ghost added the 📝 documentation Focuses on changes to the documentation (docs) label Mar 31, 2018
@flippidippi
Copy link

We also have a use case for this and adding multiple: true like below seemed to work. If this doesn't cause any issues I think it should be enabled by default.

graphqlHapi.multiple = true
graphiqlHapi.multiple = true

@tlbignerd
Copy link

I have to agree, setting the default to true would make more sense. It's certainly the standard for the other servers, where we can add graphql endpoints with different routes and options and not be required to configure a global setting that enables this functionality.

@robmcguinness
Copy link

Running into this same issue with 2.x. Previously I was running a /graphql and /mockql endpoint from one server by running two hapi plugins with different configurations, resolvers and schemas. In 1.x, I would override the plugin name to circumvent this issue:

plugin: {
    name: 'mockql',
    register: graphqlHapi.register,
  },

In 2.x, it doesn't seem possible to override the hapi plugin defaults using applyMiddleware without changes to apollo-server-hapi/ApolloServer as well.

@jpgarcia
Copy link

@robmcguinness facing same issue here. Did you find any workaround to get two endpoints? I'm getting the following error:

New route /.well-known/apollo/server-health conflicts with existing /.well-known/apollo/server-health

@jpgarcia
Copy link

I could workaround the server-health error by adding disableHealthCheck: true, to the appyMiddleware options.

Now the error is Plugin graphql already registered 🤔

@robmcguinness
Copy link

robmcguinness commented Sep 25, 2018

@jpgarcia,

I extended ApolloServer and overrode applymiddlware with necessary changes to enable multiple Apollo Hapi plugins. Use at your own risk. Good luck! 😀

const { ApolloServer } = require('apollo-server-hapi');
const { graphqlHapi } = require('apollo-server-hapi/dist/hapiApollo');

class MockApolloServer extends ApolloServer {
  async applyMiddleware({ app, path, route, cors }) {
    if (!path) path = '/mockql';

    graphqlHapi.multiple = true;

    await app.register({
      plugin: graphqlHapi,
      options: {
        path,
        graphqlOptions: this.createGraphQLServerOptions.bind(this),
        route:
          route !== undefined
            ? route
            : {
                cors: cors !== undefined ? cors : true,
              },
      },
    });

    this.graphqlPath = path;
  }
}

@jpgarcia
Copy link

Awesome! thanks for the snippet!

@flippidippi
Copy link

flippidippi commented Nov 19, 2018

If you wanna live even more dangerously use:

const { ApolloServer } = require('apollo-server-hapi')
const hapiApollo_1 = require('apollo-server-hapi/dist/hapiApollo')

hapiApollo_1.graphqlHapi.multiple = true

class MockApolloServer extends ApolloServer {
  async applyMiddleware({ app, cors, path, disableHealthCheck, onHealthCheck }) {
    return super.applyMiddleware({ app, cors, path, disableHealthCheck, onHealthCheck, hapiApollo_1 })
  }
}

@abernix
Copy link
Member

abernix commented Jul 2, 2019

I'm going to close this issue since it hasn't received any activity in quite some time, but I'm sympathetic to re-opening it if it's still a problem. It's also entirely possible that this is a documentation issue and if anyone feels that way, please do open a PR to the documentation (which lives within this repository in /docs/source) to adjust the suggestions for Hapi, as necessary.

Lastly, we're introducing a getMiddleware method in Apollo Server 2.7.0 (via #2435) which might help ease the pain here. If anyone is interested in trying that out, please see the latest alpha version of Apollo Server 2.7.0 by consulting the Publish commits in the Apollo Server 2.7.0 release PR (#2937).

@abernix abernix closed this as completed Jul 2, 2019
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
📝 documentation Focuses on changes to the documentation (docs)
Projects
None yet
Development

No branches or pull requests

6 participants