# Advanced usage


These sections cover GraphQL clients, connections, parameterized queries and fragments.

## GraphQL

Any GraphQL client works with the API. On a server, an HTTP client and a JSON parser are all you need. Client libraries such as Apollo Client and Relay add caching, and since the API follows the [Relay Connection Specification](#connections), their pagination helpers work with it. Keep your token out of client-side code either way (see [Token](/api/basics#token)).

The API supports [introspection](https://graphql.org/learn/introspection/), which is what powers the API explorer's autocompletion. Code generators such as [GraphQL Code Generator](https://the-guild.dev/graphql/codegen) use it to create types for your queries.

To learn GraphQL itself, start with [graphql.org](https://graphql.org/learn/).

## Connections

Every one-to-many relationship is a connection, with edges and nodes, as in [Relay's Connection Specification](https://relay.dev/graphql/connections.htm). That way you page through every list the same way.

### Pagination

> Pagination: first query

```shell
curl -XPOST https://api.soundtrack.io/v2 \
-H 'Authorization: Basic your_token' \
-H 'Content-Type: application/json' \
-d '{
  "query": "query AccountUsers($id: ID!, $after: String) { account(id: $id) { access { users(first: 5, after: $after) { pageInfo { hasNextPage endCursor } edges { roles node { name id } } } } } }",
  "variables": {"id": "account_id"},
  "operationName": "AccountUsers"
}'
```

> Pagination: second query

```shell
curl -XPOST https://api.soundtrack.io/v2 \
-H 'Authorization: Basic your_token' \
-H 'Content-Type: application/json' \
-d '{
  "query": "query AccountUsers($id: ID!, $after: String) { account(id: $id) { access { users(first: 5, after: $after) { pageInfo { hasNextPage endCursor } edges { roles node { name id } } } } } }",
  "variables": {"id": "account_id", "after": "end_cursor_from_previous_response"},
  "operationName": "AccountUsers"
}'
```

Pass the connection arguments, such as how many results you want and after which cursor, then read each node from the connection's edges. An edge holds details about the relationship between the parent and the node, such as a user's roles.

The first query gets the first five users with access to the account. To get the next page, pass the `endCursor` from the previous response's `pageInfo` as the `after` argument.

## Parameterized queries

> A document with two operations, and `operationName` picking one:

```shell
curl -XPOST https://api.soundtrack.io/v2 \
-H 'Authorization: Basic your_token' \
-H 'Content-Type: application/json' \
-d '
{
    "query": "query soundZone($id: ID!) { soundZone(id: $id) { name } } query account($id: ID!) { account(id: $id) { businessName } }",
    "variables": { "id": "sound_zone_id" },
    "operationName": "soundZone"
}
'
```

Every example in this guide is a parameterized query: values travel in `variables` instead of being written into the query text. That guards against injection and lets you reuse the same query with different values.

One document can hold several operations. `operationName` then picks which one runs, as in this example, where only `soundZone` runs.

## Fragments & multiple queries

```shell
curl -XPOST https://api.soundtrack.io/v2 \
-H 'Authorization: Basic your_token' \
-H 'Content-Type: application/json' \
-d '{
  "query": "fragment NowPlayingFields on NowPlaying { startedAt track { title artists { name } album { display { image { size(width: 640, height: 640) } } } } } query TwoZones($zone1: ID!, $zone2: ID!) { nowPlaying1: nowPlaying(soundZone: $zone1) { ...NowPlayingFields } nowPlaying2: nowPlaying(soundZone: $zone2) { ...NowPlayingFields } }",
  "variables": {"zone1": "sound_zone_id_1", "zone2": "sound_zone_id_2"},
  "operationName": "TwoZones"
}'
```

This example asks what's playing in two zones in one request, to save a round trip. That works for any queries that don't depend on each other. Aliases (`nowPlaying1`, `nowPlaying2`) tell the results apart, and a [fragment](https://graphql.org/learn/queries/#fragments) saves repeating the same fields.
