Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
public startGraphQLSubscription(
options: SubscriptionOptions,
): Observable {
const { query } = options;
let transformedDoc = this.dataStore.getCache().transformDocument(query);
const variables = assign(
{},
getDefaultValues(getOperationDefinition(query)),
options.variables,
);
const request: Request = {
query: transformedDoc,
variables,
operationName: getOperationName(transformedDoc) || undefined,
};
let sub: Subscription;
let observers: Observer[] = [];
return new Observable(observer => {
observers.push(observer);
// If this is the first observer, actually initiate the network subscription
if (observers.length === 1) {
const handler = {
next: (result: FetchResult) => {
this.dataStore.markSubscriptionResult(
result,
transformedDoc,
variables,
queryId,
document,
options,
fetchMoreForQueryId,
}: {
requestId: number;
queryId: string;
document: DocumentNode;
options: WatchQueryOptions;
fetchMoreForQueryId?: string;
}): Promise {
const { variables, context, errorPolicy = 'none' } = options;
const request = {
query: document,
variables,
operationName: getOperationName(document) || undefined,
context: context || {},
};
request.context.forceFetch = !this.queryDeduplication;
// add the cache to the context to links can do things with it
request.context.cache = this.dataStore.getCache();
let resultFromStore: any;
let errorsFromStore: any;
const retPromise = new Promise>((resolve, reject) => {
this.addFetchQueryPromise(requestId, retPromise, resolve, reject);
const subscription = execute(this.deduplicator, request).subscribe({
next: (result: ExecutionResult) => {
// default the lastRequestId to 1
const { lastRequestId } = this.getQuery(queryId);
const queryLabel = (queryId, query) => {
let queryName;
if (query.queryString) {
// Parse the query string, then extract the query name.
queryName = getOperationName(parse(query.queryString));
} else if (query.document) {
// The query string has already been parsed (e.g. using `graphql-tag`)
// so extract the query name from the parsed document.
queryName = getOperationName(query.document);
}
// If we haven't been able to find the query name, make one last
// attempt to parse and pull it from the source of a document
// (if it exsts).
if (!queryName && query.document.loc.source) {
queryName = getOperationName(parse(query.document.loc.source.body));
}
// If the query name can't be extracted, fallback on the query ID as the
// label.
return queryName || queryId;
};
function transformOperation(operation: GraphQLRequest): GraphQLRequest {
const transformedOperation: GraphQLRequest = {
variables: operation.variables || {},
extensions: operation.extensions || {},
operationName: operation.operationName,
query: operation.query,
};
// best guess at an operation name
if (!transformedOperation.operationName) {
transformedOperation.operationName =
typeof transformedOperation.query !== 'string'
? getOperationName(transformedOperation.query)
: '';
}
return transformedOperation as Operation;
}
QueryManager.prototype.fetchRequest = function (_a) {
var _this = this;
var requestId = _a.requestId, queryId = _a.queryId, document = _a.document, options = _a.options, fetchMoreForQueryId = _a.fetchMoreForQueryId;
var variables = options.variables, context = options.context, _b = options.errorPolicy, errorPolicy = _b === void 0 ? 'none' : _b;
var request = {
query: document,
variables: variables,
operationName: getOperationName(document) || undefined,
context: context || {},
};
request.context.forceFetch = !this.queryDeduplication;
request.context.cache = this.dataStore.getCache();
var resultFromStore;
var errorsFromStore;
var retPromise = new Promise(function (resolve, reject) {
_this.addFetchQueryPromise(requestId, retPromise, resolve, reject);
var subscription = execute(_this.deduplicator, request).subscribe({
next: function (result) {
var lastRequestId = _this.getQuery(queryId).lastRequestId;
if (requestId >= (lastRequestId || 1)) {
try {
_this.dataStore.markQueryResult(result, document, variables, fetchMoreForQueryId, errorPolicy === 'ignore');
}
catch (e) {
function transformOperation(operation) {
var transformedOperation = {
variables: operation.variables || {},
extensions: operation.extensions || {},
operationName: operation.operationName,
query: operation.query,
};
if (!transformedOperation.operationName) {
transformedOperation.operationName =
typeof transformedOperation.query !== 'string'
? apollo_utilities_1.getOperationName(transformedOperation.query)
: '';
}
return transformedOperation;
}
exports.transformOperation = transformOperation;
QueryManager.prototype.mutate = function (_a) {
var _this = this;
var mutation = _a.mutation, variables = _a.variables, optimisticResponse = _a.optimisticResponse, updateQueriesByName = _a.updateQueries, _b = _a.refetchQueries, refetchQueries = _b === void 0 ? [] : _b, updateWithProxyFn = _a.update, _c = _a.errorPolicy, errorPolicy = _c === void 0 ? 'none' : _c, _d = _a.context, context = _d === void 0 ? {} : _d;
if (!mutation) {
throw new Error('mutation option is required. You must specify your GraphQL document in the mutation option.');
}
var mutationId = this.generateQueryId();
var cache = this.dataStore.getCache();
(mutation = cache.transformDocument(mutation)),
(variables = assign({}, getDefaultValues(getMutationDefinition(mutation)), variables));
var mutationString = print(mutation);
var request = {
query: mutation,
variables: variables,
operationName: getOperationName(mutation) || undefined,
context: context,
};
this.setQuery(mutationId, function () { return ({ document: mutation }); });
var generateUpdateQueriesInfo = function () {
var ret = {};
if (updateQueriesByName) {
Object.keys(updateQueriesByName).forEach(function (queryName) {
return (_this.queryIdsByName[queryName] || []).forEach(function (queryId) {
ret[queryId] = {
updater: updateQueriesByName[queryName],
query: _this.queryStore.get(queryId),
};
});
});
}
return ret;
function transformOperation(operation) {
var transformedOperation = {
variables: operation.variables || {},
extensions: operation.extensions || {},
operationName: operation.operationName,
query: operation.query,
};
if (!transformedOperation.operationName) {
transformedOperation.operationName =
typeof transformedOperation.query !== 'string'
? getOperationName(transformedOperation.query)
: '';
}
return transformedOperation;
}
function createOperation(starting, operation) {
function executeMutation(client: any, link: ApolloLink = client.link, offlineRecord: OfflineRecordCache): Promise {
const {
request: {
payload: { mutation, variables, context },
},
} = offlineRecord;
const query = client.queryManager.transform(mutation).document;
const operation = {
query,
variables,
operationName: getOperationName(query) || void 0,
context: client.queryManager.prepareContext({
...context,
forceFetch: true,
}),
};
const options: Options = {
observable: multiplex(execute(link, operation)) as any,
};
return observableToPromise(options, (result) => result);
}
const nextQueryResult = tryFunctionOrLogError(() =>
updater(currentQueryResult, {
mutationResult: mutation.result,
queryName: getOperationName(query.document) || undefined,
queryVariables: query.variables,
}),
);