【问题标题】:String cannot represent value GraphQL字符串不能表示值 GraphQL
【发布时间】:2020-05-18 08:40:04
【问题描述】:

我正在构建一个 GraphQL API,在我的一个端点中我遇到了这个问题,它说“消息”:“字符串无法表示值:[\”Vitoria\”,\“Serra\”,\“Cariacica \", \"Vila Velha\"]"。

我已尝试更改类型: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, {})
            }
        }
    }
})


【问题讨论】:

    标签: javascript graphql


    【解决方案1】:

    如果一个字段返回一个字符串数组,而不仅仅是一个字符串,那么你应该使用GraphQLList作为here所示的类型:

    type: new GraphQLList(GraphQLString)
    

    或使用 SDL

    [String]
    

    如果数组不包含任何空值,那就更好了

    type: new GraphQLList(new GraphQLNonNull(GraphQLString))
    

    或使用 SDL

    [String!]
    

    如果数组不包含任何空值并且字段本身永远不会为空,那么执行:

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

    或使用 SDL

    [String!]!
    

    【讨论】:

      猜你喜欢
      • 2023-04-05
      • 2021-03-07
      • 2021-03-31
      • 2023-03-13
      • 2010-11-13
      • 2018-02-11
      • 2012-12-24
      • 2018-05-22
      • 2019-09-30
      相关资源
      最近更新 更多