Query, Mutation, Subscription Services
Apollo Angular introduces a new approach of working with GraphQL in Angular by providing Query, Mutation and Subscription APIs. This approach makes GraphQL Documents first-class citizens, and the new APIs are easy to use and generate.
If you’re familiar with the library, you already know the Apollo service. It is a regular Angular service, pretty much the only one you need to use.
The API is straightforward, query and watchQuery methods for Queries, mutate and subscribe
accordingly for Mutations and Subscriptions. There is more than that but if you don’t do anything
advanced that’s all you really need.
We decided to introduce a new approach of working with GraphQL in Angular.
There are now 3 new APIs: Query, Mutation and Subscription. Each of them allows to define the
shape of a result & variables. The only thing you need to do is to set the document property. That’s
it, you use it as a regular Angular service.
In this approach GraphQL Documents are first-class citizens, you think about the query, for example, as a main subject.
Query
To get started with the new API, let’s see how you define queries with it.
You create a service and extend it with a Query class from apollo-angular. Only thing you need
to set is a document property.
We have now a ready to use GraphQL Query that takes advantage of Apollo service under the hood.
Basic Example
Let’s see how to actually use it in a component:
Example with Variables
API of Query
Query class has two methods:
watch(variables?, options?)- it’s the same asApollo.watchQueryexcept it accepts variables as a first argument and regular options as the second onefetch(variables?, options?)- same asApollo.query, it fetches data once.
Mutation
You create a service and extend it with a Mutation class from apollo-angular. Only thing you
need to set is a document property.
We have now a ready to use GraphQL Mutation.
Basic Example
Let’s see how to actually use it in a component:
API of Mutation
Mutation class has only one method:
mutate(variables?, options?)- it’s the same asApollo.mutateexcept it accepts variables as a first argument and regular options as the second one.
Subscription
You create a service and extend it with a Subscription class from apollo-angular. Only thing you
need to set is a document property.
We have now a ready to use GraphQL Subscription.
Basic Example
Let’s see how to actually use it in a component:
API of Subscription
Subscription class has only one method:
subscribe(variables?, options?, extraOptions?)- it’s the same asApollo.subscribeexcept its first argument expect variables.
Code Generation
There’s a tool to generate a ready to use in your component, strongly typed Angular services, for every defined query, mutation or subscription.
In short, you define a query in .graphql file so your IDE gives you autocompletion and validation.
Code generation tool outputs to a file, a fully featured service called AllPostsGQL with every
interface you will need.
To learn more about the tool, please read the “Apollo-Angular 1.2 — using GraphQL in your apps just got a whole lot easier!” article or go straight to documentation.