0

Currently, I'm taking geolocation coordinates from my GET url parameters, which actually them into strings. i.e.

For a url like this: www.example.com?geolocation=-23.12345&geolocation=12.12345

it converts it into: ["-23.12345", "12.12345"]

Is it possible to just send it as an array of strings, which GraphQL can then reformat before testing it?

Here's a bit of my GraphQL code:

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
                }
            }
        }
    })
A. L
  • 11,695
  • 23
  • 85
  • 163
  • Yes, you can do that, but no, you really shouldn't. A geolocation is a tuple of exactly two numbers, it should be an object with `longitude` and `latitude` properties, not an array/list of arbitrarily many numbers. – Bergi Dec 25 '19 at 15:49
  • @Bergi that's how Google's location API returns the geolocation I believe. It's a bit of old code I'm relearning. Also, could you point me in the direction of the documentation or even just writing the answer? Thanks. – A. L Dec 25 '19 at 15:55
  • What kind of "documentation" do you expect? – Bergi Dec 25 '19 at 15:59
  • Even if Google's location API does that, in GraphQL it's much better represented using a custom Scalar type (that only permits numeric arrays of length 2). – Bergi Dec 25 '19 at 15:59
  • @Bergi What's the code to force convert a String into Float? Where should I put the conversion? And I think you're right. Having longitude/latitude object is going to be much more clear going forward. I think either Google or PostgreSQL has their geolocation arrays in the wrong order, because they map differently if you put their coordinates in. – A. L Dec 25 '19 at 16:09
  • https://stackoverflow.com/questions/642650/how-to-convert-string-into-float-in-javascript/642674#642674 – Bergi Dec 25 '19 at 16:30
  • @Bergi I mean in GraphQL, before it tries to parse the data – A. L Dec 26 '19 at 00:23
  • If you use a custom scalar, you can use a custom parser and serialiser. Not sure what you mean by "*before GraphQL tries to parse the data*"? You should probably bring it to the right format *before* passing it to GraphQL. – Bergi Dec 26 '19 at 12:08

1 Answers1

1

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.

Christos Lytras
  • 36,310
  • 4
  • 80
  • 113
Markus Madeja
  • 848
  • 7
  • 10