Nikolai Henriksen
Nikolai Henriksen

Reputation: 93

How can I get and use the properties I need from this GraphQL API using Dart?

Before you start reading: I have looked at the GraphQL documentation, but my usecase is so specific and I only need the data once, and therefore I allow myself to ask the community for help on this one to save some time and frustration (not planning to learn GraphQL in the future)

Intro

I am a CS student developing an app for Flutter on the side, where I need information about the name and location of every bus stop in a specific county in Norway. Luckily, there's an open GraphQL API for this (API URL: https://api.entur.io/stop-places/v1/graphql). The thing is, I don't know how to query a GraphQL API, and I do not want to spend time learning it as I am only going to fetch the data once and be done with it.

Here's the IDE for the API: https://api.entur.io/stop-places/v1/ide

And this is the exact query I want to perform as I want to fetch bus stops located in the county of Trondheim:

{
  stopPlace(stopPlaceType: onstreetBus, countyReference: "Trondheim") {
    name {
      value
    }
    ... on StopPlace {
      quays {
        geometry {
          coordinates
        }
      }
    }
  }
}

The problem with this query though, is that I don't get any data when passing "Trondheim" to the countyReference (without countyReference I get the data, but not for Trondheim). I've tried using the official municipal number for the county as well without any luck, and the documentation of the API is rather poor... Maybe this is something I'll have to contact the people responsible for the API to figure out, which shouldn't be a problem.

But now back to the real problem - how can I make this query using the GraphQL package for Dart? Here's the package I'm planning to use: (https://pub.dev/packages/graphql)

I want to create a bus stop object for each bus stop, and I want to put them all in a list. Here is my bus stop model:

class BusStop with ChangeNotifier {
  final String id;
  final String name;
  final LatLng location;

  BusStop({
    this.id,
    this.name,
    this.location
  });
}

When it comes to authentication, here's what the documentation says:

This API is open under NLOD licence, however, it is required that all consumers identify themselves by using the header ET-Client-Name. Entur will deploy strict rate-limiting policies on API-consumers who do not identify with a header and reserves the right to block unidentified consumers. The structure of ET-Client-Name should be: "company - application" Header examples: "brakar - journeyplanner" "fosen_utvikling - departureboard" "norway_bussekspress - nwy-app"

Link to API documentation: https://developer.entur.org/pages-nsr-nsr

Would be great to know how I should go about this as well! I'm grateful for every answers to this, I know I am being lazy here as of learning GraphQL, but for my usecase I thought it would take less time and frustration by asking here!

Upvotes: 0

Views: 625

Answers (1)

Herku
Herku

Reputation: 7666

Getting the query right

First of all you seem to have GraphQL quite figured out. There isn't really much more to it than what you are doing. What queries an API supports depends on the API. The problem you seem to have is more related to the specific API that you are using. I might have figured the right query out for you and if not I will quickly explain what I did and maybe you can improve the query yourself:

{
  stopPlace(stopPlaceType: onstreetBus, municipalityReference: "KVE:TopographicPlace:5001") {
    name {
      value
    }
    ... on StopPlace {
      quays {
        geometry {
          coordinates
        }
      }
    }
  }
}

So to get to this I started finding out more about "Trondheim" bei using the topographicPlace query.

{
  topographicPlace(query: "Trondheim") {
    id
    name {
      value
    }
    topographicPlaceType
    parentTopographicPlace {
      id
      name {
        value
      }
    }
  }
}

If you do that you will see that "Trondheim" is not a county according to the API: "topographicPlaceType": "municipality". I have no idea what municipality is but the is a different filter for this type on the query that you provided. Then putting "Trondheim" there didn't yield any results so I tried the ID of Trondheim. This now gives me a bunch of results.

About the GraphQL client that you are using:

This seems to be an "Apollo Client" clone in Dart. Apollo Client is a heavy piece of software that comes with a lot of awesome features when used in a frontend application. You probably just want to make a single GraphQL request from a backend. I would recommend using a simple HTTP client to send a POST request to the GraphQL API and a JSON body (don't forget content type header) with the following properties: query containing the query string from above and variables a JSON object mapping variable names to values (only needed if you decide to add variables to your query.

Upvotes: 1

Related Questions