0
0
GraphQLquery~20 mins

Required fields with non-null (!) in GraphQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
GraphQL Non-Null Mastery
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 with non-null fields?

Given the schema where name is String! (non-null), what will be the result of this query?

{ user(id: 1) { id name } }

Assume the user with id=1 exists but has name set to null in the database.

GraphQL
type User {
  id: ID!
  name: String!
}

query {
  user(id: 1) {
    id
    name
  }
}
AThe query returns an error because <code>name</code> is non-null but the value is null.
BThe query returns the user with <code>name</code> as null.
CThe query returns the user but omits the <code>name</code> field.
DThe query returns an empty object for <code>user</code>.
Attempts:
2 left
💡 Hint

Remember that String! means the field cannot be null in the response.

🧠 Conceptual
intermediate
1:30remaining
Why use non-null (!) fields in GraphQL schemas?

What is the main reason to mark a field as non-null (!) in a GraphQL schema?

ATo allow the field to be omitted from the response if null.
BTo make the field optional in queries.
CTo ensure the client always receives a value and never null for that field.
DTo allow the field to accept null values in mutations.
Attempts:
2 left
💡 Hint

Think about how non-null affects the response data.

📝 Syntax
advanced
2:00remaining
Which schema definition correctly marks a field as non-null list of non-null strings?

Choose the correct GraphQL schema syntax for a field tags that is a non-null list of non-null strings.

Atags: [String!]!
Btags: [String]!
Ctags: [String!]
D!]!gnirtS[ :sgat
Attempts:
2 left
💡 Hint

Remember the order of ! affects list and item nullability.

🔧 Debug
advanced
2:30remaining
Why does this GraphQL query fail with a non-null violation error?

Given this schema:

type Post {
  id: ID!
  title: String!
  content: String
}

And this query:

{ post(id: 5) { id title content } }

The server returns an error about a non-null violation on title. What is the likely cause?

AThe <code>id</code> field is missing in the response.
BThe <code>content</code> field is null but marked non-null in schema.
CThe query syntax is invalid.
DThe <code>title</code> field in the database is null for post with id 5.
Attempts:
2 left
💡 Hint

Check which fields are non-null and their values in the data.

optimization
expert
3:00remaining
How does using non-null fields (!) improve GraphQL query performance?

Which of the following best explains how marking fields as non-null (!) can help optimize GraphQL query execution?

AThe server can skip null checks and avoid unnecessary error handling for those fields.
BThe client can cache responses more aggressively knowing fields are always present.
CThe schema compiler can generate simpler resolvers for non-null fields.
DThe server can batch requests for non-null fields separately for faster resolution.
Attempts:
2 left
💡 Hint

Think about how clients use non-null guarantees.