apollographql / apollo-feature-requests

🧑‍🚀 Apollo Client Feature Requests | (no 🐛 please).
Other
130 stars 7 forks source link

Internal Context Object in Link Operations #377

Open JoePassanante opened 1 year ago

JoePassanante commented 1 year ago

Proposal

The Apollo Link Operation should be passed additional context from internal decisions made when crafting that request.

For example, when queries are created from a poling interval being triggered a link should understand that this was the source. Allowing, the split link can decide if an operation came from polling it should be moved into a batch link or a normal HTTP link.

Interface

export enum OperationSource {
 DIRECT
 POLLING
}

export interface Operation {
  query: DocumentNode;
  variables: Record<string, any>;
  operationName: string;
  extensions: Record<string, any>;
  setContext: (context: DefaultContext) => DefaultContext;
  getContext: () => DefaultContext;
  source: OperationSource;
}

For example

const pollingSplit = split((operation)=>{
    return operation.source === OperationSource.POLLING
}, batchHTTPLink, httpLink)

I'm not sold on having an enum, but want the context to be strongly typed. Its coming from Apollos internal implementation, so it shouldn't be generic.

jerelmiller commented 1 year ago

Hey @JoePassanante 👋

This is an interesting idea!

Would you be able to expand upon the use cases for this type of behavior? You mention the batch link as an example, but could you describe further what advantages a conditional batch link has over http link in the case of polling?

Are there other use cases you can think of where this behavior would be beneficial or otherwise impossible without this solution?