apollo-link-serialize
Advanced tools
Comparing version 2.1.0 to 3.0.0
@@ -13,3 +13,3 @@ import { ApolloLink, Observable, Operation, NextLink, FetchResult } from 'apollo-link'; | ||
private opQueues; | ||
request(operation: Operation, forward: NextLink): Observable<{}>; | ||
request(origOperation: Operation, forward: NextLink): Observable<{}>; | ||
private enqueue; | ||
@@ -16,0 +16,0 @@ private cancelOp; |
@@ -14,2 +14,3 @@ "use strict"; | ||
var apollo_link_1 = require("apollo-link"); | ||
var extractKey_1 = require("./extractKey"); | ||
var SerializingLink = (function (_super) { | ||
@@ -68,8 +69,8 @@ __extends(SerializingLink, _super); | ||
} | ||
SerializingLink.prototype.request = function (operation, forward) { | ||
SerializingLink.prototype.request = function (origOperation, forward) { | ||
var _this = this; | ||
if (!operation.getContext().serializationKey) { | ||
var _a = extractKey_1.extractKey(origOperation), operation = _a.operation, key = _a.key; | ||
if (!key) { | ||
return forward(operation); | ||
} | ||
var key = operation.getContext().serializationKey; | ||
return new apollo_link_1.Observable(function (observer) { | ||
@@ -76,0 +77,0 @@ var entry = { operation: operation, forward: forward, observer: observer }; |
{ | ||
"name": "apollo-link-serialize", | ||
"version": "2.1.0", | ||
"version": "3.0.0", | ||
"description": "A link that serializes requests by key, making sure that they execute in the exact order submitted", | ||
"dependencies": { | ||
"apollo-link": "^1.2.4", | ||
"apollo-utilities": "^1.0.26", | ||
"zen-observable-ts": "^0.8.11" | ||
@@ -18,4 +19,4 @@ }, | ||
"react-scripts-ts": "^2.6.0", | ||
"ts-jest": "^20.0.14", | ||
"tslint": "^5.8.0", | ||
"ts-jest": "^20.0.14", | ||
"typescript": "^2.6.2" | ||
@@ -22,0 +23,0 @@ }, |
@@ -35,3 +35,47 @@ # apollo-link-serialize | ||
You can indicate requests that should be serialized by providing a serialization key. All requests with the same serialization key will be queued behind one another in the order they are executed. | ||
The key can be expressed via the `@serialize(key: …)` directive: | ||
```graphql | ||
# The key can be a literal string… | ||
mutation favoriteIsRed @serialize(key: "favoriteColor") { | ||
setFavoriteColor(color: "RED) | ||
} | ||
``` | ||
```graphql | ||
# …or it can be a variable in the operation… | ||
mutation upvotePost($id: ID!) @serialize(key: $id) { | ||
post(id: $id) { | ||
addVote | ||
} | ||
} | ||
``` | ||
```graphql | ||
# …and finally, it also supports interpolation: | ||
mutation upvotePost($id: ID!) @serialize(key: "post:{{id}}") { | ||
post(id: $id) { | ||
addVote | ||
} | ||
} | ||
``` | ||
Additionally, you can also pass an explicit serialization key in the operation's context: | ||
```js | ||
link.execute({ | ||
query: gql`mutation { setFavoriteColor(color: "RED") }`, | ||
context: { | ||
serializationKey: 'favoriteColor', | ||
}, | ||
}); | ||
``` | ||
Requests without a serialization key are executed in parallel. Similarly, requests with differing keys are executed in parallel with one another. | ||
### Example | ||
```js | ||
import { ApolloLink } from 'apollo-link'; | ||
@@ -51,7 +95,7 @@ import { HttpLink } from 'apollo-link-http'; | ||
const opColor = { | ||
query: gql`mutation { setFavoriteColor(color: "RED") }`, | ||
context: { | ||
// A request only gets serialized if it has context.serializationKey | ||
serializationKey: 'favoriteColor', | ||
}, | ||
query: gql` | ||
mutation favoriteIsRed @serialize(key: "favoriteColor") { | ||
setFavoriteColor(color: "RED") | ||
} | ||
`, | ||
}; | ||
@@ -61,7 +105,7 @@ | ||
const opColor2 = { | ||
query: gql`mutation { setFavoriteColor(color: "BLUE") }`, | ||
context: { | ||
// A request only gets serialized if it has context.serializationKey | ||
serializationKey: 'favoriteColor', | ||
}, | ||
query: gql` | ||
mutation favoriteIsBlue @serialize(key: "favoriteColor") { | ||
setFavoriteColor(color: "BLUE") | ||
} | ||
`, | ||
}; | ||
@@ -71,7 +115,7 @@ | ||
const opNumber = { | ||
query: gql`mutation { setFavoriteNumber(number: 7) }`, | ||
context: { | ||
// A request only gets serialized if it has context.serializationKey | ||
serializationKey: 'favoriteNumber', | ||
}, | ||
query: gql` | ||
mutation favoriteIsSeven @serialize(key: "favoriteNumber") { | ||
setFavoriteNumber(number: 7) | ||
} | ||
`, | ||
}; | ||
@@ -85,3 +129,2 @@ | ||
}); | ||
link.execute(opNumber).subscribe({ | ||
@@ -88,0 +131,0 @@ next(response) { console.log(response.data.setFavoriteNumber); }, |
@@ -10,2 +10,4 @@ import { | ||
import { extractKey } from './extractKey'; | ||
export interface OperationQueueEntry { | ||
@@ -24,7 +26,8 @@ operation: Operation; | ||
public request(operation: Operation, forward: NextLink) { | ||
if (!operation.getContext().serializationKey) { | ||
public request(origOperation: Operation, forward: NextLink) { | ||
const { operation, key } = extractKey(origOperation); | ||
if (!key) { | ||
return forward(operation); | ||
} | ||
const key = operation.getContext().serializationKey; | ||
return new Observable(observer => { | ||
@@ -54,5 +57,3 @@ const entry = { operation, forward, observer }; | ||
private cancelOp = (key: string, entryToRemove: OperationQueueEntry) => { | ||
if (!this.opQueues[key]) { | ||
return; | ||
} | ||
if (!this.opQueues[key]) { /* should never happen */ return; } | ||
const idx = this.opQueues[key].findIndex(entry => entryToRemove === entry); | ||
@@ -59,0 +60,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
269530
27
1429
134
3
+ Addedapollo-utilities@^1.0.26