GraphQL 架构不会导入
Posted
技术标签:
【中文标题】GraphQL 架构不会导入【英文标题】:GraphQL schema won't import 【发布时间】:2016-11-05 01:23:34 【问题描述】:我正在尝试设置一个 express 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 ));
...
它有效,返回数据'查看器!但由于我不想要主文件中的所有内容,我尝试将这个确切的代码传输到另一个文件并像这样导入它:
//THIS DOES NOT WORK
...
var Schema = require('./build/models/graphql/schema');
app.use('/graphql', graphqlHTTP( schema: Schema ));
...
我收到以下错误:
"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."
]
我不确定我做错了什么。万一这与它有关,我正在用 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;
这是我的 package.json:
"express": "^4.13.4",
"express-graphql": "^0.5.3",
"graphql": "^0.6.0",
我检查了 node_modules 文件夹中只有一个 graphql。 graphql 是否期望所有模块都具有相同的实例,例如共享的全局实例? express-graphql 是否使用它自己的版本?我该如何检查?我是节点新手,有没有办法检查实例?
【问题讨论】:
【参考方案1】:我认为这不是 GraphQL 问题,而是您在 JS 中如何使用 require 和 export 的问题。问题大概是:
var Schema = require('./build/models/graphql/schema')
随着
var Schema = new graphql.GraphQLSchema( 查询:根查询 );
exports.default = 架构;
您导入的值与导出的值不同。尝试将 Schema 导出为 module.exports = Schema
或将其导入为 Schema = require("./...").default
【讨论】:
你提到的第二个文件是编译器输出的结果,看起来像 Babel。第一个文件也使用 Babel 吗?我有一种预感,您在一个文件中使用 ES6 导入/导出语法,而在另一个文件中直接使用 require()。我建议使用所有一种语法或所有其他语法来避免这种错误 谢谢李!你是对的,我正在用 babel 编译除主文件之外的所有内容,主文件是来自我们生产环境的样板代码,所以我不理会它。一旦我继续并在 es6 中重写它并将其与所有其余部分一起编译下来,一切正常。这些东西对于新手来说很难理解。感谢帮助!!!【参考方案2】:还要确保您的 node_modules 目录中没有安装多个版本的 GraphQL。
如错误所示,这很可能与 node_modules 目录中的多个 GraphQL 副本有关。你检查过吗?如果有多个副本,如果您使用的是 npm 版本 2,您可能可以通过运行 npm dedupe
来解决它。如果您使用的是 npm 3,那么很可能您已经安装了两个不同版本的 @ 987654322@模块。
无论哪种方式,您都必须确保在编译步骤之后,express-graphql 和您的架构都指向 graphql
模块的同一个副本。
【讨论】:
您好,感谢您的回复 - 我有 npm 版本 3.8.6 - 我在 node_modules 文件夹中只有一份 graphql 副本。它是否安装在其他任何地方?也许作为 express-graphql 的依赖项?有没有办法知道实例是从哪里拉出来的?【参考方案3】:如果您想实现和维护 Graphql 服务(主要是它们的模式),请查看 graphqly。这是定义 Graphql 模式的示例代码:
import graphly from "graphqly";
const gBuilder = graphly.createBuilder();
// define types, inputs ... (in any order)
gBuilder.type("Products").implements("List").def(`
products: [Product]!
`);
gBuilder.type("Product").def(`
id: ID!
name: String!
link: String
price: Int
`);
// we're too lazy to define a separate input, so we can `extend` other structure
gBuilder.input("ProductInput").ext("Product");
gBuilder.enum("ProductOrder").def(`
PRICE_DESCENDING
PRICE_ASCENDING
NEWEST
`);
【讨论】:
以上是关于GraphQL 架构不会导入的主要内容,如果未能解决你的问题,请参考以下文章
如何将graphql模式导入graphene-python程序?
我的 Gatsby GraphQL 架构不会使用新的 Sanity 架构进行更新
带有 cacheControl 指令的 Apollo Graphql 导入问题