apollo-link-http
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -5,2 +5,9 @@ # Change log | ||
### v1.1.0 | ||
- support dynamic endpoints using `uri` on the context | ||
- the request not attaches the raw response as `response` on the context. This can be used to access response headers or more | ||
### v1.0.0 | ||
- official release, not changes | ||
### v0.9.0 | ||
@@ -7,0 +14,0 @@ - changed `fetcherOptions` to be `fetchOptions` and added a test for using 'GET' requests |
@@ -66,3 +66,3 @@ (function (global, factory) { | ||
library = 'node-fetch'; | ||
throw new Error("fetch is not found globally and no fetcher passed, to fix pass a fetch for\n your environment like https://www.npmjs.com/package/" + library + ".\n\n For example:\n import fetch from '" + library + "';\n import { createHttpLink } from 'apollo-link-http';\n\n const link = createFetchLink({ uri: '/graphql', fetch: fetch });\n "); | ||
throw new Error("fetch is not found globally and no fetcher passed, to fix pass a fetch for\n your environment like https://www.npmjs.com/package/" + library + ".\n\n For example:\n import fetch from '" + library + "';\n import { createHttpLink } from 'apollo-link-http';\n\n const link = createHttpLink({ uri: '/graphql', fetch: fetch });\n "); | ||
} | ||
@@ -89,3 +89,3 @@ }; | ||
return new apolloLink.Observable(function (observer) { | ||
var _a = operation.getContext(), headers = _a.headers, credentials = _a.credentials, _b = _a.fetchOptions, fetchOptions = _b === void 0 ? {} : _b; | ||
var _a = operation.getContext(), headers = _a.headers, credentials = _a.credentials, _b = _a.fetchOptions, fetchOptions = _b === void 0 ? {} : _b, contextURI = _a.uri; | ||
var operationName = operation.operationName, extensions = operation.extensions, variables = operation.variables, query = operation.query; | ||
@@ -126,3 +126,7 @@ var body = { | ||
fetcherOptions.signal = signal; | ||
fetcher(uri, fetcherOptions) | ||
fetcher(contextURI || uri, fetcherOptions) | ||
.then(function (response) { | ||
operation.setContext({ response: response }); | ||
return response; | ||
}) | ||
.then(parseAndCheckResponse(operation)) | ||
@@ -129,0 +133,0 @@ .then(function (result) { |
@@ -62,3 +62,3 @@ var __extends = (this && this.__extends) || (function () { | ||
library = 'node-fetch'; | ||
throw new Error("fetch is not found globally and no fetcher passed, to fix pass a fetch for\n your environment like https://www.npmjs.com/package/" + library + ".\n\n For example:\n import fetch from '" + library + "';\n import { createHttpLink } from 'apollo-link-http';\n\n const link = createFetchLink({ uri: '/graphql', fetch: fetch });\n "); | ||
throw new Error("fetch is not found globally and no fetcher passed, to fix pass a fetch for\n your environment like https://www.npmjs.com/package/" + library + ".\n\n For example:\n import fetch from '" + library + "';\n import { createHttpLink } from 'apollo-link-http';\n\n const link = createHttpLink({ uri: '/graphql', fetch: fetch });\n "); | ||
} | ||
@@ -85,3 +85,3 @@ }; | ||
return new Observable(function (observer) { | ||
var _a = operation.getContext(), headers = _a.headers, credentials = _a.credentials, _b = _a.fetchOptions, fetchOptions = _b === void 0 ? {} : _b; | ||
var _a = operation.getContext(), headers = _a.headers, credentials = _a.credentials, _b = _a.fetchOptions, fetchOptions = _b === void 0 ? {} : _b, contextURI = _a.uri; | ||
var operationName = operation.operationName, extensions = operation.extensions, variables = operation.variables, query = operation.query; | ||
@@ -122,3 +122,7 @@ var body = { | ||
fetcherOptions.signal = signal; | ||
fetcher(uri, fetcherOptions) | ||
fetcher(contextURI || uri, fetcherOptions) | ||
.then(function (response) { | ||
operation.setContext({ response: response }); | ||
return response; | ||
}) | ||
.then(parseAndCheckResponse(operation)) | ||
@@ -125,0 +129,0 @@ .then(function (result) { |
{ | ||
"name": "apollo-link-http", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "HTTP transport layer for GraphQL", | ||
@@ -48,3 +48,3 @@ "author": "Evans Hauser <evanshauser@gmail.com>", | ||
"@types/graphql": "0.11.5", | ||
"@types/jest": "21.1.4", | ||
"@types/jest": "21.1.5", | ||
"apollo-fetch": "0.6.0", | ||
@@ -59,6 +59,6 @@ "apollo-link": "^1.0.0", | ||
"rollup": "0.45.2", | ||
"ts-jest": "21.1.3", | ||
"ts-jest": "21.1.4", | ||
"tslint": "5.8.0", | ||
"typescript": "2.5.1", | ||
"uglify-js": "3.1.5" | ||
"typescript": "2.6.1", | ||
"uglify-js": "3.1.6" | ||
}, | ||
@@ -65,0 +65,0 @@ "jest": { |
@@ -39,6 +39,11 @@ --- | ||
## Context | ||
The Http Link uses the `headers` field on the context to allow passing headers to the HTTP request. It also supports the `credentials` field for defining credentials policy for fetch and `fetchOptions` to allow generic fetch overrides (i.e. method: "GET"). These options will override the same key if passed when creating the the link. | ||
The Http Link uses the `headers` field on the context to allow passing headers to the HTTP request. It also supports the `credentials` field for defining credentials policy, `uri` for changing the endpoint dynamically, and `fetchOptions` to allow generic fetch overrides (i.e. method: "GET"). These options will override the same key if passed when creating the the link. | ||
This link also attaches the response from the `fetch` operation on the context as `response` so you can access it from within another link. | ||
- `headers`: an object representing values to be sent as headers on the request | ||
- `credentials`: a string representing the credentials policy you want for the fetch call | ||
- `uri`: a string of the endpoint you want to fetch from | ||
- `fetchOptions`: any overrides of the fetch options argument to pass to the fetch call | ||
- `response`: this is the raw response from the fetch request after it is made. | ||
@@ -49,3 +54,3 @@ | ||
import ApolloClient from "apollo-client"; | ||
import InMemoryCache from "apollo-cache-inmemory"; | ||
import { InMemoryCache } from "apollo-cache-inmemory"; | ||
@@ -52,0 +57,0 @@ const client = new ApolloClient({ |
@@ -26,2 +26,3 @@ import { Observable, ApolloLink, execute } from 'apollo-link'; | ||
const data = { data: { hello: 'world' } }; | ||
const data2 = { data: { hello: 'everyone' } }; | ||
const mockError = { throws: new TypeError('mock me') }; | ||
@@ -32,6 +33,9 @@ | ||
beforeEach(() => { | ||
fetchMock.post('begin:data2', data2); | ||
fetchMock.post('begin:data', data); | ||
fetchMock.post('begin:error', mockError); | ||
fetchMock.post('begin:apollo', data); | ||
fetchMock.get('begin:data', data); | ||
fetchMock.get('begin:data2', data2); | ||
@@ -219,3 +223,15 @@ const next = jest.fn(); | ||
}); | ||
it('allows for dynamic endpoint setting', done => { | ||
const variables = { params: 'stub' }; | ||
const link = createHttpLink({ uri: 'data' }); | ||
execute(link, { | ||
query: sampleQuery, | ||
variables, | ||
context: { uri: 'data2' }, | ||
}).subscribe(result => { | ||
expect(result).toEqual(data2); | ||
done(); | ||
}); | ||
}); | ||
it('adds headers to the request from the context', done => { | ||
@@ -227,3 +243,7 @@ const variables = { params: 'stub' }; | ||
}); | ||
return forward(operation); | ||
return forward(operation).map(result => { | ||
const { response } = operation.getContext(); | ||
expect(response.headers).toBeDefined(); | ||
return result; | ||
}); | ||
}); | ||
@@ -338,2 +358,47 @@ const link = middleware.concat(createHttpLink({ uri: 'data' })); | ||
}); | ||
it('adds uri to the request from the context', done => { | ||
const variables = { params: 'stub' }; | ||
const middleware = new ApolloLink((operation, forward) => { | ||
operation.setContext({ | ||
uri: 'data', | ||
}); | ||
return forward(operation); | ||
}); | ||
const link = middleware.concat(createHttpLink()); | ||
execute(link, { query: sampleQuery, variables }).subscribe(result => { | ||
const uri = fetchMock.lastUrl(); | ||
expect(uri).toBe('data'); | ||
done(); | ||
}); | ||
}); | ||
it('adds uri to the request from the setup', done => { | ||
const variables = { params: 'stub' }; | ||
const link = createHttpLink({ uri: 'data' }); | ||
execute(link, { query: sampleQuery, variables }).subscribe(result => { | ||
const uri = fetchMock.lastUrl(); | ||
expect(uri).toBe('data'); | ||
done(); | ||
}); | ||
}); | ||
it('prioritizes context uri over setup uri', done => { | ||
const variables = { params: 'stub' }; | ||
const middleware = new ApolloLink((operation, forward) => { | ||
operation.setContext({ | ||
uri: 'apollo', | ||
}); | ||
return forward(operation); | ||
}); | ||
const link = middleware.concat( | ||
createHttpLink({ uri: 'data', credentials: 'error' }), | ||
); | ||
execute(link, { query: sampleQuery, variables }).subscribe(result => { | ||
const uri = fetchMock.lastUrl(); | ||
expect(uri).toBe('apollo'); | ||
done(); | ||
}); | ||
}); | ||
it('adds fetchOptions to the request from the setup', done => { | ||
@@ -340,0 +405,0 @@ const variables = { params: 'stub' }; |
@@ -72,3 +72,3 @@ import { ApolloLink, Observable, RequestHandler } from 'apollo-link'; | ||
const link = createFetchLink({ uri: '/graphql', fetch: fetch }); | ||
const link = createHttpLink({ uri: '/graphql', fetch: fetch }); | ||
`, | ||
@@ -119,2 +119,3 @@ ); | ||
fetchOptions = {}, | ||
uri: contextURI, | ||
} = operation.getContext(); | ||
@@ -170,3 +171,8 @@ const { operationName, extensions, variables, query } = operation; | ||
fetcher(uri, fetcherOptions) | ||
fetcher(contextURI || uri, fetcherOptions) | ||
// attach the raw response to the context for usage | ||
.then(response => { | ||
operation.setContext({ response }); | ||
return response; | ||
}) | ||
.then(parseAndCheckResponse(operation)) | ||
@@ -173,0 +179,0 @@ .then(result => { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
68362
1133
189