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 {