|
| 1 | +# @n1ru4l/graphql-live-query-patch |
| 2 | + |
| 3 | +[](https://www.npmjs.com/package/@n1ru4l/graphql-live-query-patch) [](https://www.npmjs.com/package/@n1ru4l/graphql-live-query-patch) |
| 4 | + |
| 5 | +Make your live query payload smaller with json patches. |
| 6 | + |
| 7 | +If you have big live query operations you might opt into only sending JSON patches to your clients. This requires the server to always store the latest execution result. When a new execution result is published a JSON patch is generated by diffing those execution results. |
| 8 | +The resulting patch operations are then sent to the client where they are applied to the initial execution result. |
| 9 | + |
| 10 | +For example usage check out the todo-example client & server code. |
| 11 | + |
| 12 | +## Install Instructions |
| 13 | + |
| 14 | +```bash |
| 15 | +yarn add -E @n1ru4l/graphql-live-query-patch |
| 16 | +``` |
| 17 | + |
| 18 | +## API |
| 19 | + |
| 20 | +### `createLiveQueryPatchDeflator` |
| 21 | + |
| 22 | +```ts |
| 23 | +import { execute } from "graphql"; |
| 24 | +import { createLiveQueryPatchDeflator } from "@n1ru4l/graphql-live-query-patch"; |
| 25 | +import { schema } from "./schema"; |
| 26 | + |
| 27 | +execute({ |
| 28 | + schema, |
| 29 | + operationDocument: parse(/* GraphQL */ ` |
| 30 | + query todosQuery @live { |
| 31 | + todos { |
| 32 | + id |
| 33 | + content |
| 34 | + isComplete |
| 35 | + } |
| 36 | + } |
| 37 | + `), |
| 38 | + rootValue: rootValue, |
| 39 | + contextValue: {}, |
| 40 | + variableValues: null, |
| 41 | + operationName: "todosQuery", |
| 42 | +}).then(async (result) => { |
| 43 | + if (isAsyncIterable(result)) { |
| 44 | + for (const value of createLiveQueryPatchDeflator(result)) { |
| 45 | + console.log(value); |
| 46 | + } |
| 47 | + } |
| 48 | +}); |
| 49 | +``` |
| 50 | + |
| 51 | +### `applyLiveQueryPatchDeflator` |
| 52 | + |
| 53 | +Convenience wrapper for applying `createLiveQueryPatchDeflator` on the `execute` return value. |
| 54 | + |
| 55 | +```ts |
| 56 | +import { execute } from "graphql"; |
| 57 | +import { applyLiveQueryPatchDeflator } from "@n1ru4l/graphql-live-query-patch"; |
| 58 | +import { schema } from "./schema"; |
| 59 | + |
| 60 | +const result = applyLiveQueryPatchDeflator( |
| 61 | + execute({ |
| 62 | + schema, |
| 63 | + operationDocument: parse(/* GraphQL */ ` |
| 64 | + query todosQuery @live { |
| 65 | + todos { |
| 66 | + id |
| 67 | + content |
| 68 | + isComplete |
| 69 | + } |
| 70 | + } |
| 71 | + `), |
| 72 | + rootValue: rootValue, |
| 73 | + contextValue: {}, |
| 74 | + variableValues: null, |
| 75 | + operationName: "todosQuery", |
| 76 | + }) |
| 77 | +); |
| 78 | +``` |
| 79 | + |
| 80 | +### `createLiveQueryPatchInflator` |
| 81 | + |
| 82 | +Inflate the execution result on the client side. |
| 83 | + |
| 84 | +```ts |
| 85 | +import { createLiveQueryPatchInflator } from "@n1ru4l/graphql-live-query-patch"; |
| 86 | + |
| 87 | +const asyncIterable = createLiveQueryPatchInflator( |
| 88 | + networkLayer.execute({ |
| 89 | + operation: /* GraphQL */ ` |
| 90 | + query todosQuery @live { |
| 91 | + todos { |
| 92 | + id |
| 93 | + content |
| 94 | + isComplete |
| 95 | + } |
| 96 | + } |
| 97 | + `, |
| 98 | + }) |
| 99 | +); |
| 100 | +``` |
0 commit comments