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, their pagination helpers work with it. Keep your token out of client-side code either way (see Token).
The API supports introspection, which is what powers the API explorer's autocompletion. Code generators such as GraphQL Code Generator use it to create types for your queries.
To learn GraphQL itself, start with graphql.org.
Connections
Every one-to-many relationship is a connection, with edges and nodes, as in Relay's Connection Specification. That way you page through every list the same way.
Pagination
Pagination: first query
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
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
operationNamepicking one:
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
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 saves repeating the same fields.