Skip to content

Commit

Permalink
feat: remove the execute parameter on InMemoryLiveQueryStore (#928)
Browse files Browse the repository at this point in the history
  • Loading branch information
n1ru4l authored Jun 20, 2022
1 parent bb9c030 commit 93239dc
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 46 deletions.
24 changes: 24 additions & 0 deletions .changeset/satan-goats-sacrifice.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
"@n1ru4l/in-memory-live-query-store": minor
---

Drop the `execute` constructor argument option.
Please use `InMemoryLiveQueryStore.makeExecute` instead.

**Old**

```ts
import { InMemoryLiveQueryStore } from "@n1ru4l/in-memory-live-query-store";
import { execute as executeImplementation } from "graphql";
const liveQueryStore = new InMemoryLiveQueryStore({ execute });
const execute = liveQueryStore.execute;
```

**New**

```ts
import { InMemoryLiveQueryStore } from "@n1ru4l/in-memory-live-query-store";
import { execute as executeImplementation } from "graphql";
const liveQueryStore = new InMemoryLiveQueryStore();
const execute = liveQueryStore.makeExecute(executeImplementation);
```
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import {
GraphQLSchema,
GraphQLString,
parse,
execute as executeImplementation,
execute as defaultExecuteImplementation,
GraphQLList,
ExecutionArgs,
} from "graphql";
import { isAsyncIterable } from "@graphql-tools/utils";
import { InMemoryLiveQueryStore } from "./InMemoryLiveQueryStore";
Expand All @@ -28,6 +29,14 @@ function assertNoAsyncIterable(value: unknown) {
}
}

function execute(
store: InMemoryLiveQueryStore,
params: ExecutionArgs,
executeImplementation = defaultExecuteImplementation
) {
return store.makeExecute(executeImplementation)(params);
}

const runAllPendingStuff = () => new Promise((res) => setImmediate(res));

