Introduction to Querying with Arguments in GraphQL

In our previous lessons, we've discussed setting up a basic GraphQL server using Apollo Server and creating basic types and queries. In this lesson, we will focus on querying with arguments in GraphQL, which allows you to retrieve specific data based on the parameters you provide.

To understand querying with arguments, consider that you might want to fetch details about a specific book from a large collection. Instead of retrieving all books and filtering on the client side, you can pass an argument to your query to get just the book you need directly from the server.

Defining the GraphQL Schema with Arguments

To query specific data, we need to extend our GraphQL schema to include arguments. Recall from previous lessons that the schema defines the types of data and the shape of our queries.

Here’s how we define a schema with arguments:

In this schema:

  • The Book type has three fields: id, title, and author.
  • The Query type contains two fields:
    • books, which returns an array of Book items.
    • book, which takes an id argument of type ID! and returns a single Book.

Note that the ! symbol in ID!, String!, and other types signifies that these fields are non-nullable. This means that a value for these fields must always be provided and cannot be null. For instance, an id of type ID! must always have a value in any Book object.

Creating Resolvers for Queries with Arguments

Resolvers are functions that return data for the fields defined in your schema. When dealing with arguments, resolvers use these arguments to fetch the specific data requested.

Here are the resolvers for our schema:

Resolvers receive two main inputs:

  • parent (_ in the code): Represents the result of the previous resolver in the chain (the "parent" or "root" value) . By using _, we signal that we're aware of this parameter but don't need it for our logic.
  • args: An object containing the arguments passed to the query (e.g., id).

In the above code:

  • The books resolver returns the entire list of books.
  • The book resolver takes an id argument and returns the book that matches the given id.
Running the Apollo Server with Updated Schema and Resolvers

To see our updated schema and resolvers in action, we need to initialize and start the Apollo Server.

Here’s the code to do just that:

When you run this code in your server.ts file, you should see the following output:

This indicates that your server is running and ready to handle queries.

Making GraphQL Queries with Arguments

Now, let's make some GraphQL queries that include arguments.

We can use the following code to query all books and a specific book by ID:

Explanation:

  • We define two GraphQL queries: one to fetch all books and another to fetch a specific book by its ID.
  • We use the fetch function to make HTTP POST requests to our GraphQL server.
  • The server processes the queries and returns the requested data.
Lesson Summary

In this lesson, we expanded our knowledge of GraphQL by learning how to query with arguments. We:

  • Defined a schema that supports arguments in queries.
  • Created resolvers to handle these queries.
  • Initialized and ran an Apollo Server with the updated schema and resolvers.
  • Made and executed GraphQL queries with arguments.

You should now proceed to the practice exercises to solidify your understanding of querying with arguments. In the next lesson, we will tackle more advanced features to further enhance your skills. Keep practicing and refining your knowledge!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal