我现在已经完全浪费了我的晚上试图解决这个问题但即使经过多次搜索也无法回答。
我的服务器抓取了我的'.graphql'文件夹并将它们合并为一个大文件,我将我的解析器传递给'makeExecutableSchema()',如下所示
const folders = fs.readdirSync(path.join(__dirname, './api'));
const allResolvers = [];
let typeDefs = '';
folders.forEach(folder => {
if (folder !== 'directive') {
typeDefs = typeDefs.concat(requireText(`./api/${folder}/${folder}.graphql`, require));
const { resolver } = require(`./api/${folder}/${folder}.resolvers`);
allResolvers.push(resolver);
}
});
const resolvers = _.merge(allResolvers);
return makeExecutableSchema({typeDefs, resolvers});
现在我不断收到错误
Error: Schema must contain unique named types but contains multiple types named "Address".
at invariant (C:\100hotwater\src\server\node_modules\graphql\jsutils\invariant.js:19:11)
at typeMapReducer (C:\100hotwater\src\server\node_modules\graphql\type\schema.js:216:58)
at Array.reduce (<anonymous>)
at C:\100hotwater\src\server\node_modules\graphql\type\schema.js:237:36
at Array.forEach (<anonymous>)
at typeMapReducer (C:\100hotwater\src\server\node_modules\graphql\type\schema.js:232:51)
at Array.reduce (<anonymous>)
at new GraphQLSchema (C:\100hotwater\src\server\node_modules\graphql\type\schema.js:122:28)
at Object.extendSchema (C:\100hotwater\src\server\node_modules\graphql\utilities\extendSchema.js:161:10)
at buildSchemaFromTypeDefinitions (C:\100hotwater\src\server\node_modules\graphql-tools\dist\schemaGenerator.js:186:28)
at _generateSchema (C:\100hotwater\src\server\node_modules\graphql-tools\dist\schemaGenerator.js:97:18)
at Object.makeExecutableSchema (C:\100hotwater\src\server\node_modules\graphql-tools\dist\schemaGenerator.js:110:20)
at Object.exports.allSchema (C:\100hotwater\src\server\helpers.js:22:28)
at Server.graphQl (C:\100hotwater\src\server\index.js:28:34)
at new Server (C:\100hotwater\src\server\index.js:17:14)
at Object.<anonymous> (C:\100hotwater\src\server\index.js:42:19)
这个模式绝对不包含重复项,但如下所示,在输入“makeExecutableSchema”()“函数之前打印出来。
type Lead {
_id: String
owner: User
address: Address
steps: [Step]
}
type Mutation {
createLead(
owner: String!
address: Address
steps: [Step]
): Lead
}
type Address {
lat: Int
lng: Int
formattedAddress: String
}
type Step {
goto: String
key: String
str: String
}
type User {
_id: String
email: String
firstName: String
lastName: String
mobile: Int
phone: Int
billingAddress: String
password: String
leads: [Lead]
}
type Query {
signedInUser: User
userByEmail(email: String!): User # @isAuthenticated
}
extend type Mutation {
signIn(email: String!, password: String!): SignInResponse!
createUser(
email: String!
firstName: String!
lastName: String
mobile: Int
billingAddress: String
password: String
): User
}
type SignInResponse {
token: String
refreshToken: String
}
我在文件“node_modules \ graphql \ type \ schema.js:216:58”中注意到它似乎循环两次打字并显然在检查中捕获地址两次并发布错误但我仍然不确定从这往哪儿走。在Graphql中这样的简单错误让人很难爱。
感谢任何帮助。
总之,感谢下面的Dan,我正在定义地址类型并将其用于我的输入(Mutation)和我的输出(Query-Lead)。我当时没有意识到的是你需要定义类型&amp;输入不同。下面的工作.graphql架构。
type Lead {
_id: String
owner: User
address: Address
steps: [Step]
}
type Address {
lat: Int
lng: Int
formattedAddress: String
}
type Mutation {
createLead(
owner: String!
address: AddressInput
steps: [Step]
): Lead
}
input AddressInput {
lat: Int
lng: Int
formattedAddress: String
}
答案 0 :(得分:2)
我不确定您的某个解析器模块是什么样的,但我怀疑您打算做这样的事情:
const resolvers = _.merge(...allResolvers) // note the spread operator
否则,调用merge
实际上并没有做任何事情,而resolvers
属性最终成为数组而不是对象。
Address
类型具有相同形状的对象,则必须为其定义单独的输入类型:
type Address {
lat: Int
lng: Int
formattedAddress: String
}
input AddressInput {
lat: Int
lng: Int
formattedAddress: String
}
类型不能用于输入,输入不能用于类型。如果我们以编程方式定义架构,我们通常会看到一个错误。显然,从SDL生成模式不会产生相同的错误。如果为Address和Step添加适当的输入类型,则应该正确生成模式。