Apollo-link: Feature Request: Transform Request before sending

Created on 17 Nov 2017  路  4Comments  路  Source: apollographql/apollo-link

I'm trying to implement hawk authentication for my graphql endpoint. For this I need access to the request options and update the Authorization Headers.

Currently there is no way to access / update the data before the request is happening. Would be nice if there were. Since I need the actual request (including the final payload) to calculate the Headers a middlewareLink doesn't seem to work.

help wanted

All 4 comments

@steffenmllr whoa! I've never seen that before!

Yeah I think this would be a great addition to the http-link package. It would also allow quite a lot of other features. Would you be up for a PR to add this?

I'd be happy to help you, or anyone else who wants to add this in!

@steffenmllr so this is actually already possible! I'll push a test showing it soon!

@jbaxleyiii great news! I just monkey patched the link package for now which works but a offical version would be great

@steffenmllr so you can pass a custom fetch to the link which allows you to intercept the request and modify it as needed when you need the fully formed request:

const customFetch = (uri, options) => {
  const { header } = Hawk.client.header('http://example.com:8000/resource/1?b=1&a=2', 'GET', { credentials: credentials, ext: 'some-app-data' });
  options.headers.Authorization = header;
  return fetch(uri, options);
};

const link = createHttpLink({ fetch: customFetch });
Was this page helpful?
0 / 5 - 0 ratings