我浏览了 GraphQL 的 Object Types教程,然后通读 Constructing Types文档的一部分。我通过创建一个简单的 case convention converter
进行了类似的风格试验。 .为什么?学习 :)
转换为使用时 GraphQLObjectType
,我想要与 buildSchema
相同的结果.
buildSchema
使用 type CaseConventions
但是当使用 GraphQLObjectType
它没有设置在 type
?我在这里做错了吗? rootValue
对象与 GraphQLObjectType
版本就像我对 buildQuery
所做的一样版本? 感谢您的耐心和帮助。
两个版本都使用这个对象:
class CaseConventions {
constructor(text) {
this.text = text;
this.lowerCase = String.prototype.toLowerCase;
this.upperCase = String.prototype.toUpperCase;
}
splitTargetInput(caseOption) {
if(caseOption)
return caseOption.call(this.text).split(' ');
return this.text.split(' ');
}
cssCase() {
const wordList = this.splitTargetInput(this.lowerCase);
return wordList.join('-');
}
constCase() {
const wordList = this.splitTargetInput(this.upperCase);
return wordList.join('_');
}
}
module.exports = CaseConventions;
构建架构版本:
const schema = new buildSchema(`
type CaseConventions {
cssCase: String
constCase: String
}
type Query {
convertCase(textToConvert: String!): CaseConventions
}
`);
const root = {
convertCase: ({ textToConvert }) => {
return new CaseConventions(textToConvert);
}
};
app.use('/graphql', GraphQLHTTP({
graphiql: true,
rootValue: root,
schema
}));
GraphQLObjectType 版本:
const QueryType = new GraphQLObjectType({
name: 'Query',
fields: {
cssCase: {
type: GraphQLString,
args: { textToConvert: { type: GraphQLString } },
resolve(parentValue) {
return parentValue.cssCase();
}
},
constCase: {
type: GraphQLString,
args: { textToConvert: { type: GraphQLString } },
resolve(parentValue) {
return parentValue.constCase()
}
}
}
});
const RootQuery = new GraphQLObjectType({
name: 'RootQueryType',
fields: {
convertCase: {
type: QueryType,
args: { textToConvert: { type: GraphQLString } },
resolve(p, { textToConvert }) {
return new CaseConventions(textToConvert);
}
}
}
});
const schema = new GraphQLSchema({
query: RootQuery
});
app.use('/graphql', GraphQLHTTP({
graphiql: true,
schema
}));
最佳答案
我会尽力回答你的问题。
buildSchema
使用类型 CaseConventions 但是当使用 GraphQLObjectType 时,它没有设置为类型?我在这里做错了吗它们是两种不同的实现方式。使用
buildSchema
使用 graphQL 模式语言,而 GraphQLSchema
不使用模式语言,它以编程方式创建模式。不
不,在 buildSchema 中,根在使用时提供解析器
GraphQLSchema,根级解析器是在 Query 和 Mutation 类型上实现的,而不是在根对象上实现的。
关于schema - GraphQL buildSchema 与 GraphQLObjectType,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44765316/