apollo-fetch
Advanced tools
Comparing version 0.0.0 to 0.1.0
@@ -5,4 +5,4 @@ # Change log | ||
### 0.0.0 | ||
### 0.1.0 | ||
Initial release |
@@ -22,3 +22,3 @@ "use strict"; | ||
var request = requestAndOptions.request, options = requestAndOptions.options; | ||
var queue = function (funcs, scope) { | ||
var buildMiddlewareStack = function (funcs, scope) { | ||
var next = function () { | ||
@@ -40,3 +40,3 @@ if (funcs.length > 0) { | ||
}; | ||
queue(_middlewares.slice(), _this); | ||
buildMiddlewareStack(_middlewares.slice(), _this); | ||
}); | ||
@@ -48,3 +48,3 @@ }; | ||
var responseObject = { response: response, options: options }; | ||
var queue = function (funcs, scope) { | ||
var buildAfterwareStack = function (funcs, scope) { | ||
var next = function () { | ||
@@ -63,3 +63,3 @@ if (funcs.length > 0) { | ||
}; | ||
queue(_afterwares.slice(), _this); | ||
buildAfterwareStack(_afterwares.slice(), _this); | ||
}); | ||
@@ -69,7 +69,28 @@ }; | ||
var request = _a.request, options = _a.options; | ||
var opts = __assign({ body: JSON.stringify(request), method: 'POST' }, options, { headers: __assign({ Accept: '*/*', 'Content-Type': 'application/json' }, options.headers) }); | ||
var body; | ||
try { | ||
body = JSON.stringify(request); | ||
} | ||
catch (e) { | ||
throw new Error("Network request failed. Payload is not serizable: " + e.message); | ||
} | ||
var opts = __assign({ body: body, method: 'POST' }, options, { headers: __assign({ Accept: '*/*', 'Content-Type': 'application/json' }, (options.headers || [])) }); | ||
return customFetch ? customFetch(_uri, opts) : fetch(_uri, opts); | ||
}; | ||
var throwHttpError = function (response, error) { | ||
var httpError; | ||
if (response && response.status >= 300) { | ||
httpError = new Error("Network request failed with status " + response.status + " - \"" + response.statusText + "\""); | ||
} | ||
else { | ||
httpError = new Error("Network request failed to return valid JSON"); | ||
} | ||
httpError.response = response; | ||
httpError.raw = response.raw; | ||
httpError.parseError = error; | ||
throw httpError; | ||
}; | ||
var apolloFetch = Object.assign(function (request) { | ||
var options = {}; | ||
var parseError; | ||
return applyMiddlewares({ | ||
@@ -80,4 +101,3 @@ request: request, | ||
.then(callFetch) | ||
.then(function (response) { return response.text() | ||
.then(function (raw) { | ||
.then(function (response) { return response.text().then(function (raw) { | ||
try { | ||
@@ -88,10 +108,5 @@ var parsed = JSON.parse(raw); | ||
catch (e) { | ||
parseError = e; | ||
return __assign({}, response, { raw: raw, parsed: null }); | ||
} | ||
}) | ||
.catch(function (error) { | ||
var httpError = new Error("Network request failed with status " + response.status + " - \"" + response.statusText + "\""); | ||
httpError.response = response; | ||
httpError.parseError = error; | ||
throw httpError; | ||
}); }) | ||
@@ -108,6 +123,3 @@ .then(function (response) { return applyAfterwares({ | ||
else { | ||
var jsonError = new Error("Network request failed to return valid JSON"); | ||
jsonError.response = response; | ||
jsonError.raw = response.raw; | ||
throw jsonError; | ||
throwHttpError(response, parseError); | ||
} | ||
@@ -114,0 +126,0 @@ }); |
{ | ||
"name": "apollo-fetch", | ||
"version": "0.0.0", | ||
"version": "0.1.0", | ||
"description": "Lightweight implementation of fetch for GraphQL requests", | ||
@@ -48,2 +48,4 @@ "author": "Evans Hauser <evanshauser@gmail.com>", | ||
"istanbul": "^0.4.4", | ||
"graphql": "^0.10.3", | ||
"graphql-tag": "^2.4.2", | ||
"lodash": "^4.17.4", | ||
@@ -50,0 +52,0 @@ "mocha": "^3.2.0", |
118
README.md
# apollo-fetch | ||
`apollo-fetch` is a lightweight fetch for GraphQL requests that supports the middleware and afterware to modify the request and response. | ||
`apollo-fetch` is a lightweight client for GraphQL requests that supports middleware and afterware that modify requests and responses. | ||
By default, `apollo-fetch` uses `isomorphic-fetch` and provides the option of using a custom fetch function. | ||
By default `apollo-fetch` uses `isomorphic-fetch`, but you have the option of using a custom fetch function. | ||
In addition, `apollo-fetch` supports passing a context to the server and receiving a context. | ||
This context can be used by the middleware and afterware. | ||
# Usage | ||
Simple GraphQL query | ||
Simple GraphQL query: | ||
@@ -17,3 +14,3 @@ ```js | ||
const uri = 'example.com/graphql'; | ||
const uri = 'http://api.githunt.com/graphql'; | ||
@@ -28,9 +25,10 @@ const query = ` | ||
` | ||
const apolloFetch = createApolloFetch({ uri }); | ||
createApolloFetch({uri})({query}) | ||
.then((result) => { | ||
// GraphQL data, errors, and extensions plus context from the server | ||
const {data, error, extensions context} = result; | ||
apolloFetch({ query }) | ||
.then( result => { | ||
// GraphQL data, GraphQL errors and GraphQL extensions | ||
const { data, error, extensions } = result; | ||
}) | ||
.catch((error) => { | ||
.catch(error => { | ||
//respond to a network error | ||
@@ -40,3 +38,4 @@ }) | ||
Simple GraphQL mutation with authentication middleware | ||
Simple GraphQL mutation with authentication middleware. | ||
Middleware has access to the GraphQL query and the options passed to fetch. | ||
@@ -46,3 +45,3 @@ ```js | ||
const uri = 'example.com/graphql'; | ||
const uri = 'http://api.githunt.com/graphql'; | ||
@@ -58,43 +57,6 @@ const query = ` | ||
const apolloFetch = createApolloFetch({uri}); | ||
const apolloFetch = createApolloFetch({ uri }); | ||
apolloFetch.use({applyMiddleware: ({request, options}, next) => { | ||
if (!options.headers) { | ||
options.headers = {}; // Create the headers object if needed. | ||
} | ||
options.headers['authorization'] = 'created token'; | ||
next(); | ||
}}); | ||
apolloFetch({query}) | ||
.then((result) => { | ||
// GraphQL data, errors, and extensions plus context from the server | ||
const {data, error, extensions context} = result; | ||
}) | ||
.catch((error) => { | ||
//respond to a network error | ||
}) | ||
``` | ||
Simple GraphQL mutation with authentication middleware | ||
```js | ||
import { createApolloFetch } from 'apollo-fetch' | ||
const uri = 'example.com/graphql'; | ||
const query = ` | ||
query sampleMutation(id: ID!) { | ||
addSample(id: $id) { | ||
id, | ||
name | ||
} | ||
} | ||
` | ||
const apolloFetch = createApolloFetch({uri}); | ||
apolloFetch.use([{ | ||
applyMiddleware: ({ options }, next) => { | ||
applyMiddleware: ({ request, options }, next) => { | ||
if (!options.headers) { | ||
@@ -106,11 +68,11 @@ options.headers = {}; // Create the headers object if needed. | ||
next(); | ||
} | ||
}); | ||
}, | ||
}]); | ||
apolloFetch({query}) | ||
.then((result) => { | ||
// GraphQL data, errors, and extensions plus context from the server | ||
const {data, error, extensions context} = result; | ||
apolloFetch({ query }) | ||
.then(result => { | ||
// GraphQL data, errors, and extensions | ||
const { data, error, extensions } = result; | ||
}) | ||
.catch((error) => { | ||
.catch(error => { | ||
//respond to a network error | ||
@@ -120,3 +82,4 @@ }) | ||
Afterware to check the response status and logout on a 401 | ||
Afterware to check the response status and logout on a 401. | ||
The afterware has access to the raw reponse always and parsed response when the data is proper JSON. | ||
@@ -126,7 +89,7 @@ ```js | ||
const uri = 'example.com/graphql'; | ||
const uri = 'http://api.githunt.com/graphql'; | ||
const apolloFetch = createApolloFetch({uri}); | ||
const apolloFetch = createApolloFetch({ uri }); | ||
apolloFetch.useAfter({ | ||
apolloFetch.useAfter([{ | ||
applyAfterware: ({ response }, next) => { | ||
@@ -137,11 +100,11 @@ if (response.status === 401) { | ||
next(); | ||
} | ||
}); | ||
}, | ||
}]); | ||
apolloFetch({query}) | ||
.then((result) => { | ||
// GraphQL data, errors, and extensions plus context from the server | ||
const {data, error, extensions context} = result; | ||
apolloFetch({ query }) | ||
.then(result => { | ||
// GraphQL data, errors, and extensions from the server | ||
const { data, error, extensions } = result; | ||
}) | ||
.catch((error) => { | ||
.catch(error => { | ||
//respond to a network error | ||
@@ -165,3 +128,3 @@ }) | ||
`createApolloFetch` is a factory for `ApolloFetch`, a fetch with middleware and afterware capabilities. | ||
`createApolloFetch` is a factory for `ApolloFetch`, a fetch function with middleware and afterware capabilities. | ||
@@ -239,3 +202,3 @@ ```js | ||
`ParsedResponse` adds `raw`, the body from the .text() call on the fetch result, and `parsed`, the parsed JSON from `raw`, onto the regular Response from the fetch call. | ||
`ParsedResponse` adds `raw` (the body from the `.text()` call) to the fetch result, and `parsed` (the parsed JSON from `raw`) to the regular Response from the fetch call. | ||
@@ -248,1 +211,10 @@ ```js | ||
``` | ||
Errors returned from a call to `ApolloFetch` are normal errors that contain the parsed response, the raw response from .text(), and a possible parse error. | ||
```js | ||
FetchError extends Error { | ||
response: ParsedResponse; | ||
raw: string; | ||
parseError?: Error; | ||
} |
Sorry, the diff of this file is not supported yet
33317
17
20
282
209