简体   繁体   English

GraphQL 将字符串转换为 args 中的浮点数

[英]GraphQL convert Strings to Floats in args

Currently, I'm taking geolocation coordinates from my GET url parameters, which actually them into strings.目前,我正在从我的 GET url 参数中获取地理位置坐标,这些参数实际上是将它们转换为字符串。 ie IE

For a url like this: www.example.com?geolocation=-23.12345&geolocation=12.12345对于这样的网址: www.example.com?geolocation=-23.12345&geolocation=12.12345

it converts it into: ["-23.12345", "12.12345"]它将其转换为: ["-23.12345", "12.12345"]

Is it possible to just send it as an array of strings, which GraphQL can then reformat before testing it?是否可以将其作为字符串数组发送,然后 GraphQL 可以在测试之前对其进行重新格式化?

Here's a bit of my GraphQL code:这是我的一些 GraphQL 代码:

const geolocation =
    graphql.GraphQLNonNull(
        graphql.GraphQLList(
            graphql.GraphQLNonNull(
                graphql.GraphQLFloat
            )
        )
    )

var query =
    new graphql.GraphQLObjectType({
        name: "Query",
        fields:
        {
            findMaps:
            {
                args:
                {
                    geolocation:
                    {
                        type:
                            geolocation,
                    },
                },
                type:
                    convertToManyType(
                        map,
                        "map"
                    ),
                async resolve (
                    _,
                    args,
                    req
                )
                {
                    const results =
                        await findMaps(args)

                    return results
                }
            }
        }
    })

You can convert your Strings to Floats right before using them:您可以在使用之前将字符串转换为浮点数:

async resolve (
    _,
    args,
    req
)
{
    const results =
        await findMaps(args.map(parseFloat))

    return results
}

You could also analyse them and throw an exception if needed.如果需要,您还可以分析它们并抛出异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM