Node.js環境でGraphQL.jsの参照実装をサーバーとして使用しようとしています。私はNode.jsでの経験が限られていることに注意してください。今、私はnode
v4.2.6を使用しています。これは、Ubuntu 16.04用の最新のパッケージです。GraphQLの最小必須ノードバージョン?
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は大きく異なるスクリプトを示しているが、私は別にスキーマを組み立てるために持っている1 。 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';
}
}
}
})
});
あまりにも構文エラーで失敗した:私は、おそらくのNode.jsのv4.2.6が古すぎることを推測してい
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
。あれは正しいですか?その場合、GraphQL.jsとexpress-graphql
を使用するために必要なNode.jsの最小バージョンは何ですか?そして、それは私の問題ではない...何か考えている?そのチュートリアルのための
ああ、ありがとう! 「Google検索によるリサーチ」の弱点の1つは、前提条件の明確な説明のような馬鹿げた小さなものが私の道を横断しないということです。私の防衛では、[GraphQLコードページ](http://graphql.org/code/)、[express-graphql'プロジェクトページ(https://github.com/graphql/express- graphql)、[GraphQL.jsプロジェクトページ](https://github.com/graphql/graphql-js)ではこの前提条件について言及していません。私はまもなくv6をインストールします。再度、感謝します! – CommonsWare
Np!質問に答えた場合は、正しいとマークしてください。私は 'import'に関する他のエラーに対処するための答えを更新しました(Nodeのどのバージョンでも動作しません)。複数のバージョンのNodeを管理するために、nvmのようなものをチェックしたいかもしれません。他のページに関しては、これらのモジュールはおそらくノードv6を必要とせず、チュートリアルの構文および/またはそれらのモジュールの例は、以前のノードバージョンでは書き直すことができます。何がNodeバージョンでネイティブに動作するのか、そして何が蒸散する必要があるのかを知ることを期待して、多くのことが書かれています。 – JMM