Skip to content

Commit

Permalink
add to recentlyCompleted when problemCompleted
Browse files Browse the repository at this point in the history
- this updates the in-memory cached value of
  `recentlyCompleted` whenever the `problemCompleted`
  mutation is called
  • Loading branch information
mikemoraned committed Jun 28, 2020
1 parent e2863c0 commit a8fde5c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
38 changes: 37 additions & 1 deletion src/model/contexts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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: {
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/model/types.graphql
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
type Query {
current_user: User!
recently_completed(limit: Int!): [ProblemSpec]
}

type User {
suggestions: [GameLink]
next: ProblemSpec
recentlyCompleted: [ProblemSpec]
}

type ProblemSpec {
Expand Down

0 comments on commit a8fde5c

Please sign in to comment.