Bird
Raised Fist0
GraphQLquery~20 mins

Basic query syntax in GraphQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
GraphQL Basic Query 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 for a blog:

type Query {
  posts: [Post]
}

type Post {
  id: ID
  title: String
  author: Author
}

type Author {
  id: ID
  name: String
}

And the data:

{
  posts: [
    { id: "1", title: "Hello World", author: { id: "a1", name: "Alice" } },
    { id: "2", title: "GraphQL Basics", author: { id: "a2", name: "Bob" } }
  ]
}

What will be the result of this query?

{
  posts {
    title
    author {
      name
    }
  }
}
GraphQL
{
  posts {
    title
    author {
      name
    }
  }
}
A{"data":{"posts":[{"id":"1","title":"Hello World"},{"id":"2","title":"GraphQL Basics"}]}}
B{"data":{"posts":[{"title":"Hello World"},{"title":"GraphQL Basics"}]}}
C{"data":{"posts":[{"title":"Hello World","author":{"name":"Alice"}},{"title":"GraphQL Basics","author":{"name":"Bob"}}]}}
D{"errors":[{"message":"Field 'author' not found on type 'Post'"}]}
Attempts:
2 left
💡 Hint

Look at the fields requested in the query and match them with the schema.

🧠 Conceptual
intermediate
1:30remaining
Which part of a GraphQL query specifies what data to return?

In GraphQL, what is the name of the section inside the curly braces that tells the server what fields to fetch?

ASelection set
BMutation
CSubscription
DDirective
Attempts:
2 left
💡 Hint

It is the list of fields inside the braces after the query keyword.

📝 Syntax
advanced
2:00remaining
What error does this GraphQL query produce?

Consider this query:

{
  posts(
    title: "Hello World"
  ) {
    id
    title
  }
}

What error will this cause?

GraphQL
{
  posts(
    title: "Hello World"
  ) {
    id
    title
  }
}
AValidation Error: Field 'posts' does not accept arguments
BSyntax Error: Arguments must be inside parentheses after the field name
CRuntime Error: Cannot query field 'title' on type 'Post'
DNo error, query runs successfully
Attempts:
2 left
💡 Hint

Check if the schema allows arguments on the 'posts' field.

❓ optimization
advanced
1:30remaining
Which query is more efficient to fetch only post titles?

You want to get only the titles of all posts. Which query is better for performance?

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

Request only the fields you need to reduce data transfer.

🔧 Debug
expert
2:30remaining
Why does this GraphQL query fail?

Given the schema:

type Query {
  post(id: ID!): Post
}

type Post {
  id: ID
  title: String
}

And this query:

{
  post {
    id
    title
  }
}

Why does it fail?

GraphQL
{
  post {
    id
    title
  }
}
AError: Cannot query field 'title' on type 'Query'
BError: Missing required argument 'id' for field 'post'
CNo error, query runs successfully
DError: Field 'post' does not exist on type 'Query'
Attempts:
2 left
💡 Hint

Check if the 'post' field requires any arguments.

Practice

(1/5)
1. What is the main purpose of a GraphQL query?
easy
A. To request specific data by naming fields inside curly braces
B. To update data on the server
C. To delete data from the database
D. To create new database tables

Solution

  1. Step 1: Understand GraphQL query basics

    A GraphQL query is designed to ask for specific data by naming the fields you want inside curly braces.
  2. Step 2: Differentiate query from other operations

    Updating, deleting, or creating data are done by mutations, not queries.
  3. Final Answer:

    To request specific data by naming fields inside curly braces -> Option A
  4. Quick Check:

    GraphQL query = request specific data [OK]
Hint: Queries ask for data by naming fields in braces [OK]
Common Mistakes:
  • Confusing queries with mutations
  • Thinking queries modify data
  • Assuming queries create or delete data
2. Which of the following is the correct syntax for a simple GraphQL query to get a user's name and email?
easy
A. query { user { name, email } }
B. query user { name, email }
C. { user: name, email }
D. query { user: name, email }

Solution

  1. Step 1: Identify correct GraphQL query structure

    A GraphQL query starts with optional 'query' keyword, then curly braces containing the root field and requested subfields separated by commas or spaces.
  2. Step 2: Check each option's syntax

    'query { user { name, email } }' correctly uses the 'query' keyword, outer braces, nested 'user' braces for fields. 'query user { name, email }' misses opening brace after 'query'; '{ user: name, email }' lacks 'query' and uses invalid colon; 'query { user: name, email }' uses invalid colon after 'user'.
  3. Final Answer:

    query { user { name, email } } -> Option A
  4. Quick Check:

    Correct query syntax = query { user { name, email } } [OK]
Hint: Use curly braces to nest fields correctly [OK]
Common Mistakes:
  • Omitting curly braces around nested fields
  • Using colons incorrectly inside queries
  • Placing fields outside braces
3. Given the GraphQL query:
query { book { title author { name } } }

What data will this query return?
medium
A. Only the book's title
B. Only the author's name
C. The book's title and the author's name
D. The book's title and author as a string

Solution

  1. Step 1: Analyze the query structure

    The query asks for 'book' field with subfields 'title' and 'author'. The 'author' field itself has a subfield 'name'.
  2. Step 2: Understand nested data retrieval

    This means the query returns the book's title and inside author, only the name field.
  3. Final Answer:

    The book's title and the author's name -> Option C
  4. Quick Check:

    Nested fields return nested data = The book's title and the author's name [OK]
Hint: Nested braces mean nested data fields [OK]
Common Mistakes:
  • Thinking only top-level fields are returned
  • Assuming author returns full object, not just name
  • Confusing syntax with JSON output
4. Identify the error in this GraphQL query:
query { user { id name, } }
medium
A. Query keyword 'query' is not allowed
B. Missing parentheses after 'user'
C. Curly braces are missing around 'id' and 'name'
D. Trailing comma after 'name' is invalid

Solution

  1. Step 1: Review field list syntax inside braces

    In GraphQL, fields inside braces are separated by spaces or commas, but trailing commas are not allowed.
  2. Step 2: Check the given query

    The query has a trailing comma after 'name', which causes a syntax error.
  3. Final Answer:

    Trailing comma after 'name' is invalid -> Option D
  4. Quick Check:

    No trailing commas in field lists [OK]
Hint: Avoid trailing commas inside field lists [OK]
Common Mistakes:
  • Adding commas after last field
  • Adding parentheses unnecessarily
  • Removing required curly braces
5. You want to fetch a list of users with their id, name, and each user's posts with post title and comments count. Which GraphQL query correctly expresses this?
hard
A. query { users { id name posts { title commentsCount, } } }
B. query { users { id, name, posts { title, commentsCount } } }
C. query { users { id name posts { title, commentsCount } } }
D. query { users { id name posts { title commentsCount } } }

Solution

  1. Step 1: Understand nested fields and syntax

    The query must request 'users' with subfields 'id', 'name', and nested 'posts' with 'title' and 'commentsCount'. In single-line GraphQL queries, commas are required to separate field names, and trailing commas are invalid.
  2. Step 2: Evaluate syntax correctness

    'query { users { id, name, posts { title, commentsCount } } }' correctly separates all fields with commas at every level without trailing commas. The others have errors: missing commas between some fields combined with trailing comma; missing commas between top-level fields; or missing all commas between fields.
  3. Final Answer:

    query { users { id, name, posts { title, commentsCount } } } -> Option B
  4. Quick Check:

    Correct nesting and commas between fields = query { users { id, name, posts { title, commentsCount } } } [OK]
Hint: Nest fields inside braces without trailing commas [OK]
Common Mistakes:
  • Adding trailing commas inside nested fields
  • Misplacing commas between fields
  • Incorrect nesting of fields
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