graphqlgraphql-jsgraphql-javaexpress-graphql

How to pass object type argument in query in GraphQL?


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


Solution

  • 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.