Skip to content

Commit 6426479

Browse files
committed
Modify components to support ionic fork
1 parent d25d910 commit 6426479

File tree

4 files changed

+97
-52
lines changed

4 files changed

+97
-52
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ node_modules
44
/data/schema.sql
55
/@app/graphql/index.*
66
/@app/client/.next
7+
**/build

@app/lib/src/withApollo.tsx

Lines changed: 71 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
import { getDataFromTree } from "@apollo/react-ssr";
2-
import { InMemoryCache } from "apollo-cache-inmemory";
2+
import { InMemoryCache, NormalizedCacheObject } from "apollo-cache-inmemory";
33
import { ApolloClient } from "apollo-client";
44
import { ApolloLink, split } from "apollo-link";
55
import { onError } from "apollo-link-error";
66
import { HttpLink } from "apollo-link-http";
77
import { WebSocketLink } from "apollo-link-ws";
88
import { getOperationAST } from "graphql";
9-
import withApolloBase from "next-with-apollo";
9+
import withApolloBase, { InitApolloOptions } from "next-with-apollo";
10+
import React from "react";
1011
import { SubscriptionClient } from "subscriptions-transport-ws";
1112
import ws from "ws";
1213

1314
import { GraphileApolloLink } from "./GraphileApolloLink";
1415

16+
interface WithApolloOptions {
17+
useNext?: boolean;
18+
rootUrl?: string;
19+
}
20+
1521
let wsClient: SubscriptionClient | null = null;
1622

