Bird
Raised Fist0
GraphQLquery~20 mins

Type definitions in GraphQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
GraphQL Type 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 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?

A{"data":{"book":{"title":"1984","pages":328}}}
B{"data":{"book":{"title":"1984","author":"Orwell"}}}
C{"data":{"book":{"title":"1984"}}}
D{"data":{"book":{"pages":328}}}
Attempts:
2 left
💡 Hint

Only requested fields appear in the result.

🧠 Conceptual
intermediate
1:30remaining
Which GraphQL type definition correctly defines a nullable field?

In GraphQL, how do you define a field that can be null?

Atype User { name: String!! }
Btype User { name: String }
Ctype User { name: String! }
Dtype User { name: !String }
Attempts:
2 left
💡 Hint

Nullable fields do not have an exclamation mark.

📝 Syntax
advanced
2:00remaining
Identify the syntax error in this GraphQL type definition

Which option contains a syntax error in the GraphQL type definition?

type Product {
  id: ID!
  name: String!
  price: Float
  tags: [String!]
}
Atype Product { id: ID! name String! price: Float tags: [String!] }
Btype Product { id: ID! name: String! price: Float tags: [String!]! }
C} !]!gnirtS[ :sgat taolF :ecirp !gnirtS :eman !DI :di { tcudorP epyt
Dype Product { id: ID! name: String! price: Float tags: [String!]! }
Attempts:
2 left
💡 Hint

Check for missing colons, commas, or invalid brackets.

🔧 Debug
advanced
2:30remaining
Why does this GraphQL schema cause an error?

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?

ABecause 'Query' type cannot have arguments.
BBecause 'User' type is missing a required field.
CBecause 'id' field should not be non-nullable.
DBecause the 'friend' field creates a circular reference without a resolver.
Attempts:
2 left
💡 Hint

Think about recursive types and resolvers.

❓ optimization
expert
3:00remaining
How to optimize GraphQL type definitions for large lists?

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?

AKeep the list as is but add a non-null assertion to the list items.
BChange the field to return a single Post instead of a list.
CChange the field to use pagination arguments and return a paginated type instead of a large list.
DRemove the non-null assertion from the list and items to allow nulls.
Attempts:
2 left
💡 Hint

Think about how to handle large data sets efficiently.

Practice

(1/5)
1. What is the main purpose of type definitions in GraphQL?
easy
A. To describe the shape and types of data clients can request
B. To execute database queries directly
C. To style the user interface of a GraphQL client
D. To store data permanently in the server

Solution

  1. Step 1: Understand the role of type definitions

    Type definitions specify what data types exist and how data is structured in GraphQL.
  2. Step 2: Identify what clients use type definitions for

    Clients use type definitions to know what data they can ask for and how it will look.
  3. Final Answer:

    To describe the shape and types of data clients can request -> Option A
  4. Quick Check:

    Type definitions = Describe data shape [OK]
Hint: Type definitions describe data structure, not execution or styling [OK]
Common Mistakes:
  • Confusing type definitions with query execution
  • Thinking type definitions style UI
  • Assuming type definitions store data
2. Which of the following is the correct way to define a simple GraphQL object type named Book with a title field of type String?
easy
A. define Book { title: String }
B. object Book { title: String }
C. Book type { title: String }
D. type Book { title: String }

Solution

  1. Step 1: Recall GraphQL syntax for object types

    GraphQL uses the keyword type followed by the type name and fields in braces.
  2. Step 2: Match the correct syntax

    type Book { title: String } matches the correct syntax: type Book { title: String }.
  3. Final Answer:

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

    Object type syntax = type Name { fields } [OK]
Hint: Remember: 'type' keyword defines object types in GraphQL [OK]
Common Mistakes:
  • Using 'object' instead of 'type'
  • Placing 'type' after the name
  • Using 'define' keyword which is invalid
3. Given the following GraphQL type definition:
type User {
  id: ID!
  name: String
  age: Int
}

Which of these queries will return the name and age fields of a user?
medium
A. { user { id name age } }
B. { user { name age } }
C. { user { id } }
D. { user { age id } }

Solution

  1. Step 1: Identify requested fields in each query

    We want only name and age fields returned.
  2. Step 2: Check which query requests exactly those fields

    { user { name age } } requests name and age only, matching the requirement.
  3. Final Answer:

    { user { name age } } -> Option B
  4. Quick Check:

    Query fields = name and age only [OK]
Hint: Select only needed fields inside braces after type name [OK]
Common Mistakes:
  • Including extra fields like 'id' when not asked
  • Requesting fields in wrong order (order doesn't matter but distracts)
  • Missing required fields if needed
4. Identify the error in this GraphQL type definition:
type Product {
  id: ID
  price: Float!
  name: String!
  description: String
  price: Int
}
medium
A. Missing exclamation mark on 'id' field
B. Incorrect type 'Float!' for price
C. Duplicate field name 'price' with different types
D. Description field cannot be optional

Solution

  1. Step 1: Review all fields for correctness

    Notice that 'price' is declared twice with different types: Float! and Int.
  2. Step 2: Understand GraphQL rules on field names

    Field names must be unique within a type; duplicates cause errors.
  3. Final Answer:

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

    Duplicate fields cause errors [OK]
Hint: Check for repeated field names in type definitions [OK]
Common Mistakes:
  • Ignoring duplicate field names
  • Thinking missing '!' is always an error
  • Assuming optional fields are invalid
5. You want to define a GraphQL type Order that includes an id (non-null ID), a list of items (each an Item type), and a total price (non-null Float). Which is the correct type definition?
hard
A. type Order { id: ID! items: [Item!]! total: Float! }
B. type Order { id: ID items: Item total: Float }
C. type Order { id: ID! items: [Item]! total: Float! }
D. type Order { id: ID! items: [Item!] total: Float }

Solution

  1. Step 1: Analyze each field requirement

    'id' must be non-null ID, 'items' is a non-null list of non-null Item, 'total' is non-null Float.
  2. Step 2: Match the correct syntax

    type Order { id: ID! items: [Item!]! total: Float! } correctly uses ID!, [Item!]! (list non-null and items non-null), and Float!.
  3. Final Answer:

    type Order { id: ID! items: [Item!]! total: Float! } -> Option A
  4. Quick Check:

    Non-null fields and list syntax correct [OK]
Hint: Use [Type!]! for non-null list of non-null items [OK]
Common Mistakes:
  • Missing non-null markers (!)
  • Using list without non-null item type
  • Confusing list syntax with single type
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