@envelop/persisted-operations 4.4.0-alpha-26c4ae2.0 → 4.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +127 -0
  2. package/package.json +2 -2
package/README.md ADDED
@@ -0,0 +1,127 @@
1
+ ## `@envelop/persisted-operations`
2
+
3
+ This plugin allow you to enforce execution of persisted (hashed) operation, using a custom store.
4
+
5
+ The idea behind running persisted operations is to allow clients to run only specific queries, that defined ahead of time. This provides an enhances security and disables (optionally) the execution of other operations. This plugin is useful if you are looking for a way to improve security and reduce network traffic.
6
+
7
+ ## Getting Started
8
+
9
+ ```
10
+ yarn add @envelop/persisted-operations
11
+ ```
12
+
13
+ ## Usage Example
14
+
15
+ The most basic implementation can use an in-memory JS `Map` wrapper with a `Store` object:
16
+
17
+ ```ts
18
+ import { envelop } from '@envelop/core';
19
+ import { usePersistedOperations, InMemoryStore } from '@envelop/persisted-operations';
20
+
21
+ // You can retrieve the store in any way (e.g. from a remote source) and implement it with a simple Map / Key->Value
22
+ const myData = new Map();
23
+ myData.set('persisted_1', parse(`query { ... }`));
24
+ myData.set('persisted_2', 'query { ... }'));
25
+
26
+ const store = new InMemoryStore({
27
+ initialData: myData,
28
+ })
29
+
30
+ const getEnveloped = envelop({
31
+ plugins: [
32
+ // ... other plugins ...
33
+ usePersistedOperations({
34
+ store: myStore,
35
+ }),
36
+ ],
37
+ });
38
+ ```
39
+
40
+ Now, when running operations through your GraphQL server, you can use a key instead of a query language:
41
+
42
+ ```json
43
+ {
44
+ "query": "persisted_1",
45
+ "variables": {}
46
+ }
47
+ ```
48
+
49
+ You can also provide a function to retrieve the operation id from a custom property available in your context / incoming request:
50
+
51
+ ```ts
52
+ usePersistedOperations({
53
+ store: myStore,
54
+ extractOperationId: (context) => context.request.body.operationId // get id from custom property in body object
55
+ }),
56
+ ```
57
+
58
+ ## Usage Example with built-in JsonFileStore
59
+
60
+ ```ts
61
+ import { usePersistedOperations, JsonFileStore } from '@envelop/persisted-operations';
62
+
63
+ const persistedOperationsStore = new JsonFilesStore();
64
+ const filePath = resolve(process.cwd(), 'assets/client1PersistedOperations.json');
65
+
66
+ // sync
67
+ persistedOperationsStore.loadFromFileSync(filePath); // load and parse persisted-operations files
68
+
69
+ // or async
70
+ await persistedOperationsStore.loadFromFile(filePath); // load and parse persisted-operations files
71
+
72
+ const getEnveloped = envelop({
73
+ plugins: [
74
+ // ... other plugins ...
75
+ usePersistedOperations({
76
+ store: persistedOperationsStore,
77
+ }),
78
+ ],
79
+ });
80
+ ```
81
+
82
+ ## Multiple Stores
83
+
84
+ The `store` parameter accepts both a `Store` instance, or a function. If you need to support multiple stores (based on incoming GraphQL operation/HTTP request), you can provide a function to toggle between the stores, based on your needs:
85
+
86
+ ```ts
87
+ const getEnveloped = envelop({
88
+ plugins: [
89
+ // ... other plugins ...
90
+ usePersistedOperations({
91
+ store: context => {
92
+ if (context.req.headers['user-agent'].includes('Android')) {
93
+ return mobileClientsStore;
94
+ }
95
+
96
+ return defaultStore;
97
+ },
98
+ }),
99
+ ],
100
+ });
101
+
102
+ // later, pass the initial context
103
+ const proxyFns = getEnveloped({ req });
104
+ ```
105
+
106
+ ## Additional options
107
+
108
+ ### onlyPersisted
109
+
110
+ You can pass `onlyPersisted: true` when you want to allow persisted operations only in your server. In this case the plugin will issue a GraphQL error when it does not receive an operation Id, or when the received operation id is not available in your store/s.
111
+
112
+ ### onMissingMatch
113
+
114
+ You might want to perform some actions, such as logging custom events, when your operation Id is not matched in your store/s; in this case you can use the `onMissingMatch` callback function.
115
+ The function receives the context and operationId as arguments, so you can use it like so:
116
+
117
+ ```js
118
+ onMissingMatch: (context, operationId) => {
119
+ myEventPool.add(`Missing match for operation "${operationId}" from agent "${context.req.headers['user-agent']}"`);
120
+ };
121
+ ```
122
+
123
+ ## With Relay
124
+
125
+ If you are using Relay, you can leverage `relay-compiler` feature for hashing and and creating the store for you, during build.
126
+
127
+ You can [read more about this feature here](https://relay.dev/docs/guides/persisted-queries/). After building your hashes, you can use `queryMap.json` as your store.
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@envelop/persisted-operations",
3
- "version": "4.4.0-alpha-26c4ae2.0",
3
+ "version": "4.4.0",
4
4
  "sideEffects": false,
5
5
  "peerDependencies": {
6
- "@envelop/core": "2.4.0-alpha-26c4ae2.0",
6
+ "@envelop/core": "^2.4.0",
7
7
  "graphql": "^14.0.0 || ^15.0.0 || ^16.0.0"
8
8
  },
9
9
  "repository": {