| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- import { HttpClient } from '@angular/common/http';
- import { Injectable } from '@angular/core';
- import { Apollo } from 'apollo-angular';
- import { DataProxy } from 'apollo-cache';
- import { FetchPolicy } from 'apollo-client';
- import { ExecutionResult } from 'apollo-link';
- import { DocumentNode } from 'graphql/language/ast';
- import { Observable } from 'rxjs';
- import { map } from 'rxjs/operators';
- import { CustomFields } from 'shared/shared-types';
- import { API_URL } from '../../app.config';
- import { LocalStorageService } from '../../core/providers/local-storage/local-storage.service';
- import { addCustomFields } from '../add-custom-fields';
- import { QueryResult } from '../query-result';
- import { ServerConfigService } from '../server-config';
- /**
- * Make the MutationUpdaterFn type-safe until this issue is resolved: https://github.com/apollographql/apollo-link/issues/616
- */
- export type TypedFetchResult<T = Record<string, any>> = ExecutionResult & {
- context?: T;
- data: T;
- };
- export type TypedMutationUpdateFn<T> = (proxy: DataProxy, mutationResult: TypedFetchResult<T>) => void;
- @Injectable()
- export class BaseDataService {
- constructor(
- private apollo: Apollo,
- private httpClient: HttpClient,
- private localStorageService: LocalStorageService,
- private serverConfigService: ServerConfigService,
- ) {}
- private get customFields(): CustomFields {
- return this.serverConfigService.serverConfig.customFields || {};
- }
- /**
- * Performs a GraphQL watch query
- */
- query<T, V = Record<string, any>>(
- query: DocumentNode,
- variables?: V,
- fetchPolicy: FetchPolicy = 'cache-and-network',
- ): QueryResult<T, V> {
- const withCustomFields = addCustomFields(query, this.customFields);
- const queryRef = this.apollo.watchQuery<T, V>({
- query: withCustomFields,
- variables,
- fetchPolicy,
- });
- return new QueryResult<T, any>(queryRef);
- }
- /**
- * Performs a GraphQL mutation
- */
- mutate<T, V = Record<string, any>>(
- mutation: DocumentNode,
- variables?: V,
- update?: TypedMutationUpdateFn<T>,
- ): Observable<T> {
- const withCustomFields = addCustomFields(mutation, this.customFields);
- return this.apollo
- .mutate<T, V>({
- mutation: withCustomFields,
- variables,
- update: update as any,
- })
- .pipe(map(result => result.data as T));
- }
- /**
- * Perform REST-like POST
- */
- post(path: string, payload: Record<string, any>): Observable<any> {
- return this.httpClient
- .post(`${API_URL}/${path}`, payload, {
- headers: {
- Authorization: this.getAuthHeader(),
- },
- observe: 'response',
- })
- .pipe(map(response => response.body));
- }
- /**
- * Perform REST-like GET
- */
- get(path: string): Observable<any> {
- return this.httpClient
- .get(`${API_URL}/${path}`, {
- headers: {
- Authorization: this.getAuthHeader(),
- },
- observe: 'response',
- })
- .pipe(map(response => response.body));
- }
- private getAuthHeader(): string {
- const authToken = this.localStorageService.get('authToken');
- return `Bearer ${authToken}`;
- }
- }
|