1

I'm building a GraphQL API and in one of my endpoints I have this problem, it says that "message": "String cannot represent value: [\"Vitoria\", \"Serra\", \"Cariacica\", \"Vila Velha\"]".

I have tried to Change for type: GraphQLList.


const alfa_regions = [
    {ES_capital: ['Vitoria', 'Serra', 'Cariacica', 'Vila Velha']},
    {ES_Interior: ['Piuma', 'Sao Gabriel']},
    {MG_Capital: ['Contagem']},
    {MG_Interior: ['Juiz de fora', 'Governador Valadares', 'Teofilo Otoni', 'Monte Claros']},
]


const {
    GraphQLObjectType,
    GraphQLSchema,
    GraphQLString,
    GraphQLFloat,
    GraphQLList
} = graphql

const AlfaRegionType = new GraphQLObjectType({
    name: 'AlfaRegion',
    fields: () => ({
        id: {type: GraphQLString},
        region_id: {type: GraphQLString},
        ES_capital: {type: GraphQLString},
    })
})

const RootQuery = new GraphQLObjectType({
    name: 'RootQueryType',
    fields: {
        regions: {
            type: AlfaRegionType,
            args: {id: {type: GraphQLString}},
            resolve(parent, args){
                return _.find(alfa_regions, {})
            }
        }
    }
})


1 Answer 1

2

If a field returns an array of strings, not just a single string, then you should use GraphQLList for the type as shown here:

type: new GraphQLList(GraphQLString)

or using SDL

[String]

If the array won't contain any nulls, then it'd be even better to do

type: new GraphQLList(new GraphQLNonNull(GraphQLString))

or using SDL

[String!]

If the array won't contain any nulls and the field itself won't ever be null, then do:

type: new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(GraphQLString)))

or using SDL

[String!]!
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.