Skip to content

Commit

Permalink
invalidate-query
Browse files Browse the repository at this point in the history
  • Loading branch information
benawad committed Aug 15, 2020
1 parent 4852f74 commit 2f3ca44
Showing 4 changed files with 32 additions and 3 deletions.
16 changes: 16 additions & 0 deletions web/src/generated/graphql.tsx
Original file line number Diff line number Diff line change
@@ -44,6 +44,7 @@ export type Post = {
text: Scalars['String'];
points: Scalars['Float'];
creatorId: Scalars['Float'];
creator: User;
createdAt: Scalars['String'];
updatedAt: Scalars['String'];
textSnippet: Scalars['String'];
@@ -60,6 +61,7 @@ export type User = {

export type Mutation = {
__typename?: 'Mutation';
vote: Scalars['Boolean'];
createPost: Post;
updatePost?: Maybe<Post>;
deletePost: Scalars['Boolean'];
@@ -71,6 +73,12 @@ export type Mutation = {
};


export type MutationVoteArgs = {
value: Scalars['Int'];
postId: Scalars['Int'];
};


export type MutationCreatePostArgs = {
input: PostInput;
};
@@ -249,6 +257,10 @@ export type PostsQuery = (
& { posts: Array<(
{ __typename?: 'Post' }
& Pick<Post, 'id' | 'createdAt' | 'updatedAt' | 'title' | 'textSnippet'>
& { creator: (
{ __typename?: 'User' }
& Pick<User, 'id' | 'username'>
) }
)> }
) }
);
@@ -365,6 +377,10 @@ export const PostsDocument = gql`
updatedAt
title
textSnippet
creator {
id
username
}
}
}
}
4 changes: 4 additions & 0 deletions web/src/graphql/queries/posts.graphql
Original file line number Diff line number Diff line change
@@ -7,6 +7,10 @@ query Posts($limit: Int!, $cursor: String) {
updatedAt
title
textSnippet
creator {
id
username
}
}
}
}
5 changes: 3 additions & 2 deletions web/src/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ import { useState } from "react";

const Index = () => {
const [variables, setVariables] = useState({
limit: 33,
limit: 15,
cursor: null as null | string,
});

@@ -35,7 +35,8 @@ const Index = () => {
<Stack spacing={8}>
{data!.posts.posts.map((p) => (
<Box key={p.id} p={5} shadow="md" borderWidth="1px">
<Heading fontSize="xl">{p.title}</Heading>
<Heading fontSize="xl">{p.title}</Heading>{" "}
<Text>posted by {p.creator.username}</Text>
<Text mt={4}>{p.textSnippet}</Text>
</Box>
))}
10 changes: 9 additions & 1 deletion web/src/utils/createUrqlClient.ts
Original file line number Diff line number Diff line change
@@ -31,7 +31,6 @@ const cursorPagination = (): Resolver => {
return (_parent, fieldArgs, cache, info) => {
const { parentKey: entityKey, fieldName } = info;
const allFields = cache.inspectFields(entityKey);
console.log("allFields: ", allFields);
const fieldInfos = allFields.filter((info) => info.fieldName === fieldName);
const size = fieldInfos.length;
if (size === 0) {
@@ -134,6 +133,15 @@ export const createUrqlClient = (ssrExchange: any) => ({
},
updates: {
Mutation: {
createPost: (_result, args, cache, info) => {
const allFields = cache.inspectFields("Query");
const fieldInfos = allFields.filter(
(info) => info.fieldName === "posts"
);
fieldInfos.forEach((fi) => {
cache.invalidate("Query", "posts", fi.arguments || {});
});
},
logout: (_result, args, cache, info) => {
betterUpdateQuery<LogoutMutation, MeQuery>(
cache,

0 comments on commit 2f3ca44

Please sign in to comment.