0
0
GraphQLquery~30 mins

Args argument in GraphQL - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Args Argument in GraphQL Resolvers
📖 Scenario: You are building a simple GraphQL API for a bookstore. The API should allow clients to query books and filter them by genre.
🎯 Goal: Create a GraphQL resolver that uses the args argument to filter books by genre.
📋 What You'll Learn
Create a list of books with title and genre fields
Add a GraphQL query type with a books field that accepts a genre argument
Use the args argument in the resolver to filter books by the given genre
Return all books if no genre argument is provided
💡 Why This Matters
🌍 Real World
Filtering data based on user input is common in APIs to provide customized results.
💼 Career
Understanding how to use arguments in GraphQL resolvers is essential for backend developers working with GraphQL APIs.
Progress0 / 4 steps
1
DATA SETUP: Create a list of books
Create a variable called books that is a list of dictionaries. Each dictionary should have the keys title and genre with these exact entries: {'title': 'The Hobbit', 'genre': 'Fantasy'}, {'title': '1984', 'genre': 'Dystopian'}, {'title': 'To Kill a Mockingbird', 'genre': 'Classic'}.
GraphQL
Need a hint?

Use a list of dictionaries with the exact keys and values given.

2
CONFIGURATION: Define the GraphQL schema with a genre argument
Create a GraphQL type Query with a field books that accepts an optional genre argument of type String and returns a list of Book type. Also define the Book type with title and genre fields of type String.
GraphQL
Need a hint?

Define the schema as a string with the exact types and fields.

3
CORE LOGIC: Write the resolver using the args argument
Create a resolver function called resolve_books that takes parent, args, and context as parameters. Use args.get('genre') to get the genre filter. Return all books if genre is null, otherwise return only books where the genre matches args['genre'].
GraphQL
Need a hint?

Use args.get('genre') to get the argument and filter the list accordingly.

4
COMPLETION: Connect the resolver to the schema
Create a dictionary called resolvers with a key 'Query' mapping to another dictionary with key 'books' and value resolve_books.
GraphQL
Need a hint?

Use a nested dictionary to map the Query field to the resolver function.