1723
export function resetWebsocketConnection(): void {
@@ -29,18 +35,18 @@ function makeServerSideLink(req: any, res: any) {
2935
}
3036

3137
function makeClientSideLink(ROOT_URL: string) {
32-
const nextDataEl = document.getElementById("__NEXT_DATA__");
33-
if (!nextDataEl || !nextDataEl.textContent) {
34-
throw new Error("Cannot read from __NEXT_DATA__ element");
38+
const nextDataEl =
39+
typeof document !== "undefined" && document.getElementById("__NEXT_DATA__");
40+
const headers = {};
41+
if (nextDataEl && nextDataEl.textContent) {
42+
const data = JSON.parse(nextDataEl.textContent);
43+
headers["CSRF-Token"] = data.query.CSRF_TOKEN;
3544
}
36-
const data = JSON.parse(nextDataEl.textContent);
37-
const CSRF_TOKEN = data.query.CSRF_TOKEN;
3845
const httpLink = new HttpLink({
3946
uri: `${ROOT_URL}/graphql`,
40-
credentials: "same-origin",
41-
headers: {
42-
"CSRF-Token": CSRF_TOKEN,
43-
},
47+
credentials:
48+
process.env.NODE_ENV === "development" ? "include" : "same-origin",
49+
headers,
4450
});
4551
wsClient = new SubscriptionClient(
4652
`${ROOT_URL.replace(/^http/, "ws")}/graphql`,
@@ -65,47 +71,61 @@ function makeClientSideLink(ROOT_URL: string) {
6571
return mainLink;
6672
}
6773

68-
export const withApollo = withApolloBase(
69-
({ initialState, ctx }) => {
70-
const ROOT_URL = process.env.ROOT_URL;
71-
if (!ROOT_URL) {
72-
throw new Error("ROOT_URL envvar is not set");
73-
}
74+
const getApolloClient = (
75+
{ initialState, ctx }: InitApolloOptions<NormalizedCacheObject>,
76+
withApolloOptions?: WithApolloOptions
77+
): ApolloClient<NormalizedCacheObject> => {
78+
const ROOT_URL = process.env.ROOT_URL || withApolloOptions?.rootUrl;
79+
if (!ROOT_URL) {
80+
throw new Error("ROOT_URL envvar is not set");
81+
}
82+
83+
const onErrorLink = onError(({ graphQLErrors, networkError }) => {
84+
if (graphQLErrors)
85+
graphQLErrors.map(({ message, locations, path }) =>
86+
console.error(
87+
`[GraphQL error]: message: ${message}, location: ${JSON.stringify(
88+
locations
89+
)}, path: ${JSON.stringify(path)}`
90+
)
91+
);
92+
if (networkError) console.error(`[Network error]: ${networkError}`);
93+
});
7494

75-
const onErrorLink = onError(({ graphQLErrors, networkError }) => {
76-
if (graphQLErrors)
77-
graphQLErrors.map(({ message, locations, path }) =>
78-
console.error(
79-
`[GraphQL error]: message: ${message}, location: ${JSON.stringify(
80-
locations
81-
)}, path: ${JSON.stringify(path)}`
82-
)
83-
);
84-
if (networkError) console.error(`[Network error]: ${networkError}`);
85-
});
95+
const { req, res }: any = ctx || {};
96+
const isServer = typeof window === "undefined";
97+
const mainLink =
98+
isServer && req && res
99+
? makeServerSideLink(req, res)
100+
: makeClientSideLink(ROOT_URL);
86101

87-
const { req, res }: any = ctx || {};
88-
const isServer = typeof window === "undefined";
89-
const mainLink =
90-
isServer && req && res
91-
? makeServerSideLink(req, res)
92-
: makeClientSideLink(ROOT_URL);
102+
const client = new ApolloClient({
103+
link: ApolloLink.from([onErrorLink, mainLink]),
104+
cache: new InMemoryCache({
105+
dataIdFromObject: (o) =>
106+
o.__typename === "Query"
107+
? "ROOT_QUERY"
108+
: o.id
109+
? `${o.__typename}:${o.id}`
110+
: null,
111+
}).restore(initialState || {}),
112+
});
93113

94-
const client = new ApolloClient({
95-
link: ApolloLink.from([onErrorLink, mainLink]),
96-
cache: new InMemoryCache({
97-
dataIdFromObject: (o) =>
98-
o.__typename === "Query"
99-
? "ROOT_QUERY"
100-
: o.id
101-
? `${o.__typename}:${o.id}`
102-
: null,
103-
}).restore(initialState || {}),
104-
});
114+
return client;
115+
};
105116

106-
return client;
107-
},
108-
{
109-
getDataFromTree,
110-
}
111-
);
117+
const withApolloWithNext = withApolloBase(getApolloClient, {
118+
getDataFromTree,
119+
});
120+
121+
const withApolloWithoutNext = (Component: any, options?: WithApolloOptions) => (
122+
props: any
123+
) => {
124+
const apollo = getApolloClient({}, options);
125+
return <Component {...props} apollo={apollo} />;
126+
};
127+
128+
export const withApollo = (Component: any, options?: WithApolloOptions) =>
129+
options?.useNext === false
130+
? withApolloWithoutNext(Component, options)
131+
: withApolloWithNext(Component);

@app/server/src/middleware/installCSRFProtection.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
import csrf from "csurf";
22
import { Express } from "express";
3+
import url from "url";
4+
5+
const skipList = process.env.CSRF_SKIP_REFERERS
6+
? process.env.CSRF_SKIP_REFERERS?.replace(/s\s/g, "")
7+
.split(",")
8+
.map((s) => {
9+
// It is prefixed with a protocol
10+
if (s.indexOf("//") !== -1) {
11+
const { host: skipHost } = url.parse(s);
12+
return skipHost;
13+
}
14+
15+
return s;
16+
})
17+
: [];
318

419
export default (app: Express) => {
520
const csrfProtection = csrf({
@@ -21,6 +36,12 @@ export default (app: Express) => {
2136
) {
2237
// Bypass CSRF for GraphiQL
2338
next();
39+
} else if (
40+
skipList &&
41+
skipList.includes(url.parse(req.headers.referer || "").host)
42+
) {
43+
// Bypass CSRF for named referers
44+
next();
2445
} else {
2546
csrfProtection(req, res, next);
2647
}

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,10 @@
9292
"proseWrap": "always",
9393
"overrides": [
9494
{
95-
"files": ["*.yml", "*.yaml"],
95+
"files": [
96+
"*.yml",
97+
"*.yaml"
98+
],
9699
"options": {
97100
"printWidth": 120
98101
}

0 commit comments

Comments
 (0)