Skip to content

Commit bb2183b

Browse files
authored
feat: skip option for subscription (#1229)
* feat: skip option for subscription * fix: move skip check * docs(subscription): add skip option * docs: revert `options` -> `operation`
1 parent 584d381 commit bb2183b

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,7 @@ To use subscription you can use either [subscriptions-transport-ws](https://gith
424424
- `variables`: Object (optional) - Any variables the query might need
425425
- `operationName`: String (optional) - If your query has multiple operations, you can choose which operation you want to call.
426426
- `client`: GraphQLClient - If a GraphQLClient is explicitly passed as an option, then it will be used instead of the client from the `ClientContext`.
427+
- `skip`: Boolean (optional) - If true, the subscription will not be created.
427428
- `callback`: Function - This will be invoked when the subscription receives an event from your GraphQL server - it will receive an object with the typical GraphQL response of `{ data: <your result>, errors?: [Error] }`
428429

429430
**Usage**:

packages/graphql-hooks/src/useSubscription.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ function useSubscription<
1010
Variables extends object = object,
1111
TGraphQLError = object
1212
>(
13-
options: UseSubscriptionOperation<Variables>,
13+
options: UseSubscriptionOperation<Variables> & { skip?: boolean },
1414
callback: (response: {
1515
data?: ResponseData
1616
errors?: TGraphQLError[]
@@ -29,6 +29,10 @@ function useSubscription<
2929
}
3030

3131
useDeepCompareEffect(() => {
32+
if (options.skip) {
33+
return
34+
}
35+
3236
const request = {
3337
query: options.query,
3438
variables: options.variables
@@ -51,7 +55,7 @@ function useSubscription<
5155
return () => {
5256
subscription.unsubscribe()
5357
}
54-
}, [options.query, options.variables])
58+
}, [options.query, options.variables, options.skip])
5559
}
5660

5761
export default useSubscription

packages/graphql-hooks/test-jsdom/unit/useSubscription.test.tsx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,4 +371,29 @@ describe('useSubscription', () => {
371371
return { unsubscribe, createSubscription: mockClient.createSubscription }
372372
}
373373
})
374+
375+
it('skips creating the subscription when skip is true', () => {
376+
const subscriptionClient = new MockSubscriptionClient({
377+
type: 'DATA',
378+
data: {}
379+
})
380+
mockClient = {
381+
createSubscription: jest.fn(() => {
382+
return subscriptionClient.request()
383+
}),
384+
subscriptionClient
385+
}
386+
const request = {
387+
query: TEST_SUBSCRIPTION,
388+
variables: {
389+
id: 1
390+
},
391+
skip: true
392+
}
393+
renderHook(() => useSubscription(request, jest.fn()), {
394+
wrapper: Wrapper
395+
})
396+
397+
expect(mockClient.createSubscription).not.toHaveBeenCalled()
398+
})
374399
})

0 commit comments

Comments
 (0)