我正在尝试设置一个快速的 GraphQL 服务器。当我将以下内容放入服务器启动时,按照教程进行操作:
// ENTIRE SCHEMA IN MAIN FILE THIS WORKS!!!
...
var graphql = require('graphql');
const RootQuery = new graphql.GraphQLObjectType({
name: 'RootQuery',
description: 'The root query',
fields: {
viewer: {
type: graphql.GraphQLString,
resolve() {
return 'viewer!';
}
}
}
});
const Schema = new graphql.GraphQLSchema({
query: RootQuery
});
app.use('/graphql', graphqlHTTP({ schema: Schema }));
...
Run Code Online (Sandbox Code Playgroud)
它有效,返回数据“查看器!但是由于我不想要主文件中的所有内容,我尝试将此确切代码传输到另一个文件并像这样导入:
//THIS DOES NOT WORK
...
var Schema = require('./build/models/graphql/schema');
app.use('/graphql', graphqlHTTP({ schema: Schema }));
...
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
{
"errors": [
{
"message": "Schema must be an instance of GraphQLSchema. Also ensure that there are not multiple versions of GraphQL installed in your node_modules directory."
}
]
}
Run Code Online (Sandbox Code Playgroud)
我不确定我做错了什么。如果这与它有任何关系,我在 es6 中编写,然后在构建脚本中转换回 5。这是架构文件的构建:
// TRANSPILED SCHEMA
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var graphql = require('graphql');
var RootQuery = new graphql.GraphQLObjectType({
name: 'RootQuery',
description: 'The root query',
fields: {
viewer: {
type: graphql.GraphQLString,
resolve: function resolve() {
return 'viewer!';
}
}
}
});
var Schema = new graphql.GraphQLSchema({
query: RootQuery
});
exports.default = Schema;
Run Code Online (Sandbox Code Playgroud)
这是我的 package.json:
"express": "^4.13.4",
"express-graphql": "^0.5.3",
"graphql": "^0.6.0",
Run Code Online (Sandbox Code Playgroud)
我已经检查过 node_modules 文件夹中只有一个 graphql。graphql 是否希望所有模块都具有相同的 INSTANCE,例如共享全局实例?express-graphql 是否使用它自己的版本?我如何检查?我是节点的新手,有没有办法检查实例?
我不认为这是 GraphQL 问题,而是如何在 JS 中使用 require 和导出的问题。问题大概是:
var Schema = require('./build/models/graphql/schema')
随着
var Schema = new graphql.GraphQLSchema({ 查询: RootQuery });
出口.默认=架构;
您导入的值与导出的值不同。尝试将架构导出为module.exports = Schema或将其导入为Schema = require("./...").default
| 归档时间: |
|
| 查看次数: |
2599 次 |
| 最近记录: |