@n1ru4l/in-memory-live-query-store
Advanced tools
Comparing version 0.8.0-alpha-c8576d0.0 to 0.8.0-alpha-e2bf954.0
@@ -256,3 +256,3 @@ import { visit, visitWithTypeInfo, isNonNullType, isScalarType, execute, getOperationAST, GraphQLError, defaultFieldResolver, TypeInfo } from 'graphql'; | ||
} | ||
const { schema, typeInfo } = this.getPatchedSchema(inputSchema); | ||
const { schema, typeInfo } = this._getPatchedSchema(inputSchema); | ||
const rootFieldIdentifier = Array.from(extractLiveQueryRootFieldCoordinates({ | ||
@@ -364,2 +364,3 @@ documentNode: document, | ||
}; | ||
/** @deprecated Please use InMemoryLiveQueryStore.makeExecute instead. */ | ||
this.execute = this.makeExecute(this._execute); | ||
@@ -383,3 +384,3 @@ if (params === null || params === void 0 ? void 0 : params.buildResourceIdentifier) { | ||
} | ||
getPatchedSchema(inputSchema) { | ||
_getPatchedSchema(inputSchema) { | ||
let data = this._schemaCache.get(inputSchema); | ||
@@ -386,0 +387,0 @@ if (isNone(data)) { |
@@ -260,3 +260,3 @@ 'use strict'; | ||
} | ||
const { schema, typeInfo } = this.getPatchedSchema(inputSchema); | ||
const { schema, typeInfo } = this._getPatchedSchema(inputSchema); | ||
const rootFieldIdentifier = Array.from(extractLiveQueryRootFieldCoordinates({ | ||
@@ -368,2 +368,3 @@ documentNode: document, | ||
}; | ||
/** @deprecated Please use InMemoryLiveQueryStore.makeExecute instead. */ | ||
this.execute = this.makeExecute(this._execute); | ||
@@ -387,3 +388,3 @@ if (params === null || params === void 0 ? void 0 : params.buildResourceIdentifier) { | ||
} | ||
getPatchedSchema(inputSchema) { | ||
_getPatchedSchema(inputSchema) { | ||
let data = this._schemaCache.get(inputSchema); | ||
@@ -390,0 +391,0 @@ if (isNone(data)) { |
@@ -11,3 +11,3 @@ import { ExecutionResult, execute as defaultExecute, ExecutionArgs } from "graphql"; | ||
export declare type ValidateThrottleValueFunction = (throttleValue: Maybe<number>) => Maybe<string | number>; | ||
declare type InMemoryLiveQueryStoreParameter = { | ||
export declare type InMemoryLiveQueryStoreParameter = { | ||
/** | ||
@@ -25,2 +25,4 @@ * Custom function for building resource identifiers. | ||
* Uses the `execute` exported from graphql be default. | ||
* | ||
* @deprecated Please use the InMemoryStore.createExecute method instead. | ||
* */ | ||
@@ -56,4 +58,5 @@ execute?: typeof defaultExecute; | ||
constructor(params?: InMemoryLiveQueryStoreParameter); | ||
private getPatchedSchema; | ||
private _getPatchedSchema; | ||
makeExecute: (execute: typeof defaultExecute) => (args: ExecutionArgs) => LiveExecuteReturnType; | ||
/** @deprecated Please use InMemoryLiveQueryStore.makeExecute instead. */ | ||
execute: (args: ExecutionArgs) => LiveExecuteReturnType; | ||
@@ -60,0 +63,0 @@ /** |
{ | ||
"name": "@n1ru4l/in-memory-live-query-store", | ||
"version": "0.8.0-alpha-c8576d0.0", | ||
"version": "0.8.0-alpha-e2bf954.0", | ||
"peerDependencies": { | ||
@@ -9,3 +9,3 @@ "graphql": "^15.4.0 || ^16.0.0" | ||
"@graphql-tools/utils": "^8.5.2", | ||
"@n1ru4l/graphql-live-query": "0.9.0-alpha-c8576d0.0", | ||
"@n1ru4l/graphql-live-query": "0.9.0-alpha-e2bf954.0", | ||
"@n1ru4l/push-pull-async-iterable-iterator": "^3.0.0" | ||
@@ -12,0 +12,0 @@ }, |
119
README.md
@@ -19,3 +19,3 @@ # @n1ru4l/in-memory-live-query-store | ||
A `InMemoryLiveQueryStore` instance tracks, invalidates and re-executes registered live query operations. | ||
A `InMemoryLiveQueryStore` instance tracks, invalidates and re-executes registered live query operations. | ||
@@ -28,3 +28,3 @@ The store will keep track of all root query field coordinates (e.g. `Query.todos`) and global resource identifiers (e.g. `Todo:1`). The store can than be notified to re-execute live query operations that select a given root query field or resource identifier by calling the `invalidate` method with the corresponding values. | ||
import { InMemoryLiveQueryStore } from "@n1ru4l/in-memory-live-query-store"; | ||
import { parse } from "graphql"; | ||
import { parse, execute as executeImplementation } from "graphql"; | ||
import { schema } from "./schema"; | ||
@@ -44,26 +44,26 @@ | ||
inMemoryLiveQueryStore | ||
.execute({ | ||
schema, // make sure your schema has the GraphQLLiveDirective from @n1ru4l/graphql-live-query | ||
operationDocument: parse(/* GraphQL */ ` | ||
query todosQuery @live { | ||
todos { | ||
id | ||
content | ||
isComplete | ||
} | ||
const execute = inMemoryLiveQueryStore.makeExecute(executeImplementation); | ||
execute({ | ||
schema, // make sure your schema has the GraphQLLiveDirective from @n1ru4l/graphql-live-query | ||
operationDocument: parse(/* GraphQL */ ` | ||
query todosQuery @live { | ||
todos { | ||
id | ||
content | ||
isComplete | ||
} | ||
`), | ||
rootValue: rootValue, | ||
contextValue: {}, | ||
variableValues: null, | ||
operationName: "todosQuery", | ||
}) | ||
.then(async (result) => { | ||
if (isAsyncIterable(result)) { | ||
for (const value of result) { | ||
console.log(value); | ||
} | ||
} | ||
}); | ||
`), | ||
rootValue: rootValue, | ||
contextValue: {}, | ||
variableValues: null, | ||
operationName: "todosQuery", | ||
}).then(async (result) => { | ||
if (isAsyncIterable(result)) { | ||
for (const value of result) { | ||
console.log(value); | ||
} | ||
} | ||
}); | ||
@@ -79,5 +79,5 @@ // Invalidate by resource identifier | ||
The `InMemoryLiveQueryStore.execute` function is a drop-in replacement for the default `execute` function exported from `graphql-js`. | ||
The `execute` function returned from `InMemoryLiveQueryStore.makeExecute` is a drop-in replacement for the default `execute` function exported from `graphql-js`. You can provide your own `execute` function from any graphql-js version or other library. | ||
Pass it to your favorite graphql transport that supports returning `AsyncIterator` from `execute` and thus delivering incremental query execution results. | ||
Pass it to your favorite graphql transport that supports returning `AsyncIterableIterator` (or `AsyncGenerator`) from `execute` and thus delivering incremental query execution results. | ||
@@ -92,3 +92,2 @@ List of known and tested compatible transports/servers: | ||
## Recipes | ||
@@ -101,62 +100,44 @@ | ||
```ts | ||
import Redis from 'ioredis' | ||
import { InMemoryLiveQueryStore } from '@n1ru4l/in-memory-live-query-store' | ||
import Redis from "ioredis"; | ||
import { | ||
execute as defaultExecute, | ||
ExecutionArgs, | ||
ExecutionResult | ||
} from 'graphql' | ||
import { LiveExecutionResult } from '@n1ru4l/graphql-live-query' | ||
InMemoryLiveQueryStore, | ||
InMemoryLiveQueryStoreParameter, | ||
} from "@n1ru4l/in-memory-live-query-store"; | ||
import { ExecutionArgs, execute as defaultExecute } from "graphql"; | ||
declare type MaybePromise<T> = T | Promise<T> | ||
const CHANNEL = "LIVE_QUERY_INVALIDATIONS"; | ||
declare type ExecutionParameter = | ||
| Parameters<typeof defaultExecute> | ||
| [ExecutionArgs] | ||
export interface LiveQueryStore { | ||
invalidate: (identifiers: Array<string> | string) => Promise<void> | ||
execute: ( | ||
...args: ExecutionParameter | ||
) => MaybePromise< | ||
| AsyncIterableIterator<ExecutionResult | LiveExecutionResult> | ||
| ExecutionResult | ||
> | ||
} | ||
const CHANNEL = 'LIVE_QUERY_INVALIDATIONS' | ||
export class RedisLiveQueryStore { | ||
pub: Redis.Redis | ||
sub: Redis.Redis | ||
liveQueryStore: InMemoryLiveQueryStore | ||
pub: Redis.Redis; | ||
sub: Redis.Redis; | ||
liveQueryStore: InMemoryLiveQueryStore; | ||
constructor(redisUrl: string) { | ||
this.pub = new Redis(redisUrl) | ||
this.sub = new Redis(redisUrl) | ||
this.liveQueryStore = new InMemoryLiveQueryStore() | ||
constructor(redisUrl: string, parameter?: InMemoryLiveQueryStoreParameter) { | ||
this.pub = new Redis(redisUrl); | ||
this.sub = new Redis(redisUrl); | ||
this.liveQueryStore = new InMemoryLiveQueryStore(parameter); | ||
this.sub.subscribe(CHANNEL, err => { | ||
if (err) throw err | ||
}) | ||
this.sub.subscribe(CHANNEL, (err) => { | ||
if (err) throw err; | ||
}); | ||
this.sub.on('message', (channel, resourceIdentifier) => { | ||
this.sub.on("message", (channel, resourceIdentifier) => { | ||
if (channel === CHANNEL && resourceIdentifier) | ||
this.liveQueryStore.invalidate(resourceIdentifier) | ||
}) | ||
this.liveQueryStore.invalidate(resourceIdentifier); | ||
}); | ||
} | ||
async invalidate(identifiers: Array<string> | string) { | ||
if (typeof identifiers === 'string') { | ||
identifiers = [identifiers] | ||
if (typeof identifiers === "string") { | ||
identifiers = [identifiers]; | ||
} | ||
for (const identifier of identifiers) { | ||
this.pub.publish(CHANNEL, identifier) | ||
this.pub.publish(CHANNEL, identifier); | ||
} | ||
} | ||
execute(...args: ExecutionParameter) { | ||
return this.liveQueryStore.execute(...args) | ||
makeExecute(execute: typeof defaultExecute) { | ||
return this.liveQueryStore.makeExecute(args); | ||
} | ||
} | ||
``` |
49602
934
138
+ Added@n1ru4l/graphql-live-query@0.9.0-alpha-e2bf954.0(transitive)
- Removed@n1ru4l/graphql-live-query@0.9.0-alpha-c8576d0.0(transitive)