Skip to content

Instantly share code, notes, and snippets.

@Ranatchai
Created August 9, 2019 05:14
Show Gist options
  • Save Ranatchai/9eda388baab0055335d34a7bd92769e9 to your computer and use it in GitHub Desktop.
Save Ranatchai/9eda388baab0055335d34a7bd92769e9 to your computer and use it in GitHub Desktop.
Apollo client state Example
import { clientState } from './src/state/clientState'
const client = new ApolloClient({
clientState
})
export default class App extends React.Component {
render() {
return (
<ApolloProvider client={client}>
<AppContainer />
</ApolloProvider>
)
}
}
import * as SignupForm from './signup.state'
import * as OpenAccountProgress from './openAccount.state'
export const clientState = {
defaults: {
...SignupForm.apolloState.initialState,
...OpenAccountProgress.apolloState.initialState
},
resolvers: {
Mutation: {
...SignupForm.apolloState.Mutation,
...OpenAccountProgress.apolloState.Mutation
}
}
}
import { gql } from 'apollo-boost'
export const apolloState = {
initialState: {
openAccountProgress: 2
},
Mutation: {
setOpenAccountProgress: (_, { openAccountProgress }, { cache }) => {
cache.writeData({
data: {
openAccountProgress
}
})
return null
}
}
}
export const OpenAccountQuery = gql`
query {
openAccountProgress @client
}
`
export const OpenAccountMutation = {
setOpenAccountProgress: gql`
mutation setOpenAccountProgress($openAccountProgress: Int!) {
setOpenAccountProgress(openAccountProgress: $openAccountProgress) @client
}
`
}
import { gql } from 'apollo-boost'
export const apolloState = {
initialState: {
signupForm: {
__typename: 'SignupForm',
email: '',
idCard: '',
phone: ''
}
},
Mutation: {
setSignupForm: (_, { form }, { cache }) => {
cache.writeData({
data: {
signupForm: {
__typename: 'SignupForm',
...form
}
}
})
return null
}
}
}
export const SignupFormQuery = gql`
query {
signupForm @client {
email
idCard
phone
}
}
`
export const SignupFormMutation = {
setSignupForm: gql`
mutation setSignupForm($form: SignupForm) {
setSignupForm(form: $form) @client
}
`
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment