How to pass object type argument in query in graphql?

by elisha_langworth , in category: Javascript , 4 months ago

How to pass object type argument in query in graphql?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by scotty_walker , 4 months ago

@elisha_langworth 

In GraphQL, you can pass object type arguments by creating input types. Input types in GraphQL are used to represent complex input arguments to fields or mutations. Here's an example of how to pass an object type argument in a query in GraphQL:

  1. Define an input type in your schema:
1
2
3
4
input UserInput {
  name: String!
  age: Int!
}


  1. Use the input type as an argument in your query or mutation:
1
2
3
4
5
6
7
8
type Query {
  getUser(input: UserInput!): User
}

type User {
  name: String!
  age: Int!
}


  1. Send a query with the object type argument:
1
2
3
4
5
6
query {
  getUser(input: { name: "John", age: 30 }) {
    name
    age
  }
}


By defining input types in your schema and using them as arguments in your queries or mutations, you can pass object type arguments in GraphQL. Make sure to define the fields and types of the input type correctly to match the object you want to pass as an argument.