Query, Mutation, Subscription services
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.
The best part about the new API is that you don't have to create those services, there's a tool that does it for you. To read more about it, go to "Code Generation" section
#
QueryTo 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 exampleLet's see how to actually use it in a component:
#
Example with variables#
API of QueryQuery
class has two methods:
watch(variables?, options?)
- it's the same asApollo.watchQuery
except it accepts variables as a first argument and regular options as the second onefetch(variables?, options?)
- same asApollo.query
, it fetches data once.
#
MutationYou 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 exampleLet's see how to actually use it in a component:
#
API of MutationMutation
class has only one method:
mutate(variables?, options?)
- it's the same asApollo.mutate
except it accepts variables as a first argument and regular options as the second one.
#
SubscriptionYou 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 exampleLet's see how to actually use it in a component:
#
API of SubscriptionSubscription
class has only one method:
subscribe(variables?, options?, extraOptions?)
- it's the same asApollo.subscribe
except its first argument expect variables.
#
Code GenerationThere'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.