src/services/apollo.service.ts
Properties |
Methods |
constructor(apollo: Apollo, httpLink: HttpLink, config: GapiApolloClientOptions)
|
||||||||||||||||
Defined in src/services/apollo.service.ts:17
|
||||||||||||||||
Parameters :
|
createClientWithSubscriptions |
createClientWithSubscriptions()
|
Defined in src/services/apollo.service.ts:68
|
Returns :
void
|
createHttpClient |
createHttpClient()
|
Defined in src/services/apollo.service.ts:51
|
Returns :
void
|
importQuery | ||||||||
importQuery(search: )
|
||||||||
Defined in src/services/apollo.service.ts:37
|
||||||||
Parameters :
Returns :
any
|
init |
init()
|
Defined in src/services/apollo.service.ts:29
|
Returns :
void
|
mutation | ||||||||||||
mutation(options: MutationOptions | string, variables?: )
|
||||||||||||
Defined in src/services/apollo.service.ts:99
|
||||||||||||
Parameters :
Returns :
Observable<literal type>
|
query | ||||||||||||
query(options: WatchQueryOptions | string, variables?: )
|
||||||||||||
Defined in src/services/apollo.service.ts:106
|
||||||||||||
Parameters :
Returns :
Observable<literal type>
|
resetStore |
resetStore()
|
Defined in src/services/apollo.service.ts:130
|
Returns :
Promise<[]>
|
setAuthorizationToken | ||||||||
setAuthorizationToken(token: string)
|
||||||||
Defined in src/services/apollo.service.ts:64
|
||||||||
Parameters :
Returns :
void
|
subscription | ||||||||
subscription(options: SubscriptionOptions | string)
|
||||||||
Defined in src/services/apollo.service.ts:123
|
||||||||
Parameters :
Returns :
Observable<literal type>
|
graphqlDocs |
graphqlDocs:
|
Defined in src/services/apollo.service.ts:16
|
http |
http:
|
Type : HttpLinkHandler
|
Defined in src/services/apollo.service.ts:15
|
webSocketLink |
webSocketLink:
|
Type : WebSocketLink
|
Defined in src/services/apollo.service.ts:17
|
import { Injectable, Inject } from '@angular/core';
import { ApolloModule, Apollo, QueryRef } from 'apollo-angular';
import { HttpLinkModule, HttpLink, HttpLinkHandler } from 'apollo-angular-link-http';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { GapiApolloClientOptions, GAPI_APOLLO_MODULE_CONFIG } from '../config';
import { Observable } from 'rxjs/Observable';
import { WatchQueryOptions, ApolloQueryResult, SubscribeToMoreOptions, SubscriptionOptions, MutationOptions } from 'apollo-client';
import { HttpHeaders } from '@angular/common/http';
import { ApolloLink, concat, split } from 'apollo-link';
import { WebSocketLink } from 'apollo-link-ws';
import { getMainDefinition } from 'apollo-utilities';
@Injectable()
export class GapiApolloService {
http: HttpLinkHandler = this.httpLink.create({ uri: this.config.uri });
graphqlDocs;
webSocketLink: WebSocketLink;
constructor(
private apollo: Apollo,
private httpLink: HttpLink,
@Inject(GAPI_APOLLO_MODULE_CONFIG) private config: GapiApolloClientOptions
) {
if (!this.config.documents) {
throw new Error('You must declare variable DOCUMENTS');
}
this.graphqlDocs = this.config.documents;
}
init() {
if (this.config.subscriptionsUri) {
this.createClientWithSubscriptions();
} else {
this.createHttpClient();
}
}
importQuery(search) {
let result;
Object.keys(this.graphqlDocs)
.filter(doc => {
if (doc.indexOf(search) !== -1) {
result = this.graphqlDocs[doc];
}
});
if (!result) {
throw new Error(`Missing query: ${search}`);
}
return result;
}
createHttpClient() {
this.apollo.create({
link: concat(new ApolloLink((operation, forward) => {
operation.setContext({
headers: new HttpHeaders().set('Authorization', this.config.authorization)
});
return forward(operation);
}), this.http),
cache: new InMemoryCache()
});
}
setAuthorizationToken(token: string) {
this.config.authorization = token;
}
createClientWithSubscriptions() {
this.webSocketLink = new WebSocketLink({
uri: this.config.subscriptionsUri,
options: {
reconnect: true,
connectionParams: {
token: this.config.authorization,
},
}
});
this.apollo.create({
link: concat(new ApolloLink((operation, forward) => {
operation.setContext({
// tslint:disable-next-line:max-line-length
headers: new HttpHeaders().set('Authorization', this.config.authorization)
});
return forward(operation);
}), split(
// split based on operation type
({ query }) => {
const { kind, operation } = <any>getMainDefinition(query);
return kind === 'OperationDefinition' && operation === 'subscription';
},
this.webSocketLink,
this.http,
)),
cache: new InMemoryCache()
});
}
mutation<T>(options: MutationOptions | string, variables?): Observable<{ data: T }> {
if (options.constructor === String) {
options = { mutation: this.importQuery(options), variables };
}
return this.apollo.mutate(<any>options)
}
query<T>(options: WatchQueryOptions | string, variables?): Observable<{ data: T }> {
if (options.constructor === String) {
options = { query: this.importQuery(options), variables };
}
return Observable.create((observer) => {
const subscription = this.apollo.watchQuery(<any>options)
.valueChanges
.subscribe(
(data) => observer.next(data),
(e) => {
observer.error(e);
subscription.unsubscribe();
}
);
});
}
subscription<T>(options: SubscriptionOptions | string): Observable<{ data: T }> {
if (options.constructor === String) {
options = { query: this.importQuery(options) };
}
return this.apollo.subscribe(<any>options);
}
resetStore(): Promise<ApolloQueryResult<any>[]> {
this.webSocketLink['subscriptionClient'].close();
return this.apollo.getClient().resetStore();
}
}