In GraphQL we distinguish between input- and output-types. We already learned about object types which are the most prominent output-type and let us consume data. Further, we used simple scalars like String
to pass data into a field as an argument. GraphQL defines input object types in order to define complex structures of raw data that can be used as input data.
Input object type definitions differ from object types only in the used keyword and in that their fields can not have arguments.
input BookInput { title: String author: String}
Usage
Input object types can be defined like the following.
public class BookInput{ public string Title { get; set; }
public string Author { get; set; }}
public class Mutation{ public async Task<Book> AddBook(BookInput input) { // Omitted code for brevity }}
Note: If a class is used as an argument to a resolver and it does not end in
Input
, Hot Chocolate (by default) will appendInput
to the type name in the resulting schema.
We can also use a class both as an output- and an input-type.
public class Book{ public string Title { get; set; }
public string Author { get; set; }}
public class Mutation{ public async Task<Book> AddBook(Book input) { // Omitted code for brevity }}
This will produce the following schema.
type Book { title: String author: String}
input BookInput { title: String author: String}
type Mutation { addBook(input: BookInput): Book}
Note: While it is possible, it is not encouraged, as it complicates future extensions of either type.