HomeToolsAbout

Input

What is it

Data passed to schema's resolver/field.

  • Argument values can be hardcoded as a value or passed via GraphQL input variable.
# schema definition type Query { resources: [Resource!]! resource(format: DataFormat!): Resource! } # client query query GetResource { resource(format: "jpeg") { id data } }
  • resources field returns array of Resource type data
    • Corresponding resolver does not take an input
  • resource field returns object of Resource type after taking in format argument
    • Resolvers consumes the input value and executes function

input

argument/input allows user to narrow down the data returned.

  • Query fields are an all-or-nothing return.
  • We can only get back predefined results.
# schema definition field resource(format: DataFormat!): Resource!
  • resource is the field name
  • format is the argument name
  • DataFormat would be the argument's data type
    • ! makes it non-nullable required argument
    • will result in error when other types are passed
  • Resource would be the return data type on the field
    • ! makes it non-nullable required return type

input type can be a scalar or an object.

# server/schema.graphql example of object input input SearchListingsInput { checkInDate: String! checkOutDate: String! numOfBeds: Int page: Int limit: Int sortBy: SortByCriteria # enum type } type Query { searchListings(criteria: SearchListingsInput): [Listing]! }

Client using input

The argument on the client query and the schema can be named anything.

  • It does not have to match the field name on the schema
# query query getUserPosts($userId: ID!, $postLimit: Int!) { user(id: $userId) { name posts(limit: $postLimit) { title content } } } # schema # Root query is identical schema { query: Query } # Define the root query type type Query { user(id: ID!): User } # Define the custom types type User { id: ID! name: String! posts(limit: Int!): [Post!]! } type Post { id: ID! title: String! content: String! }
AboutContact