8

如何为 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)
    };
  }
});

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

查询 { 颜色 { 名称,键 } }

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

查询 { 颜色 { 名称 } }

当我查询键时,如何创建一个返回字符串数组的模式?

4

1 回答 1

20

我想出了答案。关键是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)
    };
  }
});

使用此查询:

查询 { 颜色 { 名称,键 } }

我得到了想要的结果:

{
  name: "colors",
  keys: ["red", "blue"]
}
于 2016-06-23T20:54:12.837 回答