UNPKG

@apollo-orbit/angular

Version:

A fully-featured GraphQL client for Angular with modular state management.

229 lines (221 loc) 9.34 kB
import { Apollo as Apollo$1, provideApolloOrbit as provideApolloOrbit$1, ɵAPOLLO_INSTANCE_FACTORY as _APOLLO_INSTANCE_FACTORY } from '@apollo-orbit/angular/core'; export { APOLLO_CLIENT_FACTORY, APOLLO_MULTI_ROOT, ApolloCache, ApolloClient, ApolloError, Cache, InMemoryCache, NetworkStatus, PureMutationOptions, PureQueryOptions, PureSubscriptionOptions, QueryObservable, gql, mapMutation, mapQuery, mapSubscription, provideApolloInstance, withApolloOptions } from '@apollo-orbit/angular/core'; import * as i2 from '@apollo-orbit/core'; import { getActionType, resolveDispatchResults, MutationManager, partition, addStateToClient, addStateToCache, STATE_DEFINITION_SYMBOL, StateDescriptor as StateDescriptor$1 } from '@apollo-orbit/core'; export { identifyFragment } from '@apollo-orbit/core'; import { filter, map, Subject, from, Observable, lastValueFrom } from 'rxjs'; import * as i0 from '@angular/core'; import { Injectable, makeEnvironmentProviders, ENVIRONMENT_INITIALIZER, inject } from '@angular/core'; import { tap } from 'rxjs/operators'; import * as i1 from '@apollo/client/core'; import { ApolloError } from '@apollo/client/core'; function ofActionDispatched(...actions) { const actionMap = createActionMap(actions); return source => source.pipe(filter(ctx => ctx.status === 'dispatched' && actionMap[getActionType(ctx.action)]), map(({ action }) => action)); } function ofActionSuccess(...actions) { const actionMap = createActionMap(actions); return source => source.pipe(filter(ctx => ctx.status === 'success' && actionMap[getActionType(ctx.action)]), map(({ action }) => action)); } function ofActionError(...actions) { const actionMap = createActionMap(actions); return source => source.pipe(filter(ctx => ctx.status === 'error' && actionMap[getActionType(ctx.action)]), map(({ action }) => action)); } function ofActionComplete(...actions) { const actionMap = createActionMap(actions); const statuses = ['success', 'error']; return source => source.pipe(filter((ctx) => statuses.includes(ctx.status) && actionMap[getActionType(ctx.action)])); } function createActionMap(actions) { return actions.reduce((acc, action) => ({ ...acc, [action.type]: true }), {}); } class Apollo extends Apollo$1 { actions; manager; _actions; constructor(client, manager, defaultOptions) { super(client, defaultOptions); this.manager = manager; this._actions = new Subject(); this.actions = this._actions.asObservable(); } mutate(options) { const { manager } = this; return super.mutate(manager.wrapMutationOptions(options)).pipe(tap({ next: result => manager.runEffects(options, result, undefined), error: error => manager.runEffects(options, undefined, error) })); } dispatch(action) { this._actions.next({ action, status: 'dispatched' }); return from(this.manager .dispatch({ cache: this.cache, dispatch: this.dispatch.bind(this) }, action) .then(results => { results.forEach(result => this._actions.next(result)); return resolveDispatchResults(results); })); } static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.1.3", ngImport: i0, type: Apollo, deps: "invalid", target: i0.ɵɵFactoryTarget.Injectable }); static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "18.1.3", ngImport: i0, type: Apollo }); } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.1.3", ngImport: i0, type: Apollo, decorators: [{ type: Injectable }], ctorParameters: () => [{ type: i1.ApolloClient }, { type: i2.MutationManager }, { type: undefined }] }); const apolloErrorFactory = (graphQLErrors) => new ApolloError({ graphQLErrors }); class StateManager { clients = {}; pending = []; /** * Create a mutation manager for an apollo client */ createManager(clientId, client) { if (this.clients[clientId] !== undefined) throw new Error(`Apollo clients with duplicate options.id: '${clientId}'`); const manager = new MutationManager(apolloErrorFactory); this.clients[clientId] = { client, manager }; const [current, pending] = partition(this.pending, state => state.clientId === clientId); this.pending = pending; this.addState(client, manager, ...current); return manager; } onAddStates(states) { for (const state of states) { const pair = this.clients[state.clientId]; if (pair) { const { client, manager } = pair; this.addState(client, manager, state); } else { this.pending = [...this.pending, state]; } } } addState(client, manager, ...states) { const addToClient = addStateToClient(client); const addToCache = addStateToCache(client.cache); states.forEach(state => { addToClient(state); addToCache(state); manager.addState(state); state.onInit?.(client.cache); }); } static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.1.3", ngImport: i0, type: StateManager, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "18.1.3", ngImport: i0, type: StateManager }); } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.1.3", ngImport: i0, type: StateManager, decorators: [{ type: Injectable }] }); function withStates(...states) { return { kind: 'APOLLO_ORBIT_STATES', providers: getStatesProviders(states) }; } function provideApolloOrbit(...features) { return makeEnvironmentProviders([ provideApolloOrbit$1(...features), StateManager, { provide: Apollo, useExisting: Apollo$1 }, // in case Apollo is accidentally imported from core entry point { provide: _APOLLO_INSTANCE_FACTORY, useFactory: apolloInstanceFactory, deps: [StateManager] } ]); } function provideStates(...states) { return makeEnvironmentProviders(getStatesProviders(states)); } function getStatesProviders(states) { return [ { provide: ENVIRONMENT_INITIALIZER, multi: true, useFactory: () => () => addStates(states) } ]; } function apolloInstanceFactory(stateManager) { return (clientId, client, defaultOptions) => { const manager = stateManager.createManager(clientId, client); return new Apollo(client, manager, defaultOptions); }; } function addStates(states) { inject(StateManager).onAddStates(states.map(state => typeof state === 'function' ? state() : state)); } function state(configure, definition) { const descriptor = new StateDescriptor(definition); configure(descriptor); return descriptor[STATE_DEFINITION_SYMBOL]; } class StateDescriptor { descriptor; constructor(definition) { this.descriptor = new StateDescriptor$1(definition); } get [STATE_DEFINITION_SYMBOL]() { return this.descriptor[STATE_DEFINITION_SYMBOL]; } /** * Client name in a multi-client setup */ clientId(clientId) { this.descriptor.clientId(clientId); return this; } typeDefs(typeDefs) { this.descriptor.typeDefs(typeDefs); return this; } typePolicies(typePolicies) { this.descriptor.typePolicies(typePolicies); return this; } possibleTypes(possibleTypes) { this.descriptor.possibleTypes(possibleTypes); return this; } onInit(onInit) { this.descriptor.onInit(onInit); return this; } mutationUpdate(mutation, update) { this.descriptor.mutationUpdate(mutation, update); return this; } refetchQueries(mutation, refetchQueries) { this.descriptor.refetchQueries(mutation, refetchQueries); return this; } optimisticResponse(mutation, optimisticResponse) { this.descriptor.optimisticResponse(mutation, optimisticResponse); return this; } effect(mutation, effect) { this.descriptor.effect(mutation, effect); return this; } resolver(typeField, resolver) { this.descriptor.resolver(typeField, transformResolver(resolver)); return this; } action(actionType, actionFn) { this.descriptor.action(actionType, transformActionFn(actionFn)); return this; } } function transformResolver(fn) { return function (...args) { const result = fn(...args); return result instanceof Observable ? lastValueFrom(result, { defaultValue: void 0 }) : result; }; } function transformActionFn(fn) { return function (action, context) { const result = fn(action, context); return result instanceof Observable ? lastValueFrom(result, { defaultValue: void 0 }) : result; }; } /** * Generated bundle index. Do not edit. */ export { Apollo, StateDescriptor, ofActionComplete, ofActionDispatched, ofActionError, ofActionSuccess, provideApolloOrbit, provideStates, state, withStates }; //# sourceMappingURL=apollo-orbit.angular.mjs.map