目前,我正在从 GET url 参数中获取地理位置坐标,这些参数实际上将它们转换为字符串。即
对于这样的网址:
www.example.com?geolocation=-23.12345&geolocation=12.12345
它将其转换为:["-23.12345", "12.12345"]
是否可以将其作为字符串数组发送,然后 GraphQL 可以在测试之前重新格式化?
这是我的一些 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
}
}
}
})
最佳答案
您可以在使用字符串之前将它们转换为 float :
async resolve (
_,
args,
req
)
{
const results =
await findMaps(args.map(parseFloat))
return results
}
您还可以分析它们并在需要时引发异常。
关于javascript - GraphQL 将 args 中的字符串转换为 float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59479816/