How to pass object type argument in query in graphql?

by elisha_langworth , in category: Javascript , 9 months ago

How to pass object type argument in query in graphql?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by scotty_walker , 9 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.

Related Threads:

How to define an empty object type in a graphql schema?
How to define type for object in graphql schema?
How to return complex object as scalar type in graphql?
How to pass unique id in graphql query?
How to pass javascript variable to graphql query?
How to pass argument from iframe to parent?