From a8fde5c7b7fd86308763372c29b452d9c1a97c53 Mon Sep 17 00:00:00 2001 From: Mike Moran Date: Sun, 28 Jun 2020 15:22:31 +0100 Subject: [PATCH] add to recentlyCompleted when problemCompleted - this updates the in-memory cached value of `recentlyCompleted` whenever the `problemCompleted` mutation is called --- src/model/contexts.js | 38 +++++++++++++++++++++++++++++++++++++- src/model/types.graphql | 2 +- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/model/contexts.js b/src/model/contexts.js index c86f316..ac6c4c8 100644 --- a/src/model/contexts.js +++ b/src/model/contexts.js @@ -10,6 +10,7 @@ import { } from "../model/problem"; import {} from "../model/problem.js"; import { loader } from "graphql.macro"; +import { gql } from "apollo-boost"; export const StoreContext = React.createContext(null); @@ -34,6 +35,14 @@ export const GraphQLProvider = ({ children }) => { return { __typename: "ProblemLink", path, name: "Random Hard Game" }; } const cache = new InMemoryCache(); + cache.writeData({ + data: { + current_user: { + __typename: "User", + recentlyCompleted: [], + }, + }, + }); const client = new ApolloClient({ cache, resolvers: { @@ -58,11 +67,38 @@ export const GraphQLProvider = ({ children }) => { }, }, Mutation: { - problemCompleted: (_root, args, _context, _info) => { + problemCompleted: (_root, args, context, _info) => { const { problemSpec: { gridSpec, teamsSpec }, } = args; const completedProblem = parseProblemFrom(gridSpec, teamsSpec); + + const { cache } = context; + const query = gql` + query GetRecentlyCompleted { + current_user @client { + recentlyCompleted @client { + gridSpec + teamsSpec + } + } + } + `; + const previousData = cache.readQuery({ query }); + const nextData = { + current_user: { + __typename: "User", + recentlyCompleted: [ + { + __typename: "ProblemSpec", + gridSpec, + teamsSpec, + }, + ].concat(previousData.current_user.recentlyCompleted), + }, + }; + cache.writeQuery({ query, data: nextData }); + const nextProblem = randomProblemWithGridSize( completedProblem.grid.width, completedProblem.grid.height diff --git a/src/model/types.graphql b/src/model/types.graphql index b131673..fe3d48c 100644 --- a/src/model/types.graphql +++ b/src/model/types.graphql @@ -1,11 +1,11 @@ type Query { current_user: User! - recently_completed(limit: Int!): [ProblemSpec] } type User { suggestions: [GameLink] next: ProblemSpec + recentlyCompleted: [ProblemSpec] } type ProblemSpec {