gpt4 book ai didi

schema - GraphQL buildSchema 与 GraphQLObjectType

转载 作者:行者123 更新时间:2023-12-04 03:56:29 24 4
gpt4 key购买 nike

我浏览了 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不使用模式语言,它以编程方式创建模式。
  • 我是否在执行此操作时遇到了任何令人担忧的问题?

  • 我是否应该像使用 buildQuery 版本一样使用具有 GraphQLObjectType 版本的 rootValue 对象?
    不,在 buildSchema 中,根在使用时提供解析器
    GraphQLSchema,根级解析器是在 Query 和 Mutation 类型上实现的,而不是在根对象上实现的。
  • 关于schema - GraphQL buildSchema 与 GraphQLObjectType,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44765316/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com