Created
August 9, 2019 05:14
-
-
Save Ranatchai/9eda388baab0055335d34a7bd92769e9 to your computer and use it in GitHub Desktop.
Apollo client state Example
This file contains 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 { clientState } from './src/state/clientState' | |
const client = new ApolloClient({ | |
clientState | |
}) | |
export default class App extends React.Component { | |
render() { | |
return ( | |
<ApolloProvider client={client}> | |
<AppContainer /> | |
</ApolloProvider> | |
) | |
} | |
} |
This file contains 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 * 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 | |
} | |
} | |
} |
This file contains 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 { 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 | |
} | |
` | |
} |
This file contains 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 { 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 { | |
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