Queries

Executing a GraphQL Query with Ferry is as easy as:

  1. Creating an instance of the generated request class for your Query.
  2. Calling Client.request() with your request instance.
  3. Listening to the returned Stream.

Creating a Request

For example, let's say we've saved the following Reviews Query to a file named reviews.graphql:

query Reviews($first: Int, $offset: Int) {
reviews(first: $first, offset: $offset) {
id
stars
commentary
createdAt
}
}

Running the Ferry generator will create a reviews.req.gql.dart file with a Class named GReviewsReq. We can instantiate it like so:

final reviewsReq = GReviewsReq(
(b) => b
..vars.first = 10
..vars.offset = 0,
);
note

Notice how we can chain-assign nested values. Ferry's generated classes are based on built_value which uses the Builder Pattern.

Check out this post for more information on built_value classes and how to use them.

Listening to the Request Stream

Now, all we need to do is listen to the request() stream.

client.request(reviewsReq).listen((response) => print(response));