schema - GraphQL buildSchema 与 GraphQLObjectType

标签 schema graphql resolver object-type

我浏览了 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/

    相关文章:

    asp.net-core - 没有可用的架构 graphql asp net core

    node.js - 如何通过 GraphQL 将响应头转发给客户端

    javascript - AngularJS:解析 $http 时 $routeProvider 返回响应 obj 而不是我的 obj

    xml - XSD if-else 条件

    reactjs - TypeError:在 Next.js 中使用 urql 客户端时无法读取未定义的属性(读取 'reduceRight' )

    xml - 是否有根据模式验证 XML 的 Perl 模块?

    database - 如何从数据库获取用于Graphql中分页的光标?

    c++ - boost::asio::ip::tcp::resolver::resolve() 永远阻塞

    python - 用 `data_key` 定义的 Marshmallow 字段和用 `attribute` 定义的 Marshmallow 字段有什么区别(标识符相反?)

    mysql - 修复表还是创建并移动到新表?