Input
What is it
Data passed to schema
's resolver
/field
.
Argument
values can be hardcoded as a value or passed via GraphQLinput
variable.
# schema definition type Query { resources: [Resource!]! resource(format: DataFormat!): Resource! } # client query query GetResource { resource(format: "jpeg") { id data } }
resources
field returns array ofResource
type data- Corresponding resolver does not take an input
resource
field returns object ofResource
type after taking informat
argumentResolvers
consumes theinput
value and executes function
input
argument
/input
allows user to narrow down the data returned.
Query
fields are anall-or-nothing
return.- We can only get back predefined results.
# schema definition field resource(format: DataFormat!): Resource!
resource
is thefield
nameformat
is theargument
nameDataFormat
would be the argument's data type!
makes itnon-nullable
required argument- will result in
error
when other types are passed
Resource
would be the return data type on the field!
makes itnon-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 theschema
# 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! }