首页 > 解决方案 > graphql 对所有或少数模式字段的单选查询

问题描述

我有一个像下面这样的graphql模式

const SellerjobtypeType = new GraphQLObjectType({
  name: 'Sellerjobtype',
  fields: () => ({
    id: { type: GraphQLString },
    sellerName: { type: GraphQLString },
    sellerAdd: { type: GraphQLString },
    status: { type: GraphQLString },
    requestRaiseTime: { type: GraphQLString },
    percent: { type: GraphQLString },
    runtime: { type: GraphQLString },
    jobid: { type: new GraphQLNonNull(GraphQLString) },
    specialinstructions: { type: GraphQLString }
  })
});

当我编写 graphql 服务器代码以获取 SellerName 的所有 SellerjobtypeType 时,我必须创建一个解析器端点。

sellerjobtype: {
      type: SellerjobtypeType,
      args: { id: { type: GraphQLString}},
      resolve(parent, args) {
        return Sellerjobtype.findById(args.id);
      }
    }

这将仅基于 id 进行搜索

现在我想按卖家名称搜索我创建了另一个端点

sellerjobtype2: {
  type: SellerjobtypeType,
  args: { sellerName: { type: GraphQLString}},
  resolve(parent, args) {
    return myObj.findByName(args.sellerName);
  }
}

如果我必须搜索其他字段,我会创建另一个端点。

这是否违背了不必创建多个端点的 graphql 目的。我想我犯了一个错误。请建议我如何创建自定义端点,该端点可以根据 SellerjobtypeType 中的全部或少数字段处理所有搜索

标签: graphql

解决方案


推荐阅读