import axios from 'axios' ; import { DocumentNode , print } from 'graphql' ; import { introspectSchema , makeRemoteExecutableSchema } from 'graphql-tools' ; import getEnv from './getEnv' ; import getTokenPayload from './getTokenPayload' ; interface Operation { query : DocumentNode ; operationName ? : string ; variables ? : { [ key ] : any ; } ; context ? : { [ key ] : any ; } ; } async function makeGitHubSchema ( ) { return makeRemoteExecutableSchema ( { schema : await introspectSchema ( introspectionFetcher ) , fetcher , } ) ; } async function introspectionFetcher ( operation ) { const response = await axios ( { method : 'post' , url : 'https://api.github.com/graphql' , headers : { 'Content-Type' : 'application/json' , Authorization : `Bearer ${ getEnv ( 'GITHUB_TOKEN' ) }` , } , data : { query : print ( operation . query ) , variables : operation . variables , operationName : operation . operationName , } , } ) ; return response . data ; } async function fetcher ( operation ) { if ( ! operation . context ) throw new Error ( 'context is undefined.' ) ; const { gitHubToken } = getTokenPayload ( operation . context . graphqlContext ) ; const response = await axios ( { method : 'post' , url : 'https://api.github.com/graphql' , headers : { 'Content-Type' : 'application/json' , Authorization : `Bearer ${ gitHubToken }` , } , data : { query : print ( operation . query ) , variables : operation . variables , operationName : operation . operationName , } , } ) ; return response . data ; } export default makeGitHubSchema ;