Bird
Raised Fist0
GraphQLquery~20 mins

Object types in GraphQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
GraphQL Object Types Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
What is the output of this GraphQL query?

Given the following GraphQL schema:

type Book {
  id: ID!
  title: String!
  author: Author!
}

type Author {
  id: ID!
  name: String!
}

And this query:

{
  book(id: "1") {
    title
    author {
      name
    }
  }
}

Assuming the data has a book with id "1" titled "GraphQL Basics" by author "Alice".

What is the expected JSON response?

A{"data": {"book": {"title": "GraphQL Basics", "author": {"name": "Alice"}}}}
B{"data": {"book": {"title": "GraphQL Basics", "author": "Alice"}}}
C{"data": {"book": {"title": "GraphQL Basics"}}}
D{"errors": [{"message": "Field 'author' not found"}]}
Attempts:
2 left
💡 Hint

Remember that nested objects in GraphQL return objects, not just strings.

📝 Syntax
intermediate
1:30remaining
Identify the syntax error in this GraphQL object type definition

Which option contains a syntax error in defining a GraphQL object type?

type User {
  id: ID!
  name: String!
  age: Int
}
A
type User {
  id: ID!
  name: String!
  age Int
}
B
type User {
  id: ID!
  name: String!
  age: Int!
}
C
type User {
  id: ID!
  name: String!
  age: Int
}
D
type User {
  id: ID!
  name: String!
  age: Int
  }
Attempts:
2 left
💡 Hint

Check the colon between field name and type.

❓ optimization
advanced
2:00remaining
Optimize this GraphQL query to reduce data transfer

Given this query fetching user data:

{
  users {
    id
    name
    email
    posts {
      id
      title
      content
    }
  }
}

You only need the user's name and the titles of their posts. Which query is the best optimized version?

A{ users { name posts { title content } } }
B{ users { id name posts { id title content } } }
C{ users { name posts { title } } }
D{ users { name posts { title } email } }
Attempts:
2 left
💡 Hint

Only request the fields you need to reduce data size.

🔧 Debug
advanced
2:00remaining
Why does this GraphQL query return an error?

Given the schema:

type Product {
  id: ID!
  name: String!
  price: Float!
}

And this query:

{
  product(id: "5") {
    id
    name
    cost
  }
}

What is the cause of the error?

AThe query is missing a root query type.
BThe field 'cost' does not exist on type 'Product'.
CThe 'price' field is required but missing in the query.
DThe 'id' argument is missing in the query.
Attempts:
2 left
💡 Hint

Check if all requested fields exist in the schema.

🧠 Conceptual
expert
2:30remaining
How does GraphQL handle nested object types in queries?

Consider a GraphQL schema with nested object types, such as a Post type containing an Author object. When querying nested fields, what does GraphQL do?

AGraphQL flattens all nested objects into a single-level JSON object with dot-separated keys.
BGraphQL ignores nested objects and returns null for those fields unless explicitly joined in the schema.
CGraphQL returns only the IDs of nested objects, requiring separate queries to fetch details.
DGraphQL resolves each nested object field by calling the corresponding resolver and returns nested JSON objects matching the query structure.
Attempts:
2 left
💡 Hint

Think about how GraphQL responses mirror the query shape.

Practice

(1/5)
1. What is the main purpose of an Object type in GraphQL?
easy
A. To execute database queries directly
B. To create user interface components
C. To store data permanently
D. To define the shape and fields of data objects

Solution

  1. Step 1: Understand the role of Object types in GraphQL

    Object types describe what fields an object has and their types, shaping the data structure.
  2. Step 2: Differentiate from other concepts

    Object types do not execute queries, store data, or create UI components; they only define data shapes.
  3. Final Answer:

    To define the shape and fields of data objects -> Option D
  4. Quick Check:

    Object type = data shape definition [OK]
Hint: Object types describe data fields and their types [OK]
Common Mistakes:
  • Confusing object types with query execution
  • Thinking object types store data
  • Mixing object types with UI components
2. Which of the following is the correct syntax to define a GraphQL object type named Book with a title field of type String?
easy
A. object Book { title: String }
B. type Book { title: String }
C. Book type { title: String }
D. define Book { title: String }

Solution

  1. Step 1: Recall GraphQL object type syntax

    GraphQL uses the keyword type followed by the name and fields in braces.
  2. Step 2: Check each option

    Only type Book { title: String } uses the correct syntax: type Book { title: String }. Others use invalid keywords.
  3. Final Answer:

    type Book { title: String } -> Option B
  4. Quick Check:

    Keyword 'type' + name + fields = correct syntax [OK]
Hint: Use 'type' keyword to define object types [OK]
Common Mistakes:
  • Using 'object' or 'define' instead of 'type'
  • Placing type name after 'type' incorrectly
  • Missing braces around fields
3. Given the GraphQL object type definition:
type User {
  id: ID
  name: String
  age: Int
}

What is the expected result of querying:
{ user { id name } }
?
medium
A. Returns user object with only id and name fields
B. Returns user object with id, name, and age fields
C. Returns an error because age is missing
D. Returns null because query is incomplete

Solution

  1. Step 1: Understand GraphQL query field selection

    GraphQL returns only the fields requested in the query, here id and name.
  2. Step 2: Check if missing fields cause errors

    Missing age in query does not cause error; it is simply not returned.
  3. Final Answer:

    Returns user object with only id and name fields -> Option A
  4. Quick Check:

    Query fields = returned fields only [OK]
Hint: GraphQL returns only requested fields in query [OK]
Common Mistakes:
  • Expecting all fields to be returned by default
  • Thinking missing fields cause errors
  • Assuming incomplete queries return null
4. Identify the error in this GraphQL object type definition:
type Product {
  id: ID!
  name: String
  price: Float
  price: Int
}
medium
A. ID type cannot have exclamation mark
B. Missing exclamation mark on 'name' field
C. Duplicate field name 'price' with different types
D. Object type must have at least one field

Solution

  1. Step 1: Review field names and types

    The field price is declared twice with different types (Float and Int), which is invalid.
  2. Step 2: Check other options

    Exclamation mark on ID! is valid for non-null. Fields can be optional. Object type can have multiple fields.
  3. Final Answer:

    Duplicate field name 'price' with different types -> Option C
  4. Quick Check:

    Field names must be unique in object type [OK]
Hint: Field names must be unique in object types [OK]
Common Mistakes:
  • Allowing duplicate field names
  • Misunderstanding non-null syntax
  • Thinking object types need all fields non-null
5. You want to define a GraphQL object type Author that includes a list of Book objects. Which is the correct way to define the books field?
hard
A. books: [Book]
B. books: Book[]
C. books: List<Book>
D. books: {Book}

Solution

  1. Step 1: Recall GraphQL list syntax

    GraphQL uses square brackets around a type to indicate a list, e.g., [Book].
  2. Step 2: Evaluate each option

    books: [Book] uses correct GraphQL syntax. Options A, B, and C use syntax from other languages or invalid formats.
  3. Final Answer:

    books: [Book] -> Option A
  4. Quick Check:

    Lists use square brackets in GraphQL [OK]
Hint: Use square brackets for lists in GraphQL types [OK]
Common Mistakes:
  • Using array syntax from other languages
  • Using curly braces instead of brackets
  • Confusing list syntax with generics
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard