|
| 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 | + |
1 | 6 | const translations = require("./src/utils/translations") |
2 | 7 | require("dotenv").config() |
3 | 8 |
|
4 | 9 | const supportedLanguages = translations.supportedLanguages |
5 | 10 | const defaultLanguage = `en` |
6 | 11 | const siteUrl = `https://ethereum.org` |
7 | 12 |
|
| 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 | + |
8 | 35 | // Note: to run this application locally you need to: |
9 | 36 | // 1. Create a .env file in the root directory (see .env.example) |
10 | 37 | // 2. Create a Github personal access token with `read:user` scope |
@@ -225,6 +252,19 @@ module.exports = { |
225 | 252 | typeName: `GitHub`, |
226 | 253 | fieldName: `github`, |
227 | 254 | 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 | + }, |
228 | 268 | headers: { |
229 | 269 | Authorization: `Bearer ${GITHUB_TOKEN_READ_ONLY}`, |
230 | 270 | }, |
|
0 commit comments