@apollo/client/core#DocumentNode TypeScript Examples
The following examples show how to use
@apollo/client/core#DocumentNode.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: api.service.ts From etherspot-sdk with MIT License | 6 votes |
async query<T extends {}>(query: DocumentNode, options?: ApiRequestQueryOptions<T>): Promise<T> {
options = {
variables: {},
fetchPolicy: 'no-cache',
...options,
};
const {
omitChainIdVariable, //
variables,
fetchPolicy,
models,
} = options;
return this.wrapCall(
() =>
this.apolloClient.query<T>({
query,
fetchPolicy,
variables: this.prepareApiVariables(variables, omitChainIdVariable),
}),
models,
);
}
Example #2
Source File: api.service.ts From etherspot-sdk with MIT License | 6 votes |
async mutate<T extends {}>(mutation: DocumentNode, options?: ApiRequestOptions<T>): Promise<T> {
options = {
variables: {},
...options,
};
const {
omitChainIdVariable, //
variables,
models,
} = options;
return this.wrapCall(
() =>
this.apolloClient.mutate<T>({
mutation,
variables: this.prepareApiVariables(variables, omitChainIdVariable),
}),
models,
);
}
Example #3
Source File: api.service.ts From etherspot-sdk with MIT License | 6 votes |
subscribe<T extends {}>(query: DocumentNode, options?: ApiRequestOptions<T>): Observable<T> {
const {
omitChainIdVariable, //
variables,
models,
} = options;
return this.apolloClient
.subscribe<T>({
query,
variables: this.prepareApiVariables(variables, omitChainIdVariable),
})
.map(({ data }) => mapApiResult(data, models));
}
Example #4
Source File: ApolloExtensionsClient.ts From apollo-cache-policies with Apache License 2.0 | 5 votes |
// Watches the data in the cache similarly to watchQuery and additionally extracts the given fieldName from the watch query
// subscription and returns a subscription that emits that field data.
private watchQueryForField(query: DocumentNode, fieldName: string): ObservableQuery {
const obsQuery = this.watchQuery({
fetchPolicy: 'cache-only',
query: query,
});
const subscribe = obsQuery.subscribe.bind(obsQuery);
obsQuery.subscribe = (observer, ...rest) => {
// This check is modeled after the Zen Observable observer check:
// https://github.com/zenparsing/zen-observable/blob/master/src/Observable.js#L211
if (typeof observer !== 'object') {
observer = {
next: observer,
error: rest[0] as (error: any) => void,
complete: rest[1] as () => void,
};
}
const observerNext = observer.next;
// The observer maps the value emitted from the observable to the data at the
// given field name.
observer.next = (value: Record<string, any>) => {
if (observerNext) {
observerNext(value?.data?.[fieldName]);
}
}
const subscription = subscribe(observer);
const unsubscribe = subscription.unsubscribe.bind(subscription);
subscription.unsubscribe = () => {
// @ts-ignore typePolicies is private. Delete the field name from the type policies
// after the subscription has been cleaned up.
delete this.cache.policies.typePolicies.Query.fields[fieldName];
unsubscribe();
}
return subscription;
}
return obsQuery;
}