Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6b7966e

Browse files
committed
GitHunt-API branch and GitHunt-React now at the point where the SSR renders through persisted queries with extractgql
1 parent 9c11408 commit 6b7966e

File tree

3 files changed

+50
-38
lines changed

3 files changed

+50
-38
lines changed

api/index.js

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ import { subscriptionManager } from './subscriptions';
1919

2020
import schema from './schema';
2121

22+
import {
23+
createPersistedQueryMiddleware,
24+
} from 'extractgql/lib/src/server/serverUtil';
25+
2226
let PORT = 3010;
2327
if (process.env.PORT) {
2428
PORT = parseInt(process.env.PORT, 10) + 100;
@@ -33,46 +37,52 @@ app.use(bodyParser.json());
3337

3438
setUpGitHubLogin(app);
3539

36-
app.use('/graphql', graphqlExpress((req) => {
37-
// Get the query, the same way express-graphql does it
38-
// https://github.com/graphql/express-graphql/blob/3fa6e68582d6d933d37fa9e841da5d2aa39261cd/src/index.js#L257
39-
const query = req.query.query || req.body.query;
40-
if (query && query.length > 2000) {
41-
// None of our app's queries are this long
42-
// Probably indicates someone trying to send an overly expensive query
43-
throw new Error('Query too large.');
44-
}
40+
console.log('Creating middleware.');
41+
createPersistedQueryMiddleware('./extracted_queries.json').then((middleware) => {
42+
app.use('/graphql', middleware);
43+
console.log('Created middleware and used it as the middleware.');
44+
45+
app.use('/graphql', graphqlExpress((req) => {
46+
// Get the query, the same way express-graphql does it
47+
// https://github.com/graphql/express-graphql/blob/3fa6e68582d6d933d37fa9e841da5d2aa39261cd/src/index.js#L257
48+
const query = req.query.query || req.body.query;
49+
if (query && query.length > 2000) {
50+
// None of our app's queries are this long
51+
// Probably indicates someone trying to send an overly expensive query
52+
throw new Error('Query too large.');
53+
}
4554

46-
let user;
47-
if (req.user) {
48-
// We get req.user from passport-github with some pretty oddly named fields,
49-
// let's convert that to the fields in our schema, which match the GitHub
50-
// API field names.
51-
user = {
52-
login: req.user.username,
53-
html_url: req.user.profileUrl,
54-
avatar_url: req.user.photos[0].value,
55-
};
56-
}
55+
let user;
56+
if (req.user) {
57+
// We get req.user from passport-github with some pretty oddly named fields,
58+
// let's convert that to the fields in our schema, which match the GitHub
59+
// API field names.
60+
user = {
61+
login: req.user.username,
62+
html_url: req.user.profileUrl,
63+
avatar_url: req.user.photos[0].value,
64+
};
65+
}
5766

58-
// Initialize a new GitHub connector instance for every GraphQL request, so that API fetches
59-
// are deduplicated per-request only.
60-
const gitHubConnector = new GitHubConnector({
61-
clientId: GITHUB_CLIENT_ID,
62-
clientSecret: GITHUB_CLIENT_SECRET,
63-
});
64-
65-
return {
66-
schema,
67-
context: {
68-
user,
69-
Repositories: new Repositories({ connector: gitHubConnector }),
70-
Users: new Users({ connector: gitHubConnector }),
71-
Entries: new Entries(),
72-
Comments: new Comments(),
73-
},
74-
};
75-
}));
67+
// Initialize a new GitHub connector instance for every GraphQL request, so that API fetches
68+
// are deduplicated per-request only.
69+
const gitHubConnector = new GitHubConnector({
70+
clientId: GITHUB_CLIENT_ID,
71+
clientSecret: GITHUB_CLIENT_SECRET,
72+
});
73+
74+
return {
75+
schema,
76+
context: {
77+
user,
78+
Repositories: new Repositories({ connector: gitHubConnector }),
79+
Users: new Users({ connector: gitHubConnector }),
80+
Entries: new Entries(),
81+
Comments: new Comments(),
82+
},
83+
};
84+
}));
85+
});
7686

7787
app.use('/graphiql', graphiqlExpress({
7888
endpointURL: '/graphql',

extracted_queries.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"query Comment($repoName: String!) {\n currentUser {\n login\n html_url\n __typename\n }\n entry(repoFullName: $repoName) {\n id\n postedBy {\n login\n html_url\n __typename\n }\n createdAt\n comments {\n ...CommentsPageComment\n __typename\n }\n repository {\n full_name\n html_url\n description\n open_issues_count\n stargazers_count\n __typename\n }\n __typename\n }\n}":{"transformedQuery":{"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"Comment","loc":{"start":6,"end":13}},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"repoName","loc":{"start":15,"end":23}},"loc":{"start":14,"end":23}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"String","loc":{"start":25,"end":31}},"loc":{"start":25,"end":31}},"loc":{"start":25,"end":32}},"defaultValue":null,"loc":{"start":14,"end":32}}],"directives":[],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","alias":null,"name":{"kind":"Name","value":"currentUser","loc":{"start":38,"end":49}},"arguments":[],"directives":[],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","alias":null,"name":{"kind":"Name","value":"login","loc":{"start":56,"end":61}},"arguments":[],"directives":[],"selectionSet":null,"loc":{"start":56,"end":61}},{"kind":"Field","alias":null,"name":{"kind":"Name","value":"html_url","loc":{"start":66,"end":74}},"arguments":[],"directives":[],"selectionSet":null,"loc":{"start":66,"end":74}},{"kind":"Field","alias":null,"name":{"kind":"Name","value":"__typename"}}],"loc":{"start":50,"end":78}},"loc":{"start":38,"end":78}},{"kind":"Field","alias":null,"name":{"kind":"Name","value":"entry","loc":{"start":81,"end":86}},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"repoFullName","loc":{"start":87,"end":99}},"value":{"kind":"Variable","name":{"kind":"Name","value":"repoName","loc":{"start":102,"end":110}},"loc":{"start":101,"end":110}},"loc":{"start":87,"end":110}}],"directives":[],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","alias":null,"name":{"kind":"Name","value":"id","loc":{"start":118,"end":120}},"arguments":[],"directives":[],"selectionSet":null,"loc":{"start":118,"end":120}},{"kind":"Field","alias":null,"name":{"kind":"Name","value":"postedBy","loc":{"start":125,"end":133}},"arguments":[],"directives":[],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","alias":null,"name":{"kind":"Name","value":"login","loc":{"start":142,"end":147}},"arguments":[],"directives":[],"selectionSet":null,"loc":{"start":142,"end":147}},{"kind":"Field","alias":null,"name":{"kind":"Name","value":"html_url","loc":{"start":154,"end":162}},"arguments":[],"directives":[],"selectionSet":null,"loc":{"start":154,"end":162}},{"kind":"Field","alias":null,"name":{"kind":"Name","value":"__typename"}}],"loc":{"start":134,"end":168}},"loc":{"start":125,"end":168}},{"kind":"Field","alias":null,"name":{"kind":"Name","value":"createdAt","loc":{"start":173,"end":182}},"arguments":[],"directives":[],"selectionSet":null,"loc":{"start":173,"end":182}},{"kind":"Field","alias":null,"name":{"kind":"Name","value":"comments","loc":{"start":187,"end":195}},"arguments":[],"directives":[],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"FragmentSpread","name":{"kind":"Name","value":"CommentsPageComment","loc":{"start":207,"end":226}},"directives":[],"loc":{"start":204,"end":226}},{"kind":"Field","alias":null,"name":{"kind":"Name","value":"__typename"}}],"loc":{"start":196,"end":232}},"loc":{"start":187,"end":232}},{"kind":"Field","alias":null,"name":{"kind":"Name","value":"repository","loc":{"start":237,"end":247}},"arguments":[],"directives":[],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","alias":null,"name":{"kind":"Name","value":"full_name","loc":{"start":256,"end":265}},"arguments":[],"directives":[],"selectionSet":null,"loc":{"start":256,"end":265}},{"kind":"Field","alias":null,"name":{"kind":"Name","value":"html_url","loc":{"start":272,"end":280}},"arguments":[],"directives":[],"selectionSet":null,"loc":{"start":272,"end":280}},{"kind":"Field","alias":null,"name":{"kind":"Name","value":"description","loc":{"start":287,"end":298}},"arguments":[],"directives":[],"selectionSet":null,"loc":{"start":287,"end":298}},{"kind":"Field","alias":null,"name":{"kind":"Name","value":"open_issues_count","loc":{"start":305,"end":322}},"arguments":[],"directives":[],"selectionSet":null,"loc":{"start":305,"end":322}},{"kind":"Field","alias":null,"name":{"kind":"Name","value":"stargazers_count","loc":{"start":329,"end":345}},"arguments":[],"directives":[],"selectionSet":null,"loc":{"start":329,"end":345}},{"kind":"Field","alias":null,"name":{"kind":"Name","value":"__typename"}}],"loc":{"start":248,"end":351}},"loc":{"start":237,"end":351}},{"kind":"Field","alias":null,"name":{"kind":"Name","value":"__typename"}}],"loc":{"start":112,"end":355}},"loc":{"start":81,"end":355}}],"loc":{"start":34,"end":357}},"loc":{"start":0,"end":357}},{"kind":"FragmentDefinition","name":{"kind":"Name","value":"CommentsPageComment","loc":{"start":368,"end":387}},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"Comment","loc":{"start":391,"end":398}},"loc":{"start":391,"end":398}},"directives":[],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","alias":null,"name":{"kind":"Name","value":"id","loc":{"start":403,"end":405}},"arguments":[],"directives":[],"selectionSet":null,"loc":{"start":403,"end":405}},{"kind":"Field","alias":null,"name":{"kind":"Name","value":"postedBy","loc":{"start":408,"end":416}},"arguments":[],"directives":[],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","alias":null,"name":{"kind":"Name","value":"login","loc":{"start":423,"end":428}},"arguments":[],"directives":[],"selectionSet":null,"loc":{"start":423,"end":428}},{"kind":"Field","alias":null,"name":{"kind":"Name","value":"html_url","loc":{"start":433,"end":441}},"arguments":[],"directives":[],"selectionSet":null,"loc":{"start":433,"end":441}},{"kind":"Field","alias":null,"name":{"kind":"Name","value":"__typename"}}],"loc":{"start":417,"end":445}},"loc":{"start":408,"end":445}},{"kind":"Field","alias":null,"name":{"kind":"Name","value":"createdAt","loc":{"start":448,"end":457}},"arguments":[],"directives":[],"selectionSet":null,"loc":{"start":448,"end":457}},{"kind":"Field","alias":null,"name":{"kind":"Name","value":"content","loc":{"start":460,"end":467}},"arguments":[],"directives":[],"selectionSet":null,"loc":{"start":460,"end":467}}],"loc":{"start":399,"end":469}},"loc":{"start":359,"end":469}}]},"id":1},"query Feed($type: FeedType!, $offset: Int, $limit: Int) {\n currentUser {\n login\n __typename\n }\n feed(type: $type, offset: $offset, limit: $limit) {\n ...FeedEntry\n __typename\n }\n}":{"transformedQuery":{"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"Feed","loc":{"start":6,"end":10}},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"type","loc":{"start":12,"end":16}},"loc":{"start":11,"end":16}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"FeedType","loc":{"start":18,"end":26}},"loc":{"start":18,"end":26}},"loc":{"start":18,"end":27}},"defaultValue":null,"loc":{"start":11,"end":27}},{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"offset","loc":{"start":30,"end":36}},"loc":{"start":29,"end":36}},"type":{"kind":"NamedType","name":{"kind":"Name","value":"Int","loc":{"start":38,"end":41}},"loc":{"start":38,"end":41}},"defaultValue":null,"loc":{"start":29,"end":41}},{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"limit","loc":{"start":44,"end":49}},"loc":{"start":43,"end":49}},"type":{"kind":"NamedType","name":{"kind":"Name","value":"Int","loc":{"start":51,"end":54}},"loc":{"start":51,"end":54}},"defaultValue":null,"loc":{"start":43,"end":54}}],"directives":[],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","alias":null,"name":{"kind":"Name","value":"currentUser","loc":{"start":226,"end":237}},"arguments":[],"directives":[],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","alias":null,"name":{"kind":"Name","value":"login","loc":{"start":244,"end":249}},"arguments":[],"directives":[],"selectionSet":null,"loc":{"start":244,"end":249}},{"kind":"Field","alias":null,"name":{"kind":"Name","value":"__typename"}}],"loc":{"start":238,"end":253}},"loc":{"start":226,"end":253}},{"kind":"Field","alias":null,"name":{"kind":"Name","value":"feed","loc":{"start":256,"end":260}},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"type","loc":{"start":261,"end":265}},"value":{"kind":"Variable","name":{"kind":"Name","value":"type","loc":{"start":268,"end":272}},"loc":{"start":267,"end":272}},"loc":{"start":261,"end":272}},{"kind":"Argument","name":{"kind":"Name","value":"offset","loc":{"start":274,"end":280}},"value":{"kind":"Variable","name":{"kind":"Name","value":"offset","loc":{"start":283,"end":289}},"loc":{"start":282,"end":289}},"loc":{"start":274,"end":289}},{"kind":"Argument","name":{"kind":"Name","value":"limit","loc":{"start":291,"end":296}},"value":{"kind":"Variable","name":{"kind":"Name","value":"limit","loc":{"start":299,"end":304}},"loc":{"start":298,"end":304}},"loc":{"start":291,"end":304}}],"directives":[],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"FragmentSpread","name":{"kind":"Name","value":"FeedEntry","loc":{"start":315,"end":324}},"directives":[],"loc":{"start":312,"end":324}},{"kind":"Field","alias":null,"name":{"kind":"Name","value":"__typename"}}],"loc":{"start":306,"end":328}},"loc":{"start":256,"end":328}}],"loc":{"start":56,"end":330}},"loc":{"start":0,"end":330}},{"kind":"FragmentDefinition","name":{"kind":"Name","value":"FeedEntry","loc":{"start":341,"end":350}},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"Entry","loc":{"start":354,"end":359}},"loc":{"start":354,"end":359}},"directives":[],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","alias":null,"name":{"kind":"Name","value":"id","loc":{"start":364,"end":366}},"arguments":[],"directives":[],"selectionSet":null,"loc":{"start":364,"end":366}},{"kind":"Field","alias":null,"name":{"kind":"Name","value":"commentCount","loc":{"start":369,"end":381}},"arguments":[],"directives":[],"selectionSet":null,"loc":{"start":369,"end":381}},{"kind":"Field","alias":null,"name":{"kind":"Name","value":"repository","loc":{"start":384,"end":394}},"arguments":[],"directives":[],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","alias":null,"name":{"kind":"Name","value":"full_name","loc":{"start":401,"end":410}},"arguments":[],"directives":[],"selectionSet":null,"loc":{"start":401,"end":410}},{"kind":"Field","alias":null,"name":{"kind":"Name","value":"html_url","loc":{"start":415,"end":423}},"arguments":[],"directives":[],"selectionSet":null,"loc":{"start":415,"end":423}},{"kind":"Field","alias":null,"name":{"kind":"Name","value":"owner","loc":{"start":428,"end":433}},"arguments":[],"directives":[],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","alias":null,"name":{"kind":"Name","value":"avatar_url","loc":{"start":442,"end":452}},"arguments":[],"directives":[],"selectionSet":null,"loc":{"start":442,"end":452}},{"kind":"Field","alias":null,"name":{"kind":"Name","value":"__typename"}}],"loc":{"start":434,"end":458}},"loc":{"start":428,"end":458}},{"kind":"Field","alias":null,"name":{"kind":"Name","value":"__typename"}}],"loc":{"start":395,"end":462}},"loc":{"start":384,"end":462}},{"kind":"FragmentSpread","name":{"kind":"Name","value":"VoteButtons","loc":{"start":468,"end":479}},"directives":[],"loc":{"start":465,"end":479}},{"kind":"FragmentSpread","name":{"kind":"Name","value":"RepoInfo","loc":{"start":485,"end":493}},"directives":[],"loc":{"start":482,"end":493}}],"loc":{"start":360,"end":495}},"loc":{"start":332,"end":495}},{"kind":"FragmentDefinition","name":{"kind":"Name","value":"VoteButtons","loc":{"start":506,"end":517}},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"Entry","loc":{"start":521,"end":526}},"loc":{"start":521,"end":526}},"directives":[],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","alias":null,"name":{"kind":"Name","value":"score","loc":{"start":531,"end":536}},"arguments":[],"directives":[],"selectionSet":null,"loc":{"start":531,"end":536}},{"kind":"Field","alias":null,"name":{"kind":"Name","value":"vote","loc":{"start":539,"end":543}},"arguments":[],"directives":[],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","alias":null,"name":{"kind":"Name","value":"vote_value","loc":{"start":550,"end":560}},"arguments":[],"directives":[],"selectionSet":null,"loc":{"start":550,"end":560}},{"kind":"Field","alias":null,"name":{"kind":"Name","value":"__typename"}}],"loc":{"start":544,"end":564}},"loc":{"start":539,"end":564}}],"loc":{"start":527,"end":566}},"loc":{"start":497,"end":566}},{"kind":"FragmentDefinition","name":{"kind":"Name","value":"RepoInfo","loc":{"start":577,"end":585}},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"Entry","loc":{"start":589,"end":594}},"loc":{"start":589,"end":594}},"directives":[],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","alias":null,"name":{"kind":"Name","value":"createdAt","loc":{"start":599,"end":608}},"arguments":[],"directives":[],"selectionSet":null,"loc":{"start":599,"end":608}},{"kind":"Field","alias":null,"name":{"kind":"Name","value":"repository","loc":{"start":611,"end":621}},"arguments":[],"directives":[],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","alias":null,"name":{"kind":"Name","value":"description","loc":{"start":628,"end":639}},"arguments":[],"directives":[],"selectionSet":null,"loc":{"start":628,"end":639}},{"kind":"Field","alias":null,"name":{"kind":"Name","value":"stargazers_count","loc":{"start":644,"end":660}},"arguments":[],"directives":[],"selectionSet":null,"loc":{"start":644,"end":660}},{"kind":"Field","alias":null,"name":{"kind":"Name","value":"open_issues_count","loc":{"start":665,"end":682}},"arguments":[],"directives":[],"selectionSet":null,"loc":{"start":665,"end":682}},{"kind":"Field","alias":null,"name":{"kind":"Name","value":"__typename"}}],"loc":{"start":622,"end":686}},"loc":{"start":611,"end":686}},{"kind":"Field","alias":null,"name":{"kind":"Name","value":"postedBy","loc":{"start":689,"end":697}},"arguments":[],"directives":[],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","alias":null,"name":{"kind":"Name","value":"html_url","loc":{"start":704,"end":712}},"arguments":[],"directives":[],"selectionSet":null,"loc":{"start":704,"end":712}},{"kind":"Field","alias":null,"name":{"kind":"Name","value":"login","loc":{"start":717,"end":722}},"arguments":[],"directives":[],"selectionSet":null,"loc":{"start":717,"end":722}},{"kind":"Field","alias":null,"name":{"kind":"Name","value":"__typename"}}],"loc":{"start":698,"end":726}},"loc":{"start":689,"end":726}}],"loc":{"start":595,"end":728}},"loc":{"start":568,"end":728}}]},"id":2},"query CurrentUserForLayout {\n currentUser {\n login\n avatar_url\n __typename\n }\n}":{"transformedQuery":{"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"CurrentUserForLayout","loc":{"start":6,"end":26}},"variableDefinitions":[],"directives":[],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","alias":null,"name":{"kind":"Name","value":"currentUser","loc":{"start":31,"end":42}},"arguments":[],"directives":[],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","alias":null,"name":{"kind":"Name","value":"login","loc":{"start":49,"end":54}},"arguments":[],"directives":[],"selectionSet":null,"loc":{"start":49,"end":54}},{"kind":"Field","alias":null,"name":{"kind":"Name","value":"avatar_url","loc":{"start":59,"end":69}},"arguments":[],"directives":[],"selectionSet":null,"loc":{"start":59,"end":69}},{"kind":"Field","alias":null,"name":{"kind":"Name","value":"__typename"}}],"loc":{"start":43,"end":73}},"loc":{"start":31,"end":73}}],"loc":{"start":27,"end":75}},"loc":{"start":0,"end":75}}]},"id":3}}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"dotenv": "2.0.0",
4747
"express": "4.14.0",
4848
"express-session": "1.14.2",
49+
"extractgql": "^0.1.0",
4950
"graphql": "0.7.2",
5051
"graphql-server-express": "^0.4.1",
5152
"graphql-subscriptions": "0.2.2",

0 commit comments

Comments
 (0)