Input
What is it
Data passed to schema's resolver/field.
Argumentvalues can be hardcoded as a value or passed via GraphQLinputvariable.
# schema definition type Query { resources: [Resource!]! resource(format: DataFormat!): Resource! } # client query query GetResource { resource(format: "jpeg") { id data } }
resourcesfield returns array ofResourcetype data- Corresponding resolver does not take an input
resourcefield returns object ofResourcetype after taking informatargumentResolversconsumes theinputvalue and executes function
input
argument/input allows user to narrow down the data returned.
Queryfields are anall-or-nothingreturn.- We can only get back predefined results.
# schema definition field resource(format: DataFormat!): Resource!
resourceis thefieldnameformatis theargumentnameDataFormatwould be the argument's data type!makes itnon-nullablerequired argument- will result in
errorwhen other types are passed
Resourcewould be the return data type on the field!makes itnon-nullablerequired 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 nameon 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! }