Skip to main content

DataService

DataService

Used to interact with the Admin API via GraphQL queries. Internally this service uses the Apollo Client, which means it maintains a normalized entity cache. For this reason, it is advisable to always select the id field of any entity, which will allow the returned data to be effectively cached.

Signature
class DataService {
query(query: DocumentNode | TypedDocumentNode<T, V>, variables?: V, fetchPolicy: WatchQueryFetchPolicy = 'cache-and-network', options: ExtendedQueryOptions = {}) => QueryResult<T, V>;
mutate(mutation: DocumentNode | TypedDocumentNode<T, V>, variables?: V, update?: MutationUpdaterFn<T>, options: ExtendedQueryOptions = {}) => Observable<T>;
}

query

method
(query: DocumentNode | TypedDocumentNode<T, V>, variables?: V, fetchPolicy: WatchQueryFetchPolicy = 'cache-and-network', options: ExtendedQueryOptions = {}) => QueryResult<T, V>

Perform a GraphQL query. Returns a QueryResult which allows further control over they type of result returned, e.g. stream of values, single value etc.

Example

const result$ = this.dataService.query(gql`
query MyQuery($id: ID!) {
product(id: $id) {
id
name
slug
}
},
{ id: 123 },
).mapSingle(data => data.product);

mutate

method
(mutation: DocumentNode | TypedDocumentNode<T, V>, variables?: V, update?: MutationUpdaterFn<T>, options: ExtendedQueryOptions = {}) => Observable<T>

Perform a GraphQL mutation.

Example

const result$ = this.dataService.mutate(gql`
mutation MyMutation($Codegen.UpdateEntityInput!) {
updateEntity(input: $input) {
id
name
}
},
{ Codegen.updateEntityInput },
);

QueryResult

This class wraps the Apollo Angular QueryRef object and exposes some getters for convenience.

Signature
class QueryResult<T, V extends Record<string, any> = Record<string, any>> {
constructor(queryRef: QueryRef<T, V>, apollo: Apollo, customFieldMap: Map<string, CustomFieldConfig[]>)
refetchOnChannelChange() => QueryResult<T, V>;
refetchOnCustomFieldsChange(customFieldsToInclude$: Observable<string[]>) => QueryResult<T, V>;
single$: Observable<T>
stream$: Observable<T>
ref: QueryRef<T, V>
mapSingle(mapFn: (item: T) => R) => Observable<R>;
mapStream(mapFn: (item: T) => R) => Observable<R>;
destroy() => ;
}

constructor

method
(queryRef: QueryRef<T, V>, apollo: Apollo, customFieldMap: Map<string, CustomFieldConfig[]>) => QueryResult

refetchOnChannelChange

method
() => QueryResult<T, V>

Re-fetch this query whenever the active Channel changes.

refetchOnCustomFieldsChange

method
v3.0.4
(customFieldsToInclude$: Observable<string[]>) => QueryResult<T, V>

Re-fetch this query whenever the custom fields change, updating the query to include the specified custom fields.

single$

property
Observable<T>

Returns an Observable which emits a single result and then completes.

stream$

property
Observable<T>

Returns an Observable which emits until unsubscribed.

ref

property
QueryRef<T, V>

mapSingle

method
(mapFn: (item: T) => R) => Observable<R>

Returns a single-result Observable after applying the map function.

mapStream

method
(mapFn: (item: T) => R) => Observable<R>

Returns a multiple-result Observable after applying the map function.

destroy

method
() =>

Signals to the internal Observable subscriptions that they should complete.