Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: skip option for subscription #1229

Merged
merged 4 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,7 @@ To use subscription you can use either [subscriptions-transport-ws](https://gith
- `variables`: Object (optional) - Any variables the query might need
- `operationName`: String (optional) - If your query has multiple operations, you can choose which operation you want to call.
- `client`: GraphQLClient - If a GraphQLClient is explicitly passed as an option, then it will be used instead of the client from the `ClientContext`.
- `skip`: Boolean (optional) - If true, the subscription will not be created.
- `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] }`

**Usage**:
Expand Down
8 changes: 6 additions & 2 deletions packages/graphql-hooks/src/useSubscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function useSubscription<
Variables extends object = object,
TGraphQLError = object
>(
options: UseSubscriptionOperation<Variables>,
options: UseSubscriptionOperation<Variables> & { skip?: boolean },
callback: (response: {
data?: ResponseData
errors?: TGraphQLError[]
Expand All @@ -29,6 +29,10 @@ function useSubscription<
}

useDeepCompareEffect(() => {
if (options.skip) {
return
}

const request = {
query: options.query,
variables: options.variables
Expand All @@ -51,7 +55,7 @@ function useSubscription<
return () => {
subscription.unsubscribe()
}
}, [options.query, options.variables])
}, [options.query, options.variables, options.skip])
}

export default useSubscription
25 changes: 25 additions & 0 deletions packages/graphql-hooks/test-jsdom/unit/useSubscription.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -371,4 +371,29 @@ describe('useSubscription', () => {
return { unsubscribe, createSubscription: mockClient.createSubscription }
}
})

it('skips creating the subscription when skip is true', () => {
const subscriptionClient = new MockSubscriptionClient({
type: 'DATA',
data: {}
})
mockClient = {
createSubscription: jest.fn(() => {
return subscriptionClient.request()
}),
subscriptionClient
}
const request = {
query: TEST_SUBSCRIPTION,
variables: {
id: 1
},
skip: true
}
renderHook(() => useSubscription(request, jest.fn()), {
wrapper: Wrapper
})

expect(mockClient.createSubscription).not.toHaveBeenCalled()
})
})
Loading