graphql(forget, { props: ({ mutate }) => ({ forget: (email) => mutate({ variables: { email }, update: (proxy, { data }) => { // Read the data from our cache for this query. let cached = proxy.readQuery({ query: userProfile }) // Errors cached.errors = data.errors // User cached.user = cached.user || { _id: null, name: null, status: null, __typename: 'User' } cached.user.status = data.forget.status // Write our data back to the cache. proxy.writeQuery({ query: userProfile, data: cached }) } }) }) })(Forget)
graphql(signup, { props: ({ mutate }) => ({ signup: (email, password) => mutate({ variables: { email, password }, update: (proxy, { data }) => { // Keep session data.signup && persist.willSetSessionToken(data.signup.sessionToken) // Read the data from our cache for this query. let cached = proxy.readQuery({ query: userProfile }) // Errors cached.errors = data.errors // User cached.user = data.signup ? data.signup.user : { _id: null, name: null, status: null, __typename: 'User' } // Authen cached.authen = { isLoggedIn: data.signup ? data.signup.isLoggedIn : null, sessionToken: data.signup ? data.signup.sessionToken : null, __typename: 'Authen' } // Write our data back to the cache. proxy.writeQuery({ query: userProfile, data: cached }) } }) }) })(SignUp)
graphql(loginWithFacebook, { props: ({ mutate }) => ({ loginWithFacebook: (deviceInfo, accessToken) => mutate({ variables: { deviceInfo, accessToken }, update: (proxy, { data }) => { // Keep session persist.willSetSessionToken(data.loginWithFacebook.sessionToken) // Read the data from our cache for this query. let cached = proxy.readQuery({ query: userProfile }) // Errors cached.errors = data.errors // User cached.user = data.loginWithFacebook.user // Authen cached.authen = { isLoggedIn: data.loginWithFacebook.isLoggedIn, sessionToken: data.loginWithFacebook.sessionToken, __typename: 'Authen' } // Write our data back to the cache. proxy.writeQuery({ query: userProfile, data: cached }) } }) }) })
let cached = proxy.readQuery({ query: userProfile })
graphql(logout, { props: ({ mutate }) => ({ logout: () => mutate({ update: (proxy, { data }) => { // Clear session persist.willRemoveSessionToken() // Read the data from our cache for this query. let cached = proxy.readQuery({ query: userProfile }) // Errors cached.errors = data.errors // User cached.user = data.logout.user ? data.logout.user : { _id: null, name: null, status: null, __typename: 'User' } // Authen cached.authen = { isLoggedIn: data.logout.isLoggedIn, sessionToken: data.logout.sessionToken, __typename: 'Authen' } // Write our data back to the cache. proxy.writeQuery({ query: userProfile, data: cached }) } }) }) })(Logout)
graphql(CREATE_CARD, { props: ({ mutate }) => ({ createCard: (content, name, order) => mutate({ variables: { content, name, order }, optimisticResponse: { __typename: 'Mutation', createCard: { id: 'abc123', __typename: 'Card', content, name, order, }, }, update: (store, { data: { createCard } }) => { // Read the data from our cache for this query. const data = store.readQuery({ query: ALL_CARDS }); // Add our comment from the mutation to the end. data.allCards.push(createCard); // Write our data back to the cache. store.writeQuery({ query: ALL_CARDS, data }); }, }), }), })(CreateCard)