【问题标题】:GraphQL buildSchema vs GraphQLObjectTypeGraphQL buildSchema 与 GraphQLObjectType
【发布时间】:2017-11-29 14:58:39
【问题描述】:

我浏览了 GraphQL 的 Object Types 教程,然后阅读了文档的 Constructing Types 部分。我通过创建一个简单的case convention converter 进行了类似的风格试验。为什么?学习:)

当转换为使用GraphQLObjectType 时,我想要与buildSchema 相同的结果。

  1. 为什么buildSchema 使用type CaseConventions 而使用GraphQLObjectType 时却没有设置为type?我在这里做错了吗?
  2. 我实施此操作时是否存在任何令人担忧的问题?
  3. 我是否应该像使用 buildQuery 版本一样使用带有 GraphQLObjectType 版本的 rootValue 对象?

感谢您的耐心和帮助。


两个版本都使用这个对象:

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; 

buildSchema 版本:

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
}));

【问题讨论】:

    标签: schema graphql resolver object-type


    【解决方案1】:

    我会尽力满意地回答你的问题。

    1. 为什么buildSchema 使用类型 CaseConventions 但在使用 GraphQLObjectType 时未设置类型?我在这里做错了吗

      它们是两种不同的实现方式。使用buildSchema 使用graphQL 模式语言,而GraphQLSchema 不使用模式语言,它以编程方式创建模式。

    2. 我在实施这个过程中是否遇到了任何令人担忧的问题?

      没有

    3. 我是否应该像使用 buildQuery 版本一样使用带有 GraphQLObjectType 版本的 rootValue 对象?

      不,在 buildSchema 中,根在使用时提供解析器 GraphQLSchema,根级解析器是在 Query 和 Mutation 类型上实现的,而不是在根对象上。

    【讨论】:

      猜你喜欢
      • 2017-09-19
      • 2018-02-08
      • 2017-11-08
      • 2017-01-30
      • 2019-03-02
      • 2019-08-19
      • 2017-10-29
      • 2017-09-28
      • 2019-03-13
      相关资源
      最近更新 更多