I am trying to get a reference implementation of GraphQL.js working in a Node.js environment as a server. Note that I have limited experience with Node.js. Right now, I am using node
v4.2.6, which is the latest package from Ubuntu for Ubuntu 16.04.
The official documentation says that this script should work:
var express = require('express');
var graphqlHTTP = require('express-graphql');
var { buildSchema } = require('graphql');
// Construct a schema, using GraphQL schema language
var schema = buildSchema(`
type Query {
hello: String
}
`);
// The root provides a resolver function for each API endpoint
var root = {
hello: () => {
return 'Hello world!';
},
};
var app = express();
app.use('/graphql', graphqlHTTP({
schema: schema,
rootValue: root,
graphiql: true,
}));
app.listen(4000);
console.log('Running a GraphQL API server at localhost:4000/graphql');
That fails with a syntax error:
server.js:3
var { buildSchema } = require('graphql');
^
SyntaxError: Unexpected token {
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:374:25)
at Object.Module._extensions..js (module.js:417:10)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
at Function.Module.runMain (module.js:442:10)
at startup (node.js:136:18)
at node.js:966:3
The express-graphql project site shows a significantly different script, but one where I have to assemble a schema separately. The GraphQL.js project site has this script for assembling a schema:
import {
graphql,
GraphQLSchema,
GraphQLObjectType,
GraphQLString
} from 'graphql';
var schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'RootQueryType',
fields: {
hello: {
type: GraphQLString,
resolve() {
return 'world';
}
}
}
})
});
That too fails with a syntax error:
server.js:1
(function (exports, require, module, __filename, __dirname) { import {
^^^^^^
SyntaxError: Unexpected reserved word
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:374:25)
at Object.Module._extensions..js (module.js:417:10)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
at Function.Module.runMain (module.js:442:10)
at startup (node.js:136:18)
at node.js:966:3
I am guessing that perhaps v4.2.6 of Node.js is too old. Is that correct? If so, what is the minimum version of Node.js required to use GraphQL.js and express-graphql
? And, if that's not my problem... any idea what is?
tThe prerequisites for that tutorial say:
Before getting started, you should have Node v6 installed [...]
Though to be fair it continues with:
[...] , although the examples should mostly work in previous versions of Node as well.
Node v4 doesn't support destructuring, which is the part it's choking on for you. You can check out node.green for a reference on what features are supported by what Node versions.
That too fails with a syntax error:
import|export
aren't supported in any version of Node. You'd have to transpile that. Or you can just use Node's module system. For that it should look something like:
var graphqlModule = require("graphql");
var graphql = graphqlModule.graphql;
var GraphQLSchema = graphqlModule.GraphQLSchema;
// ...