42
loading...
This website collects cookies to deliver better user experience
This article was published on 2017-03-01 by Kamil Kisiela @ The Guild Blog
0.11.0
of apollo-angular and a lot of things have changed and improved since our last update blog!react-apollo
, but it was already taken. That means that from now on the “angular2-apollo” package is deprecated.import { Angular2Apollo } from 'angular2-apollo'
class AppComponent {
constructor(apollo: Angular2Apollo) {}
}
import { Apollo } from 'apollo-angular'
class AppComponent {
constructor(apollo: Apollo) {}
}
import { ApolloClient } from 'apollo-client'
import { ApolloModule } from 'apollo-angular'
const client = new ApolloClient()
function provideClient() {
return client
}
ApolloModule.withClient(provideClient)
function provideClients() {
return {
default: defaultClient,
extra: extraClient
}
}
ApolloModule.forRoot(provideClients)
Apollo
service has now two new methods: use()
and default()
. First one takes a key of a client you want to use, second one returns the default client.class AppComponent {
apollo: Apollo;
ngOnInit() {
// uses the defaultClient
this.apollo.watchQuery({...}).subscribe(() => {});
// works the same as the one above
this.apollo.default().watchQuery({...}).subscribe(() => {});
// uses the extraClient
this.apollo.use('extra').watchQuery({...}).subscribe(() => {});
}
}
It's important to know that if you want to have a default client, you need to usedefault
as a key.
const query = gql`
query currentUser {
currentUser {
name
}
}
`
interface User {
name: string
}
interface Data {
currentUser: User
}
class AppComponent {
apollo: Apollo
currentUser: User
ngOnInit() {
this.apollo.watchQuery<Data>({ query }).subscribe((result) => {
this.currentUser = result.data.currentUser
})
}
}
class AppComponent {
user: ApolloQueryObservable<ApolloQueryResult<Data>>
getUser() {
this.user.subscribe((result) => {
// result is of type ApolloQueryResult<Data>
})
}
}
class AppComponent {
user: ApolloQueryObservable<Data>
getUser() {
this.user.subscribe((result) => {
// result is of type ApolloQueryResult<Data>
})
}
}
42