Advanced GraphQL Queries

Learn about advanced GraphQL querying capabilities and how to provide parameters, use fragments, and more.

Parameters in queries

So far, we’ve sent GraphQL to get information about a particular object—a currently logged-in user. But what if we have multiple objects of the same type on a server? How do we specify the object we want to fetch? For example, how would we get information about a particular GitHub repository?

In GitHub’s API, we can do this using the repository query. Here is how it’s defined in the GraphQL schema:

Press + to interact
type Query
# Query to get a repository
repository(name: String!, owner: String!): Repository
# Query that we used in the previous lesson
viewer: User!
}

The main difference between the repository query and the one that we’ve used in the previous lesson is that to call it, we need to provide values for the name and owner parameters. Both are of the type String!, which is a non-nullable string.

Notice that the return type of the query is defined as Repository without the ! sign, which means that GitHub can return null if,for example, we provide an invalid repository name.

To pass arguments to a query, we need to use the following syntax:

Press + to interact
query {
repository(owner: "apollographql", name:"apollo-server") {
# List fields to get here
...
}
}

If the repository schema definition looks like a function definition, the schema definition looks like a function call.

The only remaining thing is to, as always, specify which fields we want GraphQL API to return. In this case, we’ll request basic information like the number of forks or an owner’s name. Here’s what a full ...