apollo-server: Error registering graphiqlHapi to Hapi 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:
apollo-server-hapi@1.3.2
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)
apollo-server-hapi@1.2.0
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)
│ │ │ └── graphql-extensions@0.0.5 deduped
│ │ │ └── graphql-extensions@0.0.5 deduped
│ │ └─┬ graphql-extensions@0.0.5
├─┬ graphql@0.11.7
├─┬ graphql-tools@2.18.0
│ ├─┬ graphql-subscriptions@0.5.6
└─┬ hapi@17.2.0
It seems to me like there is some version mismatching going on, but there is no warnings when npm installing.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 16 (1 by maintainers)
Hi,
https://github.com/ignivalancy/hano-graphql
please find GraphQL, Hapi and Node Project with full configuration.
i changed register to plugin, but still the same issue
@shehanrg Thanks works now like a charm. Can this issue then be closed?
Apollo Server 2 includes GraphQL Playground instead of GraphiQL.
Related to this -> https://github.com/apollographql/apollo-server/issues/856#issuecomment-424969345
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!
@shehanrg Is the answer “You’re using an older code sample with a newer server version”
@MyracleDesign what “worked like a charm”
I still do have the same issue. The same error comes up!
Yes, I think this issue can be closed!
Fix: change
register
toplugin
. 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: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)