apollo-link-persisted-queries
Advanced tools
Comparing version 0.0.1-beta.1 to 0.0.1-beta.2
@@ -5,2 +5,5 @@ # Change log | ||
### 0.0.1-beta.2 | ||
- improve error handling through 500s and optional function to disable | ||
### 0.0.1-beta.1 | ||
@@ -7,0 +10,0 @@ - support hash lookup function for build time generation |
@@ -33,6 +33,20 @@ (function (global, factory) { | ||
generateHash: defaultGenerateHash, | ||
disable: function (_a, _b) { | ||
var errors = _a.errors; | ||
var response = _b.response; | ||
if (errors.some(function (_a) { | ||
var message = _a.message; | ||
return message === 'PersistedQueryNotSupported'; | ||
})) { | ||
return true; | ||
} | ||
if (response && response.statusCode && response.statusCode >= 500) { | ||
return true; | ||
} | ||
return false; | ||
}, | ||
}; | ||
var createPersistedQueryLink = function (_a) { | ||
var generateHash = (_a === void 0 ? defaultOptions : _a).generateHash; | ||
var doesNotSupportPersistedQueries = false; | ||
var _b = _a === void 0 ? defaultOptions : _a, generateHash = _b.generateHash, disable = _b.disable; | ||
var supportsPersistedQueries = true; | ||
var calculated = new Map(); | ||
@@ -42,3 +56,3 @@ return new apolloLink.ApolloLink(function (operation, forward) { | ||
var hashError; | ||
if (!doesNotSupportPersistedQueries) { | ||
if (supportsPersistedQueries) { | ||
var hash = calculated.get(query); | ||
@@ -65,18 +79,8 @@ if (!hash) { | ||
var subscription; | ||
var tried; | ||
var tried = false; | ||
var handler = { | ||
next: function (_a) { | ||
var data = _a.data, errors = _a.errors, rest = __rest(_a, ["data", "errors"]); | ||
if (!tried && | ||
errors && | ||
errors.some(function (_a) { | ||
var message = _a.message; | ||
return message.indexOf('PersistedQueryNot') > -1; | ||
})) { | ||
if (errors.some(function (_a) { | ||
var message = _a.message; | ||
return message === 'PersistedQueryNotSupported'; | ||
})) { | ||
doesNotSupportPersistedQueries = true; | ||
} | ||
if (!tried && errors) { | ||
supportsPersistedQueries = !disable(__assign({ data: data, errors: errors }, rest), operation.getContext()); | ||
tried = true; | ||
@@ -88,3 +92,3 @@ if (subscription) | ||
includeQuery: true, | ||
includeExtensions: !doesNotSupportPersistedQueries, | ||
includeExtensions: supportsPersistedQueries, | ||
}, | ||
@@ -102,4 +106,4 @@ }); | ||
http: { | ||
includeQuery: doesNotSupportPersistedQueries, | ||
includeExtensions: !doesNotSupportPersistedQueries, | ||
includeQuery: !supportsPersistedQueries, | ||
includeExtensions: supportsPersistedQueries, | ||
}, | ||
@@ -106,0 +110,0 @@ }); |
import { ApolloLink } from 'apollo-link'; | ||
import { ExecutionResult } from 'graphql'; | ||
export declare const VERSION = 1; | ||
export declare type Options = { | ||
generateHash?: (DocumentNode) => string; | ||
}; | ||
export declare const defaultGenerateHash: (query: any) => any; | ||
export declare const defaultOptions: { | ||
generateHash: (query: any) => any; | ||
disable: ({errors}: { | ||
errors: any; | ||
}, {response}: { | ||
response: any; | ||
}) => boolean; | ||
}; | ||
export declare const createPersistedQueryLink: ({generateHash}?: Options) => ApolloLink; | ||
export declare const createPersistedQueryLink: ({generateHash, disable}?: { | ||
generateHash?: (DocumentNode: any) => string; | ||
disable?: (result: ExecutionResult, context: any) => boolean; | ||
}) => ApolloLink; |
@@ -29,6 +29,20 @@ var __assign = (this && this.__assign) || Object.assign || function(t) { | ||
generateHash: defaultGenerateHash, | ||
disable: function (_a, _b) { | ||
var errors = _a.errors; | ||
var response = _b.response; | ||
if (errors.some(function (_a) { | ||
var message = _a.message; | ||
return message === 'PersistedQueryNotSupported'; | ||
})) { | ||
return true; | ||
} | ||
if (response && response.statusCode && response.statusCode >= 500) { | ||
return true; | ||
} | ||
return false; | ||
}, | ||
}; | ||
export var createPersistedQueryLink = function (_a) { | ||
var generateHash = (_a === void 0 ? defaultOptions : _a).generateHash; | ||
var doesNotSupportPersistedQueries = false; | ||
var _b = _a === void 0 ? defaultOptions : _a, generateHash = _b.generateHash, disable = _b.disable; | ||
var supportsPersistedQueries = true; | ||
var calculated = new Map(); | ||
@@ -38,3 +52,3 @@ return new ApolloLink(function (operation, forward) { | ||
var hashError; | ||
if (!doesNotSupportPersistedQueries) { | ||
if (supportsPersistedQueries) { | ||
var hash = calculated.get(query); | ||
@@ -61,18 +75,8 @@ if (!hash) { | ||
var subscription; | ||
var tried; | ||
var tried = false; | ||
var handler = { | ||
next: function (_a) { | ||
var data = _a.data, errors = _a.errors, rest = __rest(_a, ["data", "errors"]); | ||
if (!tried && | ||
errors && | ||
errors.some(function (_a) { | ||
var message = _a.message; | ||
return message.indexOf('PersistedQueryNot') > -1; | ||
})) { | ||
if (errors.some(function (_a) { | ||
var message = _a.message; | ||
return message === 'PersistedQueryNotSupported'; | ||
})) { | ||
doesNotSupportPersistedQueries = true; | ||
} | ||
if (!tried && errors) { | ||
supportsPersistedQueries = !disable(__assign({ data: data, errors: errors }, rest), operation.getContext()); | ||
tried = true; | ||
@@ -84,3 +88,3 @@ if (subscription) | ||
includeQuery: true, | ||
includeExtensions: !doesNotSupportPersistedQueries, | ||
includeExtensions: supportsPersistedQueries, | ||
}, | ||
@@ -98,4 +102,4 @@ }); | ||
http: { | ||
includeQuery: doesNotSupportPersistedQueries, | ||
includeExtensions: !doesNotSupportPersistedQueries, | ||
includeQuery: !supportsPersistedQueries, | ||
includeExtensions: supportsPersistedQueries, | ||
}, | ||
@@ -102,0 +106,0 @@ }); |
{ | ||
"name": "apollo-link-persisted-queries", | ||
"version": "0.0.1-beta.1", | ||
"version": "0.0.1-beta.2", | ||
"description": "Use persisted queries with Apollo Link", | ||
@@ -13,8 +13,11 @@ "author": "James Baxley <james@meteor.com>", | ||
"type": "git", | ||
"url": "git+https://github.com/apollographql/apollo-link.git" | ||
"url": | ||
"git+https://github.com/apollographql/apollo-link-persisted-queries.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/apollographql/apollo-link/issues" | ||
"url": | ||
"https://github.com/apollographql/apollo-link-persisted-queries/issues" | ||
}, | ||
"homepage": "https://github.com/apollographql/apollo-link#readme", | ||
"homepage": | ||
"https://github.com/apollographql/apollo-link-persisted-queries#readme", | ||
"scripts": { | ||
@@ -21,0 +24,0 @@ "build:browser": |
@@ -9,3 +9,3 @@ Automatic Persisted Queries | ||
Automatic Persisted Queries solves this problem by sending a generated id instead of the query text as the request. | ||
Automatic Persisted Queries solves this problem by sending a generated ID instead of the query text as the request. | ||
@@ -16,4 +16,4 @@ For more information about this solution, read [this article announcing Automatic Persisted Queries]. | ||
1. When the client makes a query, it will optimistically send a short (64-byte) cryptographic hash instead of the full query text. | ||
2. If the backend recognizes the hash, it will retrieve the full text of the query and execute it | ||
3. If the backend doesn't recogize the hash, it will ask the client to send the hash and the query text to it can store them mapped together for future lookups. During this request, the backend will also fufil the data request. | ||
2. If the backend recognizes the hash, it will retrieve the full text of the query and execute it. | ||
3. If the backend doesn't recogize the hash, it will ask the client to send the hash and the query text to it can store them mapped together for future lookups. During this request, the backend will also fulfill the data request. | ||
@@ -41,11 +41,12 @@ This library is a client implementation for use with Apollo Client by using custom Apollo Link. | ||
#### Options | ||
The createPersistedQueryLink function takes an optional object with configuration. Currently the only supported configutation is a key called `generateHash` which recieves the query and returns the hash. | ||
- `generateHash`: a function that takes the query document and returns the hash | ||
The createPersistedQueryLink function takes an optional object with configuration. Currently the only supported configutations are a key called `generateHash` which recieves the query and returns the hash, a function to conditionally disabled sending persisted queries on error | ||
- `generateHash`: a function that takes the query document and returns the hash. If not provided, `generateHash` defaults to a fast implementation of sha256 + hex digest. | ||
- `disable`: a function which takes the response and context (typically including the fetch response as `response`) and returns a boolean to disable any future persited queries for that session. This defaults to disabling on `PersistedQueryNotSupported` or a 500 or greater status code | ||
## Apollo Engine | ||
Apollo Engine supports recieving and fufulling Automatic Persisted Queries. Simply adding this link into your client app will improve your network response times when using Apollo Engine. | ||
Apollo Engine supports recieving and fulfulling Automatic Persisted Queries. Simply adding this link into your client app will improve your network response times when using Apollo Engine. | ||
### Protocal | ||
Automatic Persisted Queries are made up of three parts: the query signature, error responses, and the negotiaion protocal. | ||
### Protocol | ||
Automatic Persisted Queries are made up of three parts: the query signature, error responses, and the negotiaion protocol. | ||
@@ -68,3 +69,3 @@ **Query Signature** | ||
When sending an Automatic Persisted Query, the client ommits the `query` field normally present, and instead sends an extension field with a `persistedQuery` object as shown above. The hash is a `sha256` hash of the query string. | ||
When sending an Automatic Persisted Query, the client ommits the `query` field normally present, and instead sends an extension field with a `persistedQuery` object as shown above. The hash algorithm defaults to a `sha256` hash of the query string. | ||
@@ -110,3 +111,3 @@ If the client needs to register the hash, the query signature will be the same but include the full query text like so: | ||
**Negotiation Protocal** | ||
**Negotiation Protocol** | ||
In order to support Automatic Persisted Queries, the client and server must follow the negotiaion steps as outlined here: | ||
@@ -124,3 +125,3 @@ | ||
4. Client sends both hash and query string to Server | ||
5. Server fufils response and saves query string + hash for future lookup | ||
5. Server fulfills response and saves query string + hash for future lookup | ||
6. Client recieves data and completes request | ||
@@ -127,0 +128,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
46005
379
127