我使用graphql-import
加载在单独文件中编写的graphql架构。我有超过40个以上的个人架构文件,但令人惊讶的是它并不能识别我拥有的Contact
类型。以下是更多细节。
// contact.graphql
type Contact {
firstName: String
lastName: String
email: String!
phoneNumber: String
}
// seller.graphql
# import Contact from './contact.graphql'
type Seller {
sellerId: String!
name: String
image: String
contact: Contact # Somehow not able to find Contact type
}
注意:contact.graphql和seller.graphql位于同一目录
以下是导入架构的代码。
const { importSchema } = require('graphql-import');
// Load GraphQL schema from files
const typeDefs = importSchema('./typedef/index.graphql');
运行服务器时,出现以下错误。我不知道为什么?任何帮助将不胜感激。
/Users/****/workspace/my-project//node_modules/graphql/utilities/buildASTSchema.js:134
throw new Error('Type "' + typeRef.name.value + '" not found in document.');
^
Error: Type "Contact" not found in document.
at ASTDefinitionBuilder._resolveType (/Users/****/workspace/my-project/node_modules/graphql/utilities/buildASTSchema.js:134:11)
at ASTDefinitionBuilder.buildType (/Users/****/workspace/my-project/node_modules/graphql/utilities/buildASTSchema.js:218:79)
at ASTDefinitionBuilder._buildWrappedType (/Users/****/workspace/my-project/node_modules/graphql/utilities/buildASTSchema.js:227:24)
at ASTDefinitionBuilder.buildField (/Users/****/workspace/my-project/node_modules/graphql/utilities/buildASTSchema.js:248:18)
at /Users/****/workspace/my-project/node_modules/graphql/utilities/buildASTSchema.js:302:21
at /Users/****/workspace/my-project/node_modules/graphql/jsutils/keyValMap.js:28:31
at Array.reduce (<anonymous>)
at keyValMap (/Users/****/workspace/my-project/node_modules/graphql/jsutils/keyValMap.js:27:15)
at ASTDefinitionBuilder._makeFieldDefMap (/Users/****/workspace/my-project/node_modules/graphql/utilities/buildASTSchema.js:299:49)
at fields (/Users/****/workspace/my-project/node_modules/graphql/utilities/buildASTSchema.js:284:23)
答案 0 :(得分:0)
尝试使用# import Contact from 'contact.graphql'
答案 1 :(得分:0)
在浏览了我的大型模式列表后,我意识到两个graphql
文件具有与导致冲突的Seller
(在不同位置)相同的模式名称。
学习是,即使我们可以将graphql
个文件组织到不同的逻辑目录结构中,但我们必须保持唯一type
: - )