Apollo-server: Error registering graphiqlHapi to Hapi server

Created on 12 Jan 2018  ยท  16Comments  ยท  Source: apollographql/apollo-server

I'm following instructions here: https://www.apollographql.com/docs/apollo-server/graphiql.html#graphiqlHapi and here https://www.apollographql.com/docs/apollo-server/servers/hapi.html#Usage
copy pasted:

import { graphiqlHapi } from 'apollo-server-hapi';

server.register({
  register: graphiqlHapi,
  options: {
    path: '/graphiql',
    graphiqlOptions: {
      endpointURL: '/graphql',
    },
  },
});

But I can't get the registering to work. My code is now this:

const Hapi = require('hapi')
const { graphiqlHapi } = require('apollo-server-hapi')

const HOST = 'localhost'
const PORT = 3000

async function StartServer () {
  const server = new Hapi.server({ // eslint-disable-line
    host: HOST,
    port: PORT
  })

  console.log(graphiqlHapi)

  await server.register({
    name: 'funkuy',
    register: graphiqlHapi,
    options: {
      path: '/graphiql',
      graphiqlOptions: {
        endpointURL: '/graphql'
      }
    }
  })

  await server.start()

  console.log(`Server running at: ${server.info.uri}`)
}

StartServer().catch(err => {
  console.error(`StartServer failed: ${err.stack}`)
  process.exit(1)
})

And I get Errors, depending on the apollo-server-hapi version:

[email protected]

StartServer failed: AssertionError [ERR_ASSERTION]: Invalid plugin options {
  "plugin": {
    "options": {
      "path": "/graphiql",
      "graphiqlOptions": {
        "endpointURL": "/graphql"
      }
    },
    "register" [1]: {
      "name": "graphiql",
      "register": function (server, options) {\n        if (!options || !options.graphiqlOptions) {\n            throw new Error('Apollo Server GraphiQL requires options.');\n        }\n        server.route({\n            method: 'GET',\n            path: options.path || '/graphiql',\n            config: options.route || {},\n            handler: function (request, h) { return __awaiter(_this, void 0, void 0, function () {\n                var graphiqlString, response;\n                return __generator(this, function (_a) {\n                    switch (_a.label) {\n                        case 0: return [4, GraphiQL.resolveGraphiQLString(request.query, options.graphiqlOptions, request)];\n                        case 1:\n                            graphiqlString = _a.sent();\n                            response = h.response(graphiqlString);\n                            response.type('text/html');\n                            return [2, response];\n                    }\n                });\n            }); },\n        });\n    }
    }
  }
}

[1] "register" must be a Function
    at Object.exports.apply (/home/marcus/bd/digib/repos/imp-hapi-apollo/node_modules/hapi/lib/config.js:22:10)
    at internals.Server.register (/home/marcus/bd/digib/repos/imp-hapi-apollo/node_modules/hapi/lib/server.js:384:31)
    at StartServer (/home/marcus/bd/digib/repos/imp-hapi-apollo/src/fail.js:13:16)
    at Object.<anonymous> (/home/marcus/bd/digib/repos/imp-hapi-apollo/src/fail.js:26:1)
    at Module._compile (module.js:569:30)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)
    at Function.Module._load (module.js:458:3)
    at Function.Module.runMain (module.js:605:10)

[email protected]

