Given this GraphQL schema:
type Book {
title: String!
author: String!
pages: Int
}
type Query {
book: Book
}And this query:
{
book {
title
pages
}
}Assuming the resolver returns { title: "1984", author: "Orwell", pages: 328 }, what is the query result?
Only requested fields appear in the result.
The query asks for title and pages only, so the response includes those fields with their values.
In GraphQL, how do you define a field that can be null?
Nullable fields do not have an exclamation mark.
In GraphQL, String means the field can be null. Adding ! means it is non-nullable.
Which option contains a syntax error in the GraphQL type definition?
type Product {
id: ID!
name: String!
price: Float
tags: [String!]
}Check for missing colons, commas, or invalid brackets.
Option A has a syntax error: it is missing the colon (:) after the field name 'name'.
Consider this schema snippet:
type Query {
user(id: ID!): User
}
type User {
id: ID!
name: String!
friend: User
}Why might this schema cause an error in some GraphQL servers?
Think about recursive types and resolvers.
The 'friend' field references the same 'User' type, creating a circular reference. Without proper resolver handling, some servers error out.
You have a GraphQL type with a field returning a large list of items:
type Query {
allPosts: [Post!]!
}What is the best way to optimize this type definition for performance and usability?
Think about how to handle large data sets efficiently.
Returning large lists can cause performance issues. Using pagination with arguments like limit and offset improves performance and usability.