const getAllValues = async <T>(values: AsyncIterable<T>) => {
Expand Down Expand Up @@ -155,7 +164,7 @@ describe("conformance with default `graphql-js` exports", () => {
foo
}
`);
const result = store.execute({
const result = execute(store, {
document,
schema,
});
Expand All @@ -176,7 +185,7 @@ describe("conformance with default `graphql-js` exports", () => {
}
`);

const result = store.execute({
const result = execute(store, {
document,
schema,
});
Expand All @@ -197,7 +206,7 @@ describe("conformance with default `graphql-js` exports", () => {
}
`);

const result = store.execute({
const result = execute(store, {
document,
schema,
});
Expand All @@ -224,7 +233,7 @@ describe("conformance with default `graphql-js` exports", () => {
}
`);

const result = store.execute({
const result = execute(store, {
document,
schema,
});
Expand All @@ -249,7 +258,7 @@ it("returns a AsyncIterable that publishes a query result.", async () => {
}
`);

const executionResult = store.execute({
const executionResult = execute(store, {
schema,
document,
});
Expand Down Expand Up @@ -278,7 +287,7 @@ it("returns a AsyncIterable that publishes a query result after the schema coord
}
`);

const executionResult = store.execute({
const executionResult = execute(store, {
schema,
document,
});
Expand Down Expand Up @@ -325,7 +334,7 @@ it("returns a AsyncIterable that publishes a query result after the resource ide
}
`);

const executionResult = store.execute({
const executionResult = execute(store, {
schema,
document,
});
Expand Down Expand Up @@ -383,7 +392,7 @@ it("does not publish when a old resource identifier is invalidated", async () =>
}
`);

const executionResult = store.execute({
const executionResult = execute(store, {
schema,
document,
});
Expand Down Expand Up @@ -444,7 +453,7 @@ it("can be executed with polymorphic parameter type", () => {
}
`);

const executionResult = store.execute({ schema, document });
const executionResult = execute(store, { schema, document });
expect(executionResult).toEqual({
data: {
foo: "queried",
Expand All @@ -463,7 +472,7 @@ it("can handle missing NoLiveMixedWithDeferStreamRule", async () => {
}
`);

const executionResult = await store.execute({ schema, document });
const executionResult = await execute(store, { schema, document });
if (isAsyncIterable(executionResult)) {
const asyncIterator = executionResult[Symbol.asyncIterator]();
try {
Expand Down Expand Up @@ -508,7 +517,7 @@ it("can collect additional resource identifiers with 'extensions.liveQuery.colle
}
`);
const store = new InMemoryLiveQueryStore();
const executionResult = await store.execute({ schema, document });
const executionResult = await execute(store, { schema, document });

assertAsyncIterable(executionResult);

Expand Down Expand Up @@ -539,7 +548,7 @@ it("adds the resource identifiers as a extension field.", async () => {
}
`);

const executionResult = store.execute({
const executionResult = execute(store, {
schema,
document,
variableValues: {
Expand Down Expand Up @@ -624,7 +633,7 @@ it("can set the id field name arbitrarily", async () => {
}
`);

const executionResult = store.execute({
const executionResult = execute(store, {
schema,
document,
variableValues: {
Expand Down Expand Up @@ -656,7 +665,7 @@ it("can throttle and prevent multiple publishes", async () => {

const store = new InMemoryLiveQueryStore();

const executionResult = store.execute({
const executionResult = execute(store, {
schema,
document,
});
Expand Down Expand Up @@ -695,7 +704,7 @@ it("can throttle and publish new values after the throttle interval", async () =

const store = new InMemoryLiveQueryStore();

const executionResult = store.execute({
const executionResult = execute(store, {
schema,
document,
});
Expand Down Expand Up @@ -733,7 +742,7 @@ it("can prevent execution by returning a string from validateThrottle", async ()
},
});

let executionResult = store.execute({
let executionResult = execute(store, {
schema,
document,
variableValues: {
Expand All @@ -742,7 +751,7 @@ it("can prevent execution by returning a string from validateThrottle", async ()
});
assertAsyncIterable(executionResult);

executionResult = store.execute({
executionResult = execute(store, {
schema,
document,
variableValues: {
Expand Down Expand Up @@ -777,7 +786,7 @@ it("can override the throttle interval by returning a number from validateThrott
},
});

let executionResult = store.execute({
let executionResult = execute(store, {
schema,
document,
variableValues: {
Expand Down Expand Up @@ -816,17 +825,15 @@ it("makeExecute calls the execute it is passed to resolve live queries", async (

const executePassedAtInitializationTime = jest.fn();
executePassedAtInitializationTime.mockImplementation((args) =>
executeImplementation(args)
defaultExecuteImplementation(args)
);

const executePassedToMakeExecute = jest.fn();
executePassedToMakeExecute.mockImplementation((args) =>
executeImplementation(args)
defaultExecuteImplementation(args)
);

const store = new InMemoryLiveQueryStore({
execute: executePassedAtInitializationTime,
});
const store = new InMemoryLiveQueryStore();

const makeExecuteFn = store.makeExecute(executePassedToMakeExecute);

Expand Down Expand Up @@ -856,7 +863,7 @@ it("index via custom index field of type string", async () => {
],
});

const execute = store.makeExecute(executeImplementation);
const execute = store.makeExecute(defaultExecuteImplementation);

const document = parse(/* GraphQL */ `
query @live {
Expand Down Expand Up @@ -893,7 +900,7 @@ it("index via custom index field with string value", async () => {
},
],
});
const execute = store.makeExecute(executeImplementation);
const execute = store.makeExecute(defaultExecuteImplementation);

const document = parse(/* GraphQL */ `
query @live {
Expand Down Expand Up @@ -930,7 +937,7 @@ it("index via custom compound index", async () => {
},
],
});
const execute = store.makeExecute(executeImplementation);
const execute = store.makeExecute(defaultExecuteImplementation);

const document = parse(/* GraphQL */ `
query @live {
Expand Down
21 changes: 2 additions & 19 deletions packages/in-memory-live-query-store/src/InMemoryLiveQueryStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,6 @@ export type InMemoryLiveQueryStoreParameter = {
* This may be useful if you are using a relay compliant schema and the Typename information is not required for building a unique topic.
* */
buildResourceIdentifier?: BuildResourceIdentifierFunction;
/**
* Function which is used for executing the operations.
*
* Uses the `execute` exported from graphql be default.
*
* @deprecated Please use the InMemoryStore.createExecute method instead.
* */
execute?: typeof defaultExecute;
/**
* Whether the extensions should include a list of all resource identifiers for the latest operation result.
* Any of those can be used for invalidating and re-scheduling the operation execution.
Expand Down Expand Up @@ -188,7 +180,6 @@ export class InMemoryLiveQueryStore {
private _resourceTracker = new ResourceTracker<() => void>();
private _schemaCache = new WeakMap<GraphQLSchema, SchemaCacheRecord>();
private _buildResourceIdentifier = defaultResourceIdentifierNormalizer;
private _execute = defaultExecute;
private _includeIdentifierExtension = false;
private _idFieldName = "id";
private _validateThrottleValue: Maybe<ValidateThrottleValueFunction>;
Expand All @@ -198,9 +189,6 @@ export class InMemoryLiveQueryStore {
if (params?.buildResourceIdentifier) {
this._buildResourceIdentifier = params.buildResourceIdentifier;
}
if (params?.execute) {
this._execute = params.execute;
}
if (params?.idFieldName) {
this._idFieldName = params.idFieldName;
}
Expand Down Expand Up @@ -329,15 +317,13 @@ export class InMemoryLiveQueryStore {
run();
}

function dispose() {
onStop.then(function dispose() {
cancelThrottle?.();
liveQueryStore._resourceTracker.release(
scheduleRun,
previousIdentifier
);
}

onStop.then(dispose);
});

run = function run() {
executionCounter = executionCounter + 1;
Expand Down Expand Up @@ -429,9 +415,6 @@ export class InMemoryLiveQueryStore {
});
};

/** @deprecated Please use InMemoryLiveQueryStore.makeExecute instead. */
execute = this.makeExecute(this._execute);

/**
* Invalidate queries (and schedule their re-execution) via a resource identifier.
* @param identifiers A single or list of resource identifiers that should be invalidated.
Expand Down

0 comments on commit 93239dc

Please sign in to comment.