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, variables?: V, fetchPolicy: WatchQueryFetchPolicy = 'cache-and-network') => QueryResult<T, V>;
mutate(mutation: DocumentNode, variables?: V, update?: MutationUpdaterFn<T>) => Observable<T>;
}
Members
query
(query: DocumentNode, variables?: V, fetchPolicy: WatchQueryFetchPolicy = 'cache-and-network') => 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
(mutation: DocumentNode, variables?: V, update?: MutationUpdaterFn<T>) => Observable<T>
Perform a GraphQL mutation.
Example
const result$ = this.dataService.mutate(gql`
mutation MyMutation($input: UpdateEntityInput!) {
updateEntity(input: $input) {
id
name
}
},
{ input: updateEntityInput },
);
QueryResult
This class wraps the Apollo Angular QueryRef object and exposes some getters for convenience.
Signature
class QueryResult<T, V = Record<string, any>> {
constructor(queryRef: QueryRef<T, V>, apollo: Apollo)
completed$ = new Subject();
refetchOnChannelChange() => 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>;
}
Members
constructor
(queryRef: QueryRef<T, V>, apollo: Apollo) => QueryResult
completed$
refetchOnChannelChange
() => 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>