我目前正在学习GraphQL,我偶然发现了这个错误。如何在仍然使用GraphQLEnumType对象的情况下修复它。
const { ApolloServer, gql } = require('apollo-server');
const { GraphQLEnumType } = require('graphql');
const Bonus = new GraphQLEnumType({
name: 'Bonus',
values: {
BIG: {
value: "Big",
},
SMALL: {
value: "Small",
}
},
});
const typeDefs = gql`
enum Bonus {
BIG
SMALL
}
`;
const resolvers = {
Bonus : Bonus
}
const server = new ApolloServer({
typeDefs,
resolvers
});
server.listen().then(({ url }) => {
console.log(` Server ready at ${url}`);
});
以下是错误:
解析程序中定义了新的枚举( /home/jonas/Projects/javascript-questions-flow4b/backend/node_modules/graphql-tools/dist/generate/addResolveFunctionsToSchema.js:53 + ".“+ fieldName +”“),但枚举不在架构中”);^
错误:在解析程序中定义了Bonus.name,但架构中不存在枚举
发布于 2019-05-15 03:41:19
如果您使用typeDefs
和resolvers
配置ApolloServer,则不能使用resolvers
。相反,如果您希望为枚举值提供自定义值,请将适当的对象作为resolvers
的一部分传递,如the docs中所示。
const resolvers: {
Bonus: {
BIG: 'Big',
SMALL: 'Small',
},
}
请注意,只有当您希望在内部将枚举值映射到它们的名称以外的其他值时,才需要这样做。默认情况下,BIG
将映射到"BIG"
,SMALL
将映射到"SMALL"
,所以如果这就是您需要的全部内容,那么就不要在解析器中包含Bonus
。
发布于 2020-04-11 00:55:51
如果您正在使用typeDefs和解析器配置ApolloServer,那么您实际上可以使用GraphQLEnumType。
在typeDefs对象中将BonusType定义为标量:
const BonusType = new GraphQLEnumType({
name: 'Bonus',
values: {
BIG: {
value: "Big",
},
SMALL: {
value: "Small",
}
},
});
const typeDefs = gql`
scalar BonusType
`;
现在,每当为BonusType对象添加查询时,您将得到如下结果: 1. BonusType枚举的名称。2. BonusType枚举的值。
https://stackoverflow.com/questions/56137351
复制相似问题