Skip to content

Commit

Permalink
Pass context object to link chain when using subscriptions
Browse files Browse the repository at this point in the history
Makes sure a supplied `context` object is passed through the
link execution chain, when set in `client.subscribe` options.
  • Loading branch information
hwillson committed Apr 28, 2020
1 parent e75a1ec commit 24be8fa
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@
- Add a default `gc` implementation to `ApolloCache`. <br/>
[@justinwaite](https://github.com/justinwaite) in [#5974](https://github.com/apollographql/apollo-client/pull/5974)

- Support passing a `context` object through the link execution chain when using subscriptions. <br/>
[@sgtpepper43](https://github.com/sgtpepper43) in [#4925](https://github.com/apollographql/apollo-client/pull/4925)

### Bug Fixes

- `useMutation` adjustments to help avoid an infinite loop / too many renders issue, caused by unintentionally modifying the `useState` based mutation result directly. <br/>
Expand Down
22 changes: 22 additions & 0 deletions src/__tests__/graphqlSubscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ describe('GraphQL Subscriptions', () => {
variables: {
name: 'Changping Chen',
},
context: {
someVar: 'Some value'
}
};

defaultSub1 = {
Expand Down Expand Up @@ -275,4 +278,23 @@ describe('GraphQL Subscriptions', () => {
setTimeout(() => link.simulateComplete(), 100);
});
});

it('should pass a context object through the link execution chain', done => {
const link = mockObservableLink();
const client = new ApolloClient({
cache: new InMemoryCache(),
link,
});

client.subscribe(options).subscribe({
next() {
expect(link.operation.getContext().someVar).toEqual(
options.context.someVar
);
done();
},
});

link.simulateResult(results[0]);
});
});
1 change: 1 addition & 0 deletions src/core/ObservableQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ export class ObservableQuery<
.startGraphQLSubscription({
query: options.document,
variables: options.variables,
context: options.context,
})
.subscribe({
next: (subscriptionData: { data: TSubscriptionData }) => {
Expand Down
4 changes: 3 additions & 1 deletion src/core/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -844,14 +844,15 @@ export class QueryManager<TStore> {
query,
fetchPolicy,
variables,
context = {},
}: SubscriptionOptions): Observable<FetchResult<T>> {
query = this.transform(query).document;
variables = this.getVariables(query, variables);

const makeObservable = (variables: OperationVariables) =>
this.getObservableFromLink<T>(
query,
{},
context,
variables,
false,
).map(result => {
Expand Down Expand Up @@ -883,6 +884,7 @@ export class QueryManager<TStore> {
const observablePromise = this.localState.addExportedVariables(
query,
variables,
context,
).then(makeObservable);

return new Observable<FetchResult<T>>(observer => {
Expand Down
6 changes: 6 additions & 0 deletions src/core/watchQueryOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ export type SubscribeToMoreOptions<
variables?: TSubscriptionVariables;
updateQuery?: UpdateQueryFn<TData, TSubscriptionVariables, TSubscriptionData>;
onError?: (error: Error) => void;
context?: Record<string, any>;
};

export interface SubscriptionOptions<TVariables = OperationVariables> {
Expand All @@ -159,6 +160,11 @@ export interface SubscriptionOptions<TVariables = OperationVariables> {
* Specifies the {@link FetchPolicy} to be used for this subscription.
*/
fetchPolicy?: FetchPolicy;

/**
* Context object to be passed through the link execution chain.
*/
context?: Record<string, any>;
}

export type RefetchQueryDescription = Array<string | PureQueryOptions>;
Expand Down
4 changes: 3 additions & 1 deletion src/utilities/testing/mocking/mockSubscriptionLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ export interface MockedSubscriptionResult {
export class MockSubscriptionLink extends ApolloLink {
public unsubscribers: any[] = [];
public setups: any[] = [];
public operation: Operation;

private observer: any;

constructor() {
super();
}

public request(_req: any) {
public request(operation: Operation) {
this.operation = operation;
return new Observable<FetchResult>(observer => {
this.setups.forEach(x => x());
this.observer = observer;
Expand Down

0 comments on commit 24be8fa

Please sign in to comment.