Last active
October 15, 2021 09:08
-
-
Save David-Melo/c01b77528c2d1fd00d040e1b917c0beb to your computer and use it in GitHub Desktop.
Overmind GraphQL Effects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { graphql } from 'overmind-graphql'; | |
import { SubscriptionClient } from 'subscriptions-transport-ws'; | |
// Query & Mutation GQL Tags | |
import * as queries from './queries'; | |
import * as mutations from './mutations'; | |
// Subscription GQL Tags | |
import { UsersUpdatedQuery } from './generated/api'; | |
// overmind-graphql | |
export const gql = graphql({ | |
queries, | |
mutations | |
}); | |
type Subscriptions = { | |
[index: string]: { | |
unsubscribe: () => void; | |
} | |
} | |
// Custom Subscriptions Effect | |
export const subscriptions = (() => { | |
let client: SubscriptionClient; | |
let subscriptions: Subscriptions = {}; | |
return { | |
initialize() { | |
client = new SubscriptionClient('ws://localhost:8080/v1/graphql', { | |
reconnect: true, | |
connectionParams: { | |
headers: { | |
'x-hasura-admin-secret': '12343545' | |
} | |
} | |
}); | |
}, | |
usersUpdated(action: any, variables: Object = {}) { | |
let observable = client.request({ | |
query: UsersUpdatedQuery, | |
variables | |
}); | |
subscriptions['usersUpdated'] = observable.subscribe({ | |
next: ({data}) => action(data) | |
}); | |
}, | |
unsubscribe(subscriptionName: string) { | |
subscriptions[subscriptionName].unsubscribe(); | |
} | |
} | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you! This is the only way I could get graphql subscriptions working with overmind!