GraphQL的最低必需节点版本?

时间:2016-10-19 15:58:38

标签: node.js graphql graphql-js

我试图在Node.js环境中作为服务器获取GraphQL.js的参考实现。请注意,我对Node.js的经验有限。现在,我正在使用node v4.2.6,这是Ubuntu为Ubuntu 16.04提供的最新软件包。

The official documentation说这个脚本应该有效:

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');

失败并出现语法错误:

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显示了一个截然不同的脚本,但是我必须单独组装一个架构。 The GraphQL.js project site有这个用于组装模式的脚本:

import {
  graphql,
  GraphQLSchema,
  GraphQLObjectType,
  GraphQLString
} from 'graphql';

var schema = new GraphQLSchema({
  query: new GraphQLObjectType({
    name: 'RootQueryType',
    fields: {
      hello: {
        type: GraphQLString,
        resolve() {
          return 'world';
        }
      }
    }
  })
});

这也因语法错误而失败:

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

我猜可能是Node.js的v4.2.6太旧了。那是对的吗?如果是这样,使用GraphQL.js和express-graphql所需的Node.js的最低版本是多少?而且,如果这不是我的问题......任何想法是什么?

1 个答案:

答案 0 :(得分:2)

该教程的prerequisites说:

  

在开始之前,您应该安装Node v6 [...]

虽然公平地继续:

  

[...],尽管这些示例应该主要适用于以前版本的Node。

Node v4不支持解构,这是它为你窒息的部分。您可以查看node.green以获取有关哪些Node版本支持哪些功能的参考。

  

这也因语法错误而失败:

任何版本的Node都不支持

import|export。你必须透露这一点。或者您可以使用Node的模块系统。为此,它应该看起来像:

var graphqlModule = require("graphql");
var graphql = graphqlModule.graphql;
var GraphQLSchema = graphqlModule.GraphQLSchema;
// ...
相关问题