Skip to content

Commit 12b9310

Browse files
committed
Refactor Github API to use Apollo Link
1 parent ff5da29 commit 12b9310

File tree

3 files changed

+791
-641
lines changed

3 files changed

+791
-641
lines changed

gatsby-config.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,37 @@
1+
const { ApolloLink } = require(`apollo-link`)
2+
const { createHttpLink } = require(`apollo-link-http`)
3+
const { onError } = require("apollo-link-error")
4+
const fetch = require("node-fetch")
5+
16
const translations = require("./src/utils/translations")
27
require("dotenv").config()
38

49
const supportedLanguages = translations.supportedLanguages
510
const defaultLanguage = `en`
611
const siteUrl = `https://ethereum.org`
712

13+
const loggerLink = new ApolloLink((operation, forward) => {
14+
console.log(`GraphQL Request: ${operation.operationName}`)
15+
operation.setContext({ start: new Date() })
16+
return forward(operation).map((response) => {
17+
const responseTime = new Date() - operation.getContext().start
18+
console.log(`GraphQL Response took: ${responseTime}`)
19+
return response
20+
})
21+
})
22+
23+
const errorLink = onError(({ graphQLErrors, networkError }) => {
24+
if (graphQLErrors) {
25+
graphQLErrors.map(({ message, locations, path }) => {
26+
console.log(`GraphQL Error:`)
27+
console.log({ message, locations, path })
28+
})
29+
}
30+
if (networkError) {
31+
console.log(`Network Error: ${networkError.message}`)
32+
}
33+
})
34+
835
// Note: to run this application locally you need to:
936
// 1. Create a .env file in the root directory (see .env.example)
1037
// 2. Create a Github personal access token with `read:user` scope
@@ -225,6 +252,19 @@ module.exports = {
225252
typeName: `GitHub`,
226253
fieldName: `github`,
227254
url: `https://api.github.com/graphql`,
255+
createLink: (pluginOptions) => {
256+
return ApolloLink.from([
257+
loggerLink,
258+
errorLink,
259+
createHttpLink({
260+
uri: pluginOptions.url,
261+
fetch,
262+
headers: {
263+
Authorization: `Bearer ${GITHUB_TOKEN_READ_ONLY}`,
264+
},
265+
}),
266+
])
267+
},
228268
headers: {
229269
Authorization: `Bearer ${GITHUB_TOKEN_READ_ONLY}`,
230270
},

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@
1111
"@mdx-js/mdx": "^1.6.5",
1212
"@mdx-js/react": "^1.6.5",
1313
"algoliasearch": "^4.3.0",
14+
"apollo-link": "^1.2.14",
15+
"apollo-link-error": "^1.1.13",
16+
"apollo-link-http": "^1.5.17",
1417
"axios": "^0.21.1",
1518
"babel-plugin-styled-components": "^1.10.7",
1619
"clipboard": "^2.0.6",
1720
"dotenv": "^8.2.0",
1821
"ethereum-blockies-base64": "^1.0.2",
1922
"framer-motion": "^1.11.1",
20-
"gatsby": "^2.23.1",
23+
"gatsby": "^2.31.0",
2124
"gatsby-image": "^2.4.7",
2225
"gatsby-plugin-intl": "^0.3.3",
2326
"gatsby-plugin-lodash": "^3.3.11",
@@ -33,14 +36,15 @@
3336
"gatsby-remark-copy-linked-files": "^2.4.0",
3437
"gatsby-remark-images": "^3.3.12",
3538
"gatsby-source-filesystem": "^2.3.10",
36-
"gatsby-source-graphql": "^2.7.3",
39+
"gatsby-source-graphql": "^2.13.0",
3740
"gatsby-transformer-csv": "^2.3.10",
3841
"gatsby-transformer-gitinfo": "^1.1.0",
3942
"gatsby-transformer-remark": "^2.8.16",
4043
"gatsby-transformer-sharp": "^2.5.5",
4144
"lodash": "^4.17.20",
4245
"luxon": "^1.24.1",
4346
"netlify-lambda": "^1.6.3",
47+
"node-fetch": "^2.6.1",
4448
"polished": "^3.6.5",
4549
"prism-react-renderer": "^1.1.1",
4650
"prismjs": "^1.21.0",

0 commit comments

Comments
 (0)