Given the following GraphQL schema snippet:
type Product {
id: ID!
name: String!
price: Float!
inStock: Boolean!
quantity: Int!
}And this query:
{
product {
id
name
price
inStock
quantity
}
}Assuming the product data is: {id: "p123", name: "Pen", price: 1.5, inStock: true, quantity: 100}, what is the expected JSON response?
Remember that ID and String are returned as strings, Float as a number with decimals, Boolean as true/false, and Int as an integer.
The ID type is serialized as a string, so "p123" remains a string. The price is a Float and must include decimals (1.5). The inStock is a Boolean and must be true or false without quotes. The quantity is an Int and must be a number without decimals.
You want to store a unique identifier for each user in your GraphQL schema. Which scalar type should you use?
Think about the type designed specifically for unique identifiers.
The ID scalar type is designed to represent unique identifiers. It is serialized as a string but semantically indicates an identifier. Using ID helps tools and clients understand the field's purpose.
Which option contains a syntax error in defining a GraphQL type with scalar fields?
type Book {
title: String!
pages: Int
price: Float!
available: Boolean!
isbn: ID!
}Check the use of non-nullable markers ! and missing punctuation.
Option A is missing the non-nullable marker ! after available: Boolean which is inconsistent with the others and likely a mistake if the field is required. Also, all fields should be separated by newlines or spaces, but that is not a syntax error here. The key is the missing ! if the field is intended to be non-nullable.
Given this schema:
type Item {
id: ID!
cost: Float!
}And this query:
{
item {
id
cost
}
}The server returns:
{
"data": {
"item": {
"id": "i1",
"cost": null
}
}
}Why is cost null despite being non-nullable?
Consider what happens if a resolver returns null for a non-nullable field.
If a resolver returns null for a field marked as non-nullable (with !), the GraphQL server returns null for the entire parent object or an error depending on the implementation. Here, the cost resolver returned null, causing the field to be null despite the schema.
You have a GraphQL schema with a type UserSettings that has 20 Boolean fields representing different feature toggles. Querying all these fields causes performance issues. What is the best way to optimize the schema design?
Think about how to compact multiple true/false values efficiently.
Using a bitmask stored as an Int is a common optimization to compact many Boolean flags into a single number. This reduces the number of fields queried and can improve performance. Option C adds complexity and parsing overhead. Option C ignores the performance issue. Option C is incorrect because Float is larger and not suitable for Boolean flags.