@envelop/persisted-operations 2.1.0 → 2.2.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.
package/README.md CHANGED
@@ -103,6 +103,23 @@ const getEnveloped = envelop({
103
103
  const proxyFns = getEnveloped({ req });
104
104
  ```
105
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
+
106
123
  ## With Relay
107
124
 
108
125
  If you are using Relay, you can leverage `relay-compiler` feature for hashing and and creating the store for you, during build.
package/index.js CHANGED
@@ -92,6 +92,8 @@ const usePersistedOperations = (rawOptions) => {
92
92
  setParsedDocument(document);
93
93
  return;
94
94
  }
95
+ if (options.onMissingMatch)
96
+ options.onMissingMatch(context, operationId);
95
97
  if (options.onlyPersisted) {
96
98
  // we want to throw an error only when "onlyPersisted" is true, otherwise we let execution continue normally
97
99
  throw new graphql.GraphQLError(`Unable to match operation with id '${operationId}'`);
package/index.mjs CHANGED
@@ -88,6 +88,8 @@ const usePersistedOperations = (rawOptions) => {
88
88
  setParsedDocument(document);
89
89
  return;
90
90
  }
91
+ if (options.onMissingMatch)
92
+ options.onMissingMatch(context, operationId);
91
93
  if (options.onlyPersisted) {
92
94
  // we want to throw an error only when "onlyPersisted" is true, otherwise we let execution continue normally
93
95
  throw new GraphQLError(`Unable to match operation with id '${operationId}'`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@envelop/persisted-operations",
3
- "version": "2.1.0",
3
+ "version": "2.2.0",
4
4
  "sideEffects": false,
5
5
  "peerDependencies": {
6
6
  "graphql": "^14.0.0 || ^15.0.0 || ^16.0.0"
package/plugin.d.ts CHANGED
@@ -15,6 +15,10 @@ export declare type UsePersistedOperationsOptions<ContextType = DefaultContext>
15
15
  * Function that returns the operation id, e.g. by retrieving it from cusotm properties within context
16
16
  */
17
17
  extractOperationId?: (context: Readonly<ContextType>) => string | undefined;
18
+ /**
19
+ * Callback function to notify consumer of missing hash match, f.i. to log, monitor and/or analise these events
20
+ */
21
+ onMissingMatch?: (context: Readonly<ContextType>, operationId: string) => void;
18
22
  };
19
23
  declare const contextProperty: unique symbol;
20
24
  export declare type PersistedOperationPluginContext = {