Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/flat-hounds-share.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@n1ru4l/in-memory-live-query-store": patch
---

Do not change the context inside the live query executor, and use WeakMap to map the actual context to the live query context
Original file line number Diff line number Diff line change
Expand Up @@ -964,3 +964,33 @@ it("index via custom compound index", async () => {
isLive: true,
});
});

it("keeps the context as-is", async () => {
const schema = createTestSchema();

const store = new InMemoryLiveQueryStore();
const defaultExecuteFnSpy = jest.fn(defaultExecuteImplementation);
const execute = store.makeExecute(defaultExecuteFnSpy);

const document = parse(/* GraphQL */ `
query @live {
foo
}
`);

const contextValue = {
foo: "bar",
};

const executionResult = execute({ document, schema, contextValue });
assertAsyncIterable(executionResult);
let result = await executionResult.next();
expect(result.value).toEqual({
data: {
foo: "queried",
},
isLive: true,
});

expect(defaultExecuteFnSpy.mock.calls[0][0].contextValue).toBe(contextValue);
});
43 changes: 26 additions & 17 deletions packages/in-memory-live-query-store/src/InMemoryLiveQueryStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import {
getOperationAST,
defaultFieldResolver,
TypeInfo,
DocumentNode,
visit,
Kind,
} from "graphql";
import { mapSchema, MapperKind, isAsyncIterable } from "@graphql-tools/utils";
import { Repeater } from "@repeaterjs/repeater";
Expand Down Expand Up @@ -34,21 +37,20 @@ type AddResourceIdentifierFunction = (
values: string | Iterable<string> | None
) => void;

const originalContextSymbol = Symbol("originalContext");

type ArgumentName = string;
type ArgumentValue = string;
type IndexConfiguration = Array<
ArgumentName | [arg: ArgumentName, value: ArgumentValue]
>;

type LiveQueryContextValue = {
[originalContextSymbol]: unknown;
collectResourceIdentifier: ResourceIdentifierCollectorFunction;
addResourceIdentifier: AddResourceIdentifierFunction;
indices: Map<string, Array<IndexConfiguration>> | null;
};

const liveQueryContextMap = new WeakMap<any, LiveQueryContextValue>();

const addResourceIdentifierCollectorToSchema = (
schema: GraphQLSchema,
idFieldName: string
Expand All @@ -61,24 +63,24 @@ const addResourceIdentifierCollectorToSchema = (
fieldName === idFieldName && isNonNullIDScalarType(fieldConfig.type);
let resolve = fieldConfig.resolve ?? defaultFieldResolver;

newFieldConfig.resolve = (src, args, context, info) => {
if (!context || originalContextSymbol in context === false) {
return resolve(src, args, context, info);
newFieldConfig.resolve = (root, args, context, info) => {
if (context == null) {
return resolve(root, args, context, info);
}

const liveQueryContext = liveQueryContextMap.get(context);
if (liveQueryContext == null) {
return resolve(root, args, context, info);
}

const liveQueyContext = context as LiveQueryContextValue;
const result = resolve(
src,
args,
liveQueyContext[originalContextSymbol],
info
) as any;
const result = resolve(root, args, context, info) as any;

const fieldConfigExtensions = fieldConfig.extensions as any | undefined;
if (fieldConfigExtensions?.liveQuery?.collectResourceIdentifiers) {
liveQueyContext.addResourceIdentifier(
fieldConfigExtensions.liveQuery.collectResourceIdentifiers(
src,
root,
args
)
);
Expand Down Expand Up @@ -236,12 +238,13 @@ export class InMemoryLiveQueryStore {
schema: inputSchema,
document,
rootValue,
contextValue,
variableValues,
operationName,
...additionalArguments
} = args;

let contextValue = args.contextValue;

const operationNode = getOperationAST(document, operationName);

const fallbackToDefaultExecute = () =>
Expand Down Expand Up @@ -351,19 +354,24 @@ export class InMemoryLiveQueryStore {
}
};

const context: LiveQueryContextValue = {
[originalContextSymbol]: contextValue,
const liveQueryContext: LiveQueryContextValue = {
collectResourceIdentifier,
addResourceIdentifier,
indices: liveQueryStore._indices,
};

if (contextValue == null) {
contextValue = liveQueryContext;
}

liveQueryContextMap.set(contextValue, liveQueryContext);

const result = execute({
schema,
document,
operationName,
rootValue,
contextValue: context,
contextValue,
variableValues,
...additionalArguments,
// TODO: remove this type-cast once GraphQL.js 16-defer-stream with fixed return type got released
Expand All @@ -379,6 +387,7 @@ export class InMemoryLiveQueryStore {
);
return;
}

if (counter === executionCounter) {
liveQueryStore._resourceTracker.track(
scheduleRun,
Expand Down