0
0
GraphQLquery~20 mins

Default resolvers in GraphQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Default Resolver 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 using default resolvers?

Given this GraphQL schema and data, what will be the result of the query below?

type Query {
  book: Book
}

type Book {
  title: String
  author: Author
}

type Author {
  name: String
}

const data = {
  book: {
    title: "1984",
    author: {
      name: "George Orwell"
    }
  }
};

Query:
{
  book {
    title
    author {
      name
    }
  }
}
GraphQL
const data = {
  book: {
    title: "1984",
    author: {
      name: "George Orwell"
    }
  }
};
A{ "data": { "book": null } }
B{ "data": { "book": { "title": null, "author": { "name": null } } } }
C{ "data": { "book": { "title": "1984", "author": { "name": "George Orwell" } } } }
D{ "errors": [ { "message": "Field 'title' not found" } ] }
Attempts:
2 left
💡 Hint

Default resolvers return the value from the object if the field name matches.

🧠 Conceptual
intermediate
1:30remaining
How do default resolvers behave when a field is missing in the data?

Consider a GraphQL schema with a field author of type Author. The data object for book does not have the author property. What will the default resolver return for author?

AIt throws a runtime error because the field is missing.
BIt returns <code>null</code> for the <code>author</code> field.
CIt returns an empty object <code>{}</code> for <code>author</code>.
DIt returns the string "undefined".
Attempts:
2 left
💡 Hint

Default resolvers return null if the field is not present in the object.

📝 Syntax
advanced
2:00remaining
Which option correctly defines a default resolver function in GraphQL?

Choose the correct JavaScript function that acts as a default resolver for a field.

Afunction defaultResolver(parent, args, context, info) { return parent[info.fieldName]; }
Bfunction defaultResolver(parent, args) { return args[info.fieldName]; }
Cfunction defaultResolver(info) { return parent[info.fieldName]; }
Dfunction defaultResolver(parent, args, context) { return parent.fieldName; }
Attempts:
2 left
💡 Hint

The default resolver uses the info.fieldName to get the field name dynamically.

optimization
advanced
2:00remaining
How can default resolvers impact performance in deeply nested queries?

When using default resolvers on a deeply nested GraphQL query, what is a common performance concern?

ADefault resolvers always cache results automatically, improving performance.
BDefault resolvers prevent any data fetching beyond the first level.
CDefault resolvers batch all nested field requests into a single database call by default.
DDefault resolvers may cause multiple redundant data fetches if not optimized.
Attempts:
2 left
💡 Hint

Think about how default resolvers access nested fields and data fetching.

🔧 Debug
expert
3:00remaining
Why does this GraphQL query return null for a nested field despite data existing?

Given this schema and data, the query returns null for author.name. Identify the cause.

type Query {
  book: Book
}

type Book {
  title: String
  author: Author
}

type Author {
  name: String
}

const data = {
  book: {
    title: "Dune",
    authorName: "Frank Herbert"
  }
};

Query:
{
  book {
    title
    author {
      name
    }
  }
}
AThe data object uses 'authorName' instead of nested 'author' object, so default resolver returns null.
BThe schema is missing a resolver for 'author', causing a runtime error.
CThe query syntax is invalid and causes the server to return null.
DThe 'name' field is not defined in the 'Author' type, so it returns null.
Attempts:
2 left
💡 Hint

Check how the data structure matches the schema fields.