StartServer failed: AssertionError [ERR_ASSERTION]: Invalid plugin options {
  "plugin": {
    "register": function (server, options, next) {\n    if (!options || !options.graphiqlOptions) {\n        throw new Error('Apollo Server GraphiQL requires options.');\n    }\n    if (arguments.length !== 3) {\n        throw new Error(\"Apollo Server GraphiQL expects exactly 3 arguments, got \" + arguments.length);\n    }\n    server.route({\n        method: 'GET',\n        path: options.path || '/graphiql',\n        config: options.route || {},\n        handler: function (request, reply) {\n            var query = request.query;\n            GraphiQL.resolveGraphiQLString(query, options.graphiqlOptions, request).then(function (graphiqlString) {\n                reply(graphiqlString).header('Content-Type', 'text/html');\n            }, function (error) { return reply(error); });\n        },\n    });\n    return next();\n},
    "options": {
      "path": "/graphiql",
      "graphiqlOptions": {
        "endpointURL": "/graphql"
      }
    },
    "name" [1]: -- missing --
  }
}

[1] "name" is required
    at Object.exports.apply (/home/marcus/bd/digib/repos/imp-hapi-apollo/node_modules/hapi/lib/config.js:22:10)
    at internals.Server.register (/home/marcus/bd/digib/repos/imp-hapi-apollo/node_modules/hapi/lib/server.js:384:31)
    at StartServer (/home/marcus/bd/digib/repos/imp-hapi-apollo/src/fail.js:13:16)
    at Object.<anonymous> (/home/marcus/bd/digib/repos/imp-hapi-apollo/src/fail.js:26:1)
    at Module._compile (module.js:569:30)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)
    at Function.Module._load (module.js:458:3)
    at Function.Module.runMain (module.js:605:10)

(hapi and graphql) package versions:

โ”œโ”€โ”ฌ apollo-server-hapi@(tried both 1.2.0 and 1.3.0) 
โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ [email protected] deduped
โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ [email protected] deduped
โ”‚ โ”‚ โ””โ”€โ”ฌ [email protected]
โ”œโ”€โ”ฌ [email protected]
โ”œโ”€โ”ฌ [email protected]
โ”‚ โ”œโ”€โ”ฌ [email protected]
โ””โ”€โ”ฌ [email protected]

It seems to me like there is some version mismatching going on, but there is no warnings when npm installing.

๐Ÿ–‡๏ธ hapi

Most helpful comment

Fix: change register to plugin.
After messing around and reading docs of Hapi (https://hapijs.com/api) I see that it's just a change of that one fieldname like this:

  await server.register({
    plugin: graphiqlHapi,
    options: {
      path: '/graphiql',
      graphiqlOptions: {
        endpointURL: '/graphql'
      }
    }
  })

IMO it would be nice if the documentation would be updated to be compatible with a single version and it would state which version that is.
(https://www.apollographql.com/docs/apollo-server/graphiql.html#graphiqlHapi)

i changed register to plugin, but still the same issue

All 16 comments

Fix: change register to plugin.
After messing around and reading docs of Hapi (https://hapijs.com/api) I see that it's just a change of that one fieldname like this:

  await server.register({
    plugin: graphiqlHapi,
    options: {
      path: '/graphiql',
      graphiqlOptions: {
        endpointURL: '/graphql'
      }
    }
  })

IMO it would be nice if the documentation would be updated to be compatible with a single version and it would state which version that is.
(https://www.apollographql.com/docs/apollo-server/graphiql.html#graphiqlHapi)

Hi,

https://github.com/ignivalancy/hano-graphql

please find GraphQL, Hapi and Node Project with full configuration.

You can do something like this in your server.js:

'use strict'

const Hapi = require('hapi')
const { graphqlHapi, graphiqlHapi } = require('apollo-server-hapi')

const myGraphQLSchema = "" // ... define or import your schema here!

const HOST = 'localhost'
const PORT = 3000

async function start_server() {
  const server = new Hapi.server({
    host: HOST,
    port: PORT,
  })

  await server.register({
    plugin: graphqlHapi,
    options: {
      path: '/gql',
      graphqlOptions: {
        schema: myGraphQLSchema,
      },
      route: {
        cors: true,
      },
    },
  })

  await server.register({
    plugin: graphiqlHapi,
    options: {
      path: '/graphiql',
      graphiqlOptions: {
        endpointURL: '/gql'
      }
    }
  })

  try {
    await server.start()
  } catch (err) {
    console.log(`Error while starting server: ${err.message}`)
  }

  console.log(`Server running at: ${server.info.uri}`)
}

start_server()

This is running with package.json dependencies versions as belows:

  "dependencies": {
    "apollo-server-hapi": "^1.3.4",
    "graphql": "^0.13.2",
    "graphql-tools": "^2.23.1",
    "hapi": "^17.3.0"
  }

NOTE: _NodeJS doesn't support the features import and export yet. To run this code with these features you need a compiler (e.g. Babel) to convert this to require, so NodeJS can understand your code._

hi, we got the same issue after upgrading to 2.0. Any idea???

Is there any improvement in this topic? I have also registered graphql, and graphiql unfortunately got the same error.

[1] "register" is required
at new AssertionError (internal/assert.js:269:11)
at Object.exports.assert (C:UsersweberDocumentsprojectsxrmnode_moduleshapinode_moduleshoeklibindex.js:517:11)
at Object.exports.apply (C:UsersweberDocumentsprojectsxrmnode_moduleshapilibconfig.js:22:10)
at internals.Server.register (C:UsersweberDocumentsprojectsxrmnode_moduleshapilibserver.js:410:31)
at init (C:UsersweberDocumentsprojectsxrmsrcserverserver.ts:23:18)
at Object. (C:UsersweberDocumentsprojectsxrmsrcserverserver.ts:82:1)
at Module._compile (internal/modules/cjs/loader.js:688:30)
at Module.m._compile (C:UsersweberDocumentsprojectsxrmnode_modulests-nodesrcindex.ts:439:23)
at Module._extensions..js (internal/modules/cjs/loader.js:699:10)
at Object.require.extensions.(anonymous function) [as .ts] (C:UsersweberDocumentsprojectsxrmnode_modulests-nodesrcindex.ts:442:12)
at Module.load (internal/modules/cjs/loader.js:598:32)
at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
at Function.Module._load (internal/modules/cjs/loader.js:529:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:741:12)
at Object. (C:UsersweberDocumentsprojectsxrmnode_modulests-nodesrcbin.ts:157:12)
at Module._compile (internal/modules/cjs/loader.js:688:30)
generatedMessage: false,
name: 'AssertionError [ERR_ASSERTION]',
code: 'ERR_ASSERTION',
actual: false,
expected: true,
operator: '==' }

    await server.register([{
        plugin: graphqlHapi,
        options: {
            path: '/graphql',
            graphqlOptions: {
                graphQLSchema
            },
            route: {
                cors: true
            }
        }
    }, {
        plugin: graphiqlHapi,
        options: {
            path: '/graphiql',
            graphiqlOptions: {
                endpointURL: 'graphql'
            },
            route: {
                cors: true
            },
        }
    }])

Is there any improvement in this topic? I have also registered graphql, and graphiql unfortunately got the same error.

[1] "register" is required
at new AssertionError (internal/assert.js:269:11)
at Object.exports.assert (C:UsersweberDocumentsprojectsxrmnode_moduleshapinode_moduleshoeklibindex.js:517:11)
at Object.exports.apply (C:UsersweberDocumentsprojectsxrmnode_moduleshapilibconfig.js:22:10)
at internals.Server.register (C:UsersweberDocumentsprojectsxrmnode_moduleshapilibserver.js:410:31)
at init (C:UsersweberDocumentsprojectsxrmsrcserverserver.ts:23:18)
at Object. (C:UsersweberDocumentsprojectsxrmsrcserverserver.ts:82:1)
at Module._compile (internal/modules/cjs/loader.js:688:30)
at Module.m._compile (C:UsersweberDocumentsprojectsxrmnode_modulests-nodesrcindex.ts:439:23)
at Module._extensions..js (internal/modules/cjs/loader.js:699:10)
at Object.require.extensions.(anonymous function) [as .ts] (C:UsersweberDocumentsprojectsxrmnode_modulests-nodesrcindex.ts:442:12)
at Module.load (internal/modules/cjs/loader.js:598:32)
at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
at Function.Module._load (internal/modules/cjs/loader.js:529:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:741:12)
at Object. (C:UsersweberDocumentsprojectsxrmnode_modulests-nodesrcbin.ts:157:12)
at Module._compile (internal/modules/cjs/loader.js:688:30)
generatedMessage: false,
name: 'AssertionError [ERR_ASSERTION]',
code: 'ERR_ASSERTION',
actual: false,
expected: true,
operator: '==' }

    await server.register([{
        plugin: graphqlHapi,
        options: {
            path: '/graphql',
            graphqlOptions: {
                graphQLSchema
            },
            route: {
                cors: true
            }
        }
    }, {
        plugin: graphiqlHapi,
        options: {
            path: '/graphiql',
            graphiqlOptions: {
                endpointURL: 'graphql'
            },
            route: {
                cors: true
            },
        }
    }])

Apollo Server 2 includes GraphQL Playground instead of GraphiQL.

Related to this -> https://github.com/apollographql/apollo-server/issues/856#issuecomment-424969345

@shehanrg Thanks works now like a charm. Can this issue then be closed?

Yes, I think this issue can be closed!

I got the following error

`(node:7014) UnhandledPromiseRejectionWarning: AssertionError [ERR_ASSERTION]: Invalid plugin options {
  "plugin": {
    "options": {
      "path": "/graphiql",
      "graphiqlOptions": {
        "endpointURL": "/graphql"
      },
      "route": {
        "cors": true
      }
    },
    "register" [1]: -- missing --
  }
}

[1] "register" is required
    at new AssertionError (internal/assert.js:269:11)
    at Object.exports.assert (/home/tg/Desktop/Universities-API-/node_modules/hapi/node_modules/hoek/lib/index.js:517:11)
    at Object.exports.apply (/home/tg/Desktop/Universities-API-/node_modules/hapi/lib/config.js:22:10)
    at internals.Server.register (/home/tg/Desktop/Universities-API-/node_modules/hapi/lib/server.js:410:31)
    at init (/home/tg/Desktop/Universities-API-/index.js:21:16)
    at Object.<anonymous> (/home/tg/Desktop/Universities-API-/index.js:80:1)
    at Module._compile (internal/modules/cjs/loader.js:688:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
    at Module.load (internal/modules/cjs/loader.js:598:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
    at Function.Module._load (internal/modules/cjs/loader.js:529:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:741:12)
    at startup (internal/bootstrap/node.js:285:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:739:3)
(node:7014) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)`

when i ran the following code

`  await server.register({
    options: {
      path: "/graphiql",
      graphiqlOptions: {
        endpointURL: "/graphql"
      },
      route: {
        cors: true
      }
    }
  });
  await server.register({
    plugin: graphqlHapi,
    options: {
      path: "/graphql",
      graphqlOptions: {
        schema
      },
      route: {
        cors: true
      }
    }
  });
`

hi, is there a fix for this issue yet ? https://github.com/apollographql/apollo-server/issues/758#issuecomment-443219384

I am getting the same error

[1] "register" is required
    at new AssertionError (internal/errors.js:102:11)
    at Object.exports.assert (/var/www/nodejs-graphql-hapi-mongo/node_modules/hapi/node_modules/hoek/lib/index.js:736:11)
    at Object.exports.apply (/var/www/nodejs-graphql-hapi-mongo/node_modules/hapi/lib/config.js:22:10)
    at internals.Server.register (/var/www/nodejs-graphql-hapi-mongo/node_modules/hapi/lib/server.js:392:31)
    at init (/var/www/nodejs-graphql-hapi-mongo/index.js:44:18)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:189:7)
    at Function.Module.runMain (module.js:696:11)
    at startup (bootstrap_node.js:204:16)
    at bootstrap_node.js:625:3
(node:130353) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 3)
(node:130353) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

I am registering plugins like

await server.register([{
        plugin: graphqlHapi,
        register : graphqlHapi,
        options: {
            path: '/graphql',
            graphqlOptions: {
                schema
            },
            route: {
                cors: true
            }
        }
    }, {
        plugin: graphiqlHapi,
        register : graphiqlHapi,
        options: {
            path: '/graphiql',
            graphiqlOptions: {
                endpointURL: 'graphql'
            },
            route: {
                cors: true
            },
        }
    }])

package.json

{
  "name": "modern_api_hapi",
  "version": "1.0.0",
  "description": "modern node api using hapi and graphql",
  "dependencies": {
    "apollo-server-hapi": "^2.3.1",
    "graphql": "^14.0.2",
    "hapi": "^17.4.0",
    "hapi-swagger": "^9.3.0",
    "inert": "^5.1.2",
    "mongoose": "^5.4.1",
    "nodemon": "^1.18.9",
    "vision": "^5.4.4"
  },
  "scripts": {
    "start": "nodemon index.js",
  }

Fix: change register to plugin.
After messing around and reading docs of Hapi (https://hapijs.com/api) I see that it's just a change of that one fieldname like this:

  await server.register({
    plugin: graphiqlHapi,
    options: {
      path: '/graphiql',
      graphiqlOptions: {
        endpointURL: '/graphql'
      }
    }
  })

IMO it would be nice if the documentation would be updated to be compatible with a single version and it would state which version that is.
(https://www.apollographql.com/docs/apollo-server/graphiql.html#graphiqlHapi)

i changed register to plugin, but still the same issue

Fix: change register to plugin.
After messing around and reading docs of Hapi (https://hapijs.com/api) I see that it's just a change of that one fieldname like this:

  await server.register({
    plugin: graphiqlHapi,
    options: {
      path: '/graphiql',
      graphiqlOptions: {
        endpointURL: '/graphql'
      }
    }
  })

IMO it would be nice if the documentation would be updated to be compatible with a single version and it would state which version that is.
(https://www.apollographql.com/docs/apollo-server/graphiql.html#graphiqlHapi)

i changed register to plugin, but still the same issue

I still do have the same issue. The same error comes up!

Has anyone found a solution for this yet? I've just come across the same issue.

@shehanrg Is the answer "You're using an older code sample with a newer server version"

@MyracleDesign what "worked like a charm"

I'm going to close this issue since the original issue was on the old Apollo Server v1 API, but also because Hapi has changed a bit since then as well. I think it's worth noting that Apollo Server 2.x is utilizing Hapi 17, and the API for Hapi changed quite a bit between Hapi 16 and 17.

I'm not intimately familiar with Hapi, but this issue doesn't seem to be accurately tracking exactly one bug with recent versions of Apollo Server, so it's a bit hard to follow. I'm going to close it, but if anyone is still experiencing a problem, please do open a new issue with a reproduction repository (as requested and described in the template when opening a new issue) and someone should be able to take a look at it.

Thanks!

Was this page helpful?
0 / 5 - 0 ratings