0
0
GraphQLquery~20 mins

Basic query syntax in GraphQL - Practice Problems & Coding Challenges

Choose your learning style9 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.