How to pass object type argument in query in GraphQL?
Asked Answered
L

1

5

I got this type of query

query {
    searchRandom (param : MyObjectClass){
        city
    }
}

How may I set param with the type of MyObjectClass and pass it in the query? To be able to test here?

enter image description here

Louisalouisburg answered 18/2, 2022 at 19:20 Comment(0)
V
6

Use the following query.

query getData($param: MyObjectClass){
  searchRandom(param: $param)
      city
}

And then go to query variables tab in Graphiql and pass the variable data like this. You have not mention the data types included in MyObjectClass. So use this as an example:

{
  "param": {"country": "England", "population": "High" }
}

Then the data should be returned as expected.

--- Additionally ---

If you are running the server, make sure you have set the followings.

You need to create a input object in the GraphQL schema.

input MyObjectClass {
     country: String
     population: String
}

Then in the resolver you have to pass the object as the argument. (Assuming you are using JavaScript)

const resolvers = {
  Query: {
    searchRandom: (parent, { param }) => {
        var query_data = param
        ...//your code
        return city_name;
      },
    },

I am not sure whether this addresses your question or not. I hope this answer helps though.

Vermeil answered 20/2, 2022 at 13:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.