54
loading...
This website collects cookies to deliver better user experience
This article was published on 2018-08-21 by Kamil Kisiela @ The Guild Blog
query
and watchQuery
methods sometimes confused a lot of developers. For people who use Apollo for long time it's obvious but we often get asked about differences between them and many newcomers are surprised.We decided to add a new approach of working with GraphQL in Angular.
import { Injectable } from '@angular/core';
import { Query } from 'apollo-angular';
import gql from 'graphql-tag';
@Injectable({...})
export class FeedGQL extends Query {
document = gql`
query Feed {
posts {
id
title
}
}
`
}
Query
, Mutation
and Subscription
. Each of them allows to define the shape of a result & variables.document
property, That's it, and now you use it as a regular Angular service:import { Component } from '@angular/core';
import { FeedGQL } from './feed-gql.ts';
@Component({ … })
export class FeedComponent {
constructor(feedGQL: FeedGQL) {
feedGQL.watch()
.valueChanges
.subscribe(result => {
// ...
})
}
}
But it also opens up the doors for something wayyyyy cooler!
apollo-angular
in the past 2 years we always keep thinking how can we get all those technologies closer to create something that is more powerful than the sum of its parts..graphql
file with a document that you want to use in a component:query Feed {
posts {
id
title
}
}
import { FeedGQL } from './generated'
GraphQL Code Generator takes query's name, makes it PascalCased and adds GQL suffix to it. An example, “myFeed” becomes “MyFeedGQL”.
HttpClient
uses. Sergey Fetiskin wrote an article about it.Apollo.create
inside of a constructor, you can provide settings on Dependency Injection level. Read the “Using Dependency Injection” chapter in docs.