Skip to content

Commit

Permalink
ssr-cookie
Browse files Browse the repository at this point in the history
  • Loading branch information
benawad committed Aug 15, 2020
1 parent 36da2e6 commit 34ef721
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 95 deletions.
10 changes: 8 additions & 2 deletions server/src/resolvers/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,16 @@ export class PostResolver {
const realLimit = Math.min(50, limit);
const reaLimitPlusOne = realLimit + 1;

const replacements: any[] = [reaLimitPlusOne, req.session.userId];
const replacements: any[] = [reaLimitPlusOne];

if (req.session.userId) {
replacements.push(req.session.userId);
}

let cursorIdx = 3;
if (cursor) {
replacements.push(new Date(parseInt(cursor)));
cursorIdx = replacements.length;
}

const posts = await getConnection().query(
Expand All @@ -135,7 +141,7 @@ export class PostResolver {
}
from post p
inner join public.user u on u.id = p."creatorId"
${cursor ? `where p."createdAt" < $3` : ""}
${cursor ? `where p."createdAt" < $${cursorIdx}` : ""}
order by p."createdAt" DESC
limit $1
`,
Expand Down
4 changes: 4 additions & 0 deletions server/ssr-cookie.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ssr
browser -> next.js -> graphql api
client side
browser -> graphql api
199 changes: 106 additions & 93 deletions web/src/utils/createUrqlClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
import { betterUpdateQuery } from "./betterUpdateQuery";
import Router from "next/router";
import gql from "graphql-tag";
import { isServer } from "./isServer";

const errorExchange: Exchange = ({ forward }) => (ops$) => {
return pipe(
Expand Down Expand Up @@ -117,108 +118,120 @@ const cursorPagination = (): Resolver => {
};
};

export const createUrqlClient = (ssrExchange: any) => ({
url: "http://localhost:4000/graphql",
fetchOptions: {
credentials: "include" as const,
},
exchanges: [
dedupExchange,
cacheExchange({
keys: {
PaginatedPosts: () => null,
},
resolvers: {
Query: {
posts: cursorPagination(),
},
},
updates: {
Mutation: {
vote: (_result, args, cache, info) => {
const { postId, value } = args as VoteMutationVariables;
const data = cache.readFragment(
gql`
fragment _ on Post {
id
points
voteStatus
}
`,
{ id: postId } as any
);
export const createUrqlClient = (ssrExchange: any, ctx: any) => {
let cookie = "";
if (isServer()) {
cookie = ctx.req.headers.cookie;
}

if (data) {
if (data.voteStatus === value) {
return;
}
const newPoints =
(data.points as number) + (!data.voteStatus ? 1 : 2) * value;
cache.writeFragment(
return {
url: "http://localhost:4000/graphql",
fetchOptions: {
credentials: "include" as const,
headers: cookie
? {
cookie,
}
: undefined,
},
exchanges: [
dedupExchange,
cacheExchange({
keys: {
PaginatedPosts: () => null,
},
resolvers: {
Query: {
posts: cursorPagination(),
},
},
updates: {
Mutation: {
vote: (_result, args, cache, info) => {
const { postId, value } = args as VoteMutationVariables;
const data = cache.readFragment(
gql`
fragment __ on Post {
fragment _ on Post {
id
points
voteStatus
}
`,
{ id: postId, points: newPoints, voteStatus: value } as any
{ id: postId } as any
);
}
},
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,
{ query: MeDocument },
_result,
() => ({ me: null })
);
},
login: (_result, args, cache, info) => {
betterUpdateQuery<LoginMutation, MeQuery>(
cache,
{ query: MeDocument },
_result,
(result, query) => {
if (result.login.errors) {
return query;
} else {
return {
me: result.login.user,
};

if (data) {
if (data.voteStatus === value) {
return;
}
const newPoints =
(data.points as number) + (!data.voteStatus ? 1 : 2) * value;
cache.writeFragment(
gql`
fragment __ on Post {
points
voteStatus
}
`,
{ id: postId, points: newPoints, voteStatus: value } as any
);
}
);
},
register: (_result, args, cache, info) => {
betterUpdateQuery<RegisterMutation, MeQuery>(
cache,
{ query: MeDocument },
_result,
(result, query) => {
if (result.register.errors) {
return query;
} else {
return {
me: result.register.user,
};
},
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,
{ query: MeDocument },
_result,
() => ({ me: null })
);
},
login: (_result, args, cache, info) => {
betterUpdateQuery<LoginMutation, MeQuery>(
cache,
{ query: MeDocument },
_result,
(result, query) => {
if (result.login.errors) {
return query;
} else {
return {
me: result.login.user,
};
}
}
}
);
);
},
register: (_result, args, cache, info) => {
betterUpdateQuery<RegisterMutation, MeQuery>(
cache,
{ query: MeDocument },
_result,
(result, query) => {
if (result.register.errors) {
return query;
} else {
return {
me: result.register.user,
};
}
}
);
},
},
},
},
}),
errorExchange,
ssrExchange,
fetchExchange,
],
});
}),
errorExchange,
ssrExchange,
fetchExchange,
],
};
};

0 comments on commit 34ef721

Please sign in to comment.