apollo-upload-network-interface icon indicating copy to clipboard operation
apollo-upload-network-interface copied to clipboard

Decorate existing network interface?

Open thebigredgeek opened this issue 8 years ago • 3 comments

Very pleased with this package. Glad I won't need to roll my own solution haha. I was wondering if we could expose a higher-order function to decorate an existing network interface? I am currently doing the following for subscriptions:

import { print } from 'graphql-tag/printer';

// quick way to add the subscribe and unsubscribe functions to the network interface
export default function addGraphQLSubscriptions(networkInterface, wsClient) {
  return Object.assign(networkInterface, {
    subscribe(request, handler) {
      return wsClient.subscribe({
        query: print(request.query),
        variables: request.variables,
      }, handler);
    },
    unsubscribe(id) {
      wsClient.unsubscribe(id);
    },
  });
}

It seems like this will be a normal pattern moving forward for extended the default apollo network interface. I don't think it would be too difficult to expose an interface for something like this, and I am willing to submit a PR if you don't have time for it.

Thoughts @HriBB ?

thebigredgeek avatar Nov 17 '16 20:11 thebigredgeek

@thebigredgeek yeah I am busy these days, so go for it!

I am already extending HTTPFetchNetworkInterface (here), so it should be easy to create a HOC to decorate existing interface. My initial implementation included a middleware for the NetworkInterface, but the problem is this line, because it's not possible to override 'Content-Type` easily. One more thing we need to consider is batching. Also I want to wait till the API is finalized.

HriBB avatar Nov 18 '16 10:11 HriBB

To get batching to work, you need to use createBatchingNetworkInterface.

I can't work out how to get both custom interfaces to work together:

import ApolloClient, {createBatchingNetworkInterface} from 'apollo-client'
import createNetworkInterface from 'apollo-upload-network-interface'

Does this mean there is no way to get this project to work with batching?

jaydenseric avatar Feb 20 '17 03:02 jaydenseric

@jaydenseric there is now way to make this project work with batching ATM. Personally I don't need it, but you are welcome to make a PR ;) Hopefully the apollo team will add official support for file uploads. If not, we can implement batching once the API is finalized and the client hits v1.0.

HriBB avatar Feb 20 '17 16:02 HriBB