GraphQL object property must be a list of rows

How to create a schema for a property of an object that is an array of strings in GraphQL? I want the answer to look like this:

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

Here is my diagram

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

When I run this request, I get an error message and no data, the error is just [{}]

query {colors {name, keys}}

However, when I run the request to return only the name, I get a successful response.

query {colors {name}}

How to create a schema that returns an array of strings when I request keys?

+4
source share
1 answer

I understood the answer. The key must pass graphql.GraphQLStringingraphql.GraphQLList()

The scheme becomes:

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

Using this query:

query {colors {name, keys}}

I get the desired results:

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

All Articles