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
shouldSubscribe?: boolean;
}) {
super((observer: Observer>) =>
this.onSubscribe(observer),
);
// active state
this.isTornDown = false;
// query information
this.options = options;
this.variables = options.variables || ({} as TVariables);
this.queryId = queryManager.generateQueryId();
this.shouldSubscribe = shouldSubscribe;
const opDef = getOperationDefinition(options.query);
this.queryName = opDef && opDef.name && opDef.name.value;
// related classes
this.queryManager = queryManager;
}
document,
store = defaultNormalizedCacheFactory(),
variables,
dataIdFromObject,
fragmentMatcherFunction,
}: {
dataId: string;
result: any;
document: DocumentNode;
store?: NormalizedCache;
variables?: Object;
dataIdFromObject?: IdGetter;
fragmentMatcherFunction?: FragmentMatcher;
}): NormalizedCache {
// XXX TODO REFACTOR: this is a temporary workaround until query normalization is made to work with documents.
const operationDefinition = getOperationDefinition(document)!;
try {
return this.writeSelectionSetToStore({
result,
dataId,
selectionSet: operationDefinition.selectionSet,
context: {
store,
processedData: {},
variables: assign(
{},
getDefaultValues(operationDefinition),
variables,
),
dataIdFromObject,
fragmentMap: createFragmentMap(getFragmentDefinitions(document)),
const forLink = removeConnectionDirectiveFromDocument(
cache.transformForLink(transformed));
const clientQuery = this.localState.clientQuery(transformed);
const serverQuery = this.localState.serverQuery(forLink);
const cacheEntry = {
document: transformed,
// TODO These two calls (hasClientExports and shouldForceResolvers)
// could probably be merged into a single traversal.
hasClientExports: hasClientExports(transformed),
hasForcedResolvers: this.localState.shouldForceResolvers(transformed),
clientQuery,
serverQuery,
defaultVars: getDefaultValues(
getOperationDefinition(transformed)
) as OperationVariables,
};
const add = (doc: DocumentNode | null) => {
if (doc && !transformCache.has(doc)) {
transformCache.set(doc, cacheEntry);
}
}
// Add cacheEntry to the transformCache using several different keys,
// since any one of these documents could end up getting passed to the
// transform method again in the future.
add(document);
add(transformed);
add(clientQuery);
add(serverQuery);
}
QueryManager.prototype.startGraphQLSubscription = function (options) {
var _this = this;
var query = options.query;
var cache = this.dataStore.getCache();
var transformedDoc = cache.transformDocument(query);
var variables = assign({}, getDefaultValues(getOperationDefinition(query)), options.variables);
var request = {
query: transformedDoc,
variables: variables,
operationName: getOperationName(transformedDoc) || undefined,
};
var sub;
var observers = [];
return new Observable(function (observer) {
observers.push(observer);
if (observers.length === 1) {
var handler = {
next: function (result) {
_this.dataStore.markSubscriptionResult(result, transformedDoc, variables);
_this.broadcastQueries();
observers.forEach(function (obs) {
if (obs.next)
export default function reduceDocument(document, options = {}) {
const mainDefinition = getMainDefinition(document);
const fragments = getFragmentDefinitions(document);
const operationDefinition = getOperationDefinition(document);
const path = operationDefinition
? operationDefinition.operation
: `type.${mainDefinition.typeCondition.name.value}`;
const execContext = {
rawFragmentMap: createFragmentMap(fragments),
fragmentMap: options.fragmentMap || {},
variables: options.variables,
typeGetter: options.typeGetter || (() => null),
};
return {
kind: 'Document',
definitions: [transformDefinition(mainDefinition, execContext, path)],
};
}
export function writeResultToStore(_a) {
var dataId = _a.dataId, result = _a.result, document = _a.document, _b = _a.storeFactory, storeFactory = _b === void 0 ? defaultNormalizedCacheFactory : _b, _c = _a.store, store = _c === void 0 ? storeFactory() : _c, variables = _a.variables, dataIdFromObject = _a.dataIdFromObject, fragmentMatcherFunction = _a.fragmentMatcherFunction;
var operationDefinition = getOperationDefinition(document);
var selectionSet = operationDefinition.selectionSet;
var fragmentMap = createFragmentMap(getFragmentDefinitions(document));
variables = assign({}, getDefaultValues(operationDefinition), variables);
try {
return writeSelectionSetToStore({
result: result,
dataId: dataId,
selectionSet: selectionSet,
context: {
store: store,
storeFactory: storeFactory,
processedData: {},
variables: variables,
dataIdFromObject: dataIdFromObject,
fragmentMap: fragmentMap,
fragmentMatcherFunction: fragmentMatcherFunction,
return new Observable(observer => {
const { offline: { online } } = this.store.getState();
const { operation: operationType } = getOperationDefinition(operation.query);
const isMutation = operationType === 'mutation';
const isQuery = operationType === 'query';
if (!online && isQuery) {
const data = processOfflineQuery(operation, this.store);
observer.next({ data });
observer.complete();
return () => null;
}
if (isMutation) {
const { AASContext: { doIt = false } = {}, cache } = operation.getContext();
if (!doIt) {