An Input type in GraphQL is a special kind of type used to pass complex objects as arguments to queries or mutations. It defines the shape of the data you can send to the server.
Click to reveal answer
intermediate
Can Input types contain fields that are other object types?
No, Input types can only contain fields of scalar types, enums, or other input types. They cannot contain regular object types because those are for output only.
Click to reveal answer
beginner
Why do we use Input types instead of regular object types for arguments?
Input types are designed for input data validation and safety. They ensure the data sent to the server matches expected shapes and prevent misuse of output-only types.
Click to reveal answer
beginner
How do you define an Input type in GraphQL SDL?
You define an Input type using the input keyword followed by the name and fields. For example:
input UserInput {
name: String!
age: Int
}
Click to reveal answer
intermediate
Can Input types have default values for their fields?
Yes, when using Input types in arguments, you can provide default values for fields to make some inputs optional or have fallback values.
Click to reveal answer
Which keyword is used to define an Input type in GraphQL?
Ainput
Btype
Cinterface
Denum
✗ Incorrect
The input keyword is used to define Input types in GraphQL.
Can Input types contain fields of regular object types?
AYes, always
BOnly in mutations
COnly if marked nullable
DNo, never
✗ Incorrect
Input types cannot contain regular object types; they can only contain scalars, enums, or other input types.
What is the main purpose of Input types in GraphQL?
ATo define the shape of data returned from the server
BTo define the shape of data sent to the server
CTo create database tables
DTo style the UI
✗ Incorrect
Input types define the shape of data sent to the server as arguments.
Which of these is a valid field type inside an Input type?
AQuery
BObjectType
CString
DSubscription
✗ Incorrect
Input types can contain scalar types like String, but not object types like ObjectType.
Can you provide default values for fields in Input types when used as arguments?
AYes
BNo
COnly for scalar fields
DOnly for enum fields
✗ Incorrect
You can provide default values for any fields in Input types when used as arguments.
Explain what Input types are in GraphQL and why they are important.
Think about how you send data to the server and how GraphQL expects it.
You got /4 concepts.
Describe how to define and use an Input type in a GraphQL mutation.
Imagine you want to send a new user’s info to the server.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of input types in GraphQL?
easy
A. To define the shape of data sent into queries or mutations
B. To specify the output format of queries
C. To create database tables automatically
D. To style the API responses
Solution
Step 1: Understand the role of input types
Input types define how data should be structured when sent to the server in queries or mutations.
Step 2: Differentiate input from output types
Output types define what data is returned, while input types organize incoming data.
Final Answer:
To define the shape of data sent into queries or mutations -> Option A
Quick Check:
Input types = organize incoming data [OK]
Hint: Input types shape incoming data, not output [OK]
Common Mistakes:
Confusing input types with output types
Thinking input types create database tables
Assuming input types affect response styling
2. Which of the following is the correct syntax to define an input type in GraphQL?
easy
A. input UserInput = { name: String!, age: Int }
B. type UserInput { name: String! age: Int }
C. input UserInput(name: String!, age: Int)
D. input UserInput { name: String! age: Int }
Solution
Step 1: Recall input type declaration syntax
Input types use the keyword input followed by the name and curly braces for fields.
Step 2: Check each option's syntax
input UserInput { name: String! age: Int } correctly uses input UserInput { ... }. type UserInput { name: String! age: Int } uses type which is for output types. Options C and D have invalid syntax.
Final Answer:
input UserInput { name: String! age: Int } -> Option D
Quick Check:
Input types start with 'input' keyword [OK]
Hint: Input types start with 'input' keyword and braces [OK]
A. mutation { addProduct(input: { name: "Pen", price: "1.5" }) { id name } }
B. mutation { addProduct(input: { id: 123, name: "Pen" }) { id name } }
C. mutation { addProduct(input: { id: "123", name: "Pen", price: 1.5 }) { id name } }
D. mutation { addProduct(input: { id: "123", price: 1.5 }) { id name } }
Solution
Step 1: Check required fields and types
Fields id and name are required (non-null). price is optional. Types must match: id is ID (string), name is String, price is Float.
Step 2: Validate each option
mutation { addProduct(input: { id: "123", name: "Pen", price: 1.5 }) { id name } } provides all required fields with correct types. mutation { addProduct(input: { id: 123, name: "Pen" }) { id name } } uses number 123 for ID (should be string). mutation { addProduct(input: { name: "Pen", price: "1.5" }) { id name } } misses required id and uses string for price (should be Float). mutation { addProduct(input: { id: "123", price: 1.5 }) { id name } } misses required name.
Final Answer:
mutation { addProduct(input: { id: "123", name: "Pen", price: 1.5 }) { id name } } -> Option C
Quick Check:
Required fields present with correct types [OK]
Hint: Required fields must be present with correct types [OK]
Common Mistakes:
Omitting required fields
Using wrong data types (e.g., number instead of string for ID)
Passing strings for Float fields
4. Identify the error in this input type definition:
Input types list fields with their types separated by spaces or new lines; commas are not required.
Step 2: Check each option against syntax
Input types cannot have non-null fields is false; non-null fields are allowed. Field names must be lowercase is false; field names can be camelCase or lowercase. Missing commas between fields is false; commas are optional. Syntax is correct; no error is true; the syntax is valid.
Final Answer:
Syntax is correct; no error -> Option A
Quick Check:
Input fields separated by spaces, commas optional [OK]
Hint: Commas are optional between fields in input types [OK]
Common Mistakes:
Thinking commas are mandatory
Believing non-null fields are disallowed
Assuming field names must be lowercase only
5. You want to create an input type for updating a user's profile where all fields are optional except id. Which input type definition is correct?
hard
A. input UpdateUserInput { id: ID! name: String! email: String age: Int }
B. input UpdateUserInput { id: ID! name: String email: String age: Int }
D. input UpdateUserInput { id: ID name: String! email: String! age: Int! }
Solution
Step 1: Understand requirement for optional fields
Only id must be required (non-null). Other fields should be optional, so no exclamation marks.
Step 2: Evaluate each option
input UpdateUserInput { id: ID! name: String email: String age: Int } has id: ID! required and others optional. input UpdateUserInput { id: ID name: String! email: String! age: Int! } makes id optional and others required, wrong. input UpdateUserInput { id: ID! name: String! email: String! age: Int! } makes all fields required, wrong. input UpdateUserInput { id: ID! name: String! email: String age: Int } makes name required, which is not desired.
Final Answer:
input UpdateUserInput { id: ID! name: String email: String age: Int } -> Option B
Quick Check:
Only id required, others optional [OK]
Hint: Only add '!' to required fields in input types [OK]
Common Mistakes:
Making all fields required
Forgetting to make id required
Adding '!' to optional fields
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.