Hone logo
Hone
Problems

Building a GraphQL Resolver for a Simple Book Library

GraphQL resolvers are the core of a GraphQL server, responsible for fetching data for each field in a GraphQL schema. This challenge asks you to implement a resolver for a simplified book library, allowing clients to query for books and authors. Understanding resolvers is crucial for building robust and efficient GraphQL APIs.

Problem Description

You are tasked with implementing a GraphQL resolver in Python using the graphql library. The schema defines a Book type with fields id, title, and author. The Author type has fields id and name. The root query field books should return a list of Book objects, and the book query field (taking an id argument) should return a single Book object. The resolver should fetch data from in-memory dictionaries representing the book and author data.

Key Requirements:

  • Implement resolvers for the books and book query fields.
  • The books resolver should return a list of Book objects.
  • The book resolver should accept an id argument and return a single Book object based on that ID.
  • Handle the case where a book with the given ID is not found. In this case, return None.
  • Ensure the resolver correctly links Book objects to their corresponding Author objects.

Expected Behavior:

When a client queries for books, the resolver should return a list of all books. When a client queries for a specific book by ID, the resolver should return the book object with its associated author. If the book ID doesn't exist, the resolver should return None.

Edge Cases to Consider:

  • What happens if the id argument passed to the book resolver is invalid (e.g., not an integer)? While the schema validation should catch this, consider how your resolver handles unexpected input.
  • What happens if the author ID referenced in a Book object doesn't exist in the author data? (Assume this is a data integrity issue and you can safely ignore it for this challenge).

Examples

Example 1:

Input: Query {
  books {
    id
    title
    author {
      id
      name
    }
  }
}
Output: [
  {
    "id": 1,
    "title": "The Lord of the Rings",
    "author": {
      "id": 101,
      "name": "J.R.R. Tolkien"
    }
  },
  {
    "id": 2,
    "title": "Pride and Prejudice",
    "author": {
      "id": 102,
      "name": "Jane Austen"
    }
  }
]

Explanation: The books query returns a list of all books, each with its title and author information.

Example 2:

Input: Query {
  book(id: 1) {
    id
    title
    author {
      id
      name
    }
  }
Output: {
  "id": 1,
  "title": "The Lord of the Rings",
  "author": {
    "id": 101,
    "name": "J.R.R. Tolkien"
  }
}

Explanation: The book query returns the book with ID 1, including its title and author information.

Example 3:

Input: Query {
  book(id: 3) {
    id
    title
    author {
      id
      name
    }
  }
Output: null

Explanation: The book query returns null because there is no book with ID 3.

Constraints

  • The resolver must be implemented in Python.
  • The data is stored in in-memory dictionaries. No database interaction is required.
  • The graphql library must be used.
  • The resolver should be efficient enough to handle a small dataset (up to 100 books and authors). Performance is not a primary concern for this challenge.
  • Input id values for the book query should be integers.

Notes

  • You'll need to define the GraphQL schema (using the graphql library) before implementing the resolvers.
  • Consider how to structure your data (dictionaries) to efficiently retrieve books and authors.
  • The resolve function is the core of the resolver. It takes the parent object, arguments, context, and info as input and returns the data for the field.
  • The context argument can be used to pass additional data to the resolvers, such as database connections or authentication information. For this challenge, you can leave the context empty.
  • The info argument provides information about the query, such as the requested fields. You don't need to use it in this challenge.
  • Focus on correctly implementing the resolvers to fetch and return the data as specified in the schema.
Loading editor...
python