首页 文章

GraphQL对象属性应该是字符串列表

提问于
浏览
6

如何为GraphQL中的字符串数组创建对象属性的模式?我希望响应看起来像这样:

{
  name: "colors",
  keys: ["red", "blue"]
}

这是我的架构

var keysType = new graphql.GraphQLObjectType({
  name: 'keys',
  fields: function() {
    key: { type: graphql.GraphQLString }
  }
});

var ColorType = new graphql.GraphQLObjectType({
  name: 'colors',
  fields: function() {
    return {
      name: { type: graphql.GraphQLString },
      keys: { type: new graphql.GraphQLList(keysType)
    };
  }
});

当我运行此查询时,我得到一个错误,没有数据,错误只是 [{}]

查询{colors {name,keys}}

但是,当我运行查询只返回名称时,我得到了成功的响应 .

查询{colors }

如何在查询密钥时创建一个返回字符串数组的模式?

1 回答

  • 11

    我想出了答案 . 关键是将 graphql.GraphQLString 传递给 graphql.GraphQLList()

    架构变为:

    var ColorType = new graphql.GraphQLObjectType({
      name: 'colors',
      fields: function() {
        return {
          name: { type: graphql.GraphQLString },
          keys: { type: new graphql.GraphQLList(graphql.GraphQLString)
        };
      }
    });
    

    使用此查询:

    查询{colors {name,keys}}

    我得到了预期的结果:

    {
      name: "colors",
      keys: ["red", "blue"]
    }
    

相关问题