swiftgraphqlpandora

Is there any GraphQL Query to start a station from track?


Is there any way I can start a "Station" using a "trackID" directly? Right now I have to search for Station Factory using a "search" query. Using the first result of search query, I am setting the returned result as a "Source". That's how I am able to play the track as a station. Thanks.


Solution

  • As you say, you can search for the track and get the station factory that way. For example, this query:

    query {
      search(query:"Let Me by Zayn", types:[SF], pagination: {limit: 1}) {
        items {
          ... on StationFactory {
            name
            id
          }
        }
      }
    }
    

    Returns this result:

    {
      "data": {
        "search": {
          "items": [
            {
              "name": "Let Me",
              "id": "SF:21586:35686886"
            }
          ]
        }
      }
    }
    

    By including types:[SF] in the search parameters, results are limited to "station factories." Station factories (see "Station and Station Factory" in the docs) can be used to start station playback (see info on setSource here).

    A similar, but slightly different path, is to search for tracks (note that we are now specifying types:[TR] instead of types:[SF]):

    query {
      search(query:"Let Me by Zayn", types:[TR], pagination: {limit: 1}) {
        items {
          ... on Track {
            name
            artist {
              name
            }
            stationFactory {
              id
            }
          }
        }
      }
    }
    

    Which returns:

    {
      "data": {
        "search": {
          "items": [
            {
              "name": "Let Me",
              "artist": {
                "name": "ZAYN"
              },
              "stationFactory": {
                "id": "SF:21586:35686886"
              }
            }
          ]
        }
      }
    }
    

    There is one caveat/edge case to consider when using types:[TR]. Specifically, some tracks will not have a stationFactory ID. The vast majority of tracks will have stationFactory IDs so this shouldn't be a major limiting factor, but it's important to have a null check here.

    Finally, if you know the track ID already, you can look up all the associated metadata for that track using the handy-dandy entity query. Here we are pulling out the station factory ID:

    query {
      entity(id: "TR:35686886") {
        ... on Track {
          stationFactory {
            id
          }
        }
      }
    }
    

    Which returns:

    {
      "data": {
        "entity": {
          "stationFactory": {
            "id": "SF:21586:35686886"
          }
        }
      }
    }