Created
January 5, 2020 15:59
-
-
Save ztolley/4327ed8737b3e34c1bbafe3e542d1c43 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require("dotenv").config(); | |
const { setContext } = require("apollo-link-context"); | |
const { HttpLink } = require("apollo-link-http"); | |
const { ApolloServer } = require("apollo-server-express"); | |
const express = require("express"); | |
const bodyParser = require("body-parser"); | |
const { graphqlExpress } = require("apollo-server-express/dist/expressApollo"); | |
const { | |
makeRemoteExecutableSchema, | |
mergeSchemas, | |
introspectSchema | |
} = require("graphql-tools"); | |
const fetch = require("node-fetch"); | |
function getAuthContextLink() { | |
return setContext((request, previousContext) => { | |
console.log( | |
"Authorization:", | |
previousContext.graphqlContext.headers.authorization | |
); | |
return { | |
headers: { | |
authorization: previousContext.graphqlContext.headers.authorization | |
} | |
}; | |
}); | |
} | |
const createRemoteSchema = async (uri, settings) => { | |
const config = { uri: uri, fetch, ...settings }; | |
try { | |
const link = new HttpLink(config); | |
const authLink = getAuthContextLink().concat(link); | |
const schema = await introspectSchema(link); | |
return makeRemoteExecutableSchema({ | |
schema, | |
link: authLink | |
}); | |
} catch (error) { | |
console.log(error); | |
} | |
}; | |
const { FBR_API, NELSON_API } = process.env; | |
console.log("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"); | |
console.log(FBR_API); | |
console.log(NELSON_API); | |
console.log("--------------------------------"); | |
async function run() { | |
const nelsonApi = await createRemoteSchema(NELSON_API); | |
const fbrApi = await createRemoteSchema(FBR_API); | |
// Extend the FBR Change object to associate it with a Ship | |
const linkTypeDefs = ` | |
extend type Change { | |
ship: Ship | |
} | |
`; | |
const schema = mergeSchemas({ | |
schemas: [nelsonApi, fbrApi, linkTypeDefs], | |
// Resolver to fetch the ship associated with a change | |
resolvers: { | |
Change: { | |
ship: { | |
fragment: `... on Change { shipId }`, | |
resolve(response, args, context, info) { | |
return info.mergeInfo.delegateToSchema({ | |
schema: nelsonApi, | |
operation: "query", | |
fieldName: "ship", | |
args: { | |
id: response.shipId | |
}, | |
context, | |
info, | |
transforms: fbrApi.transforms | |
}); | |
} | |
} | |
} | |
} | |
}); | |
const port = process.env.SERVER_PORT || 8000; | |
const app = express(); | |
app.use( | |
"/graphql", | |
bodyParser.json(), | |
graphqlExpress(async request => ({ | |
schema, | |
context: { headers: request ? request.headers : null } | |
})) | |
); | |
app.listen(port, () => | |
console.log(`🚀 Gateway Server ready on port ${port}!`) | |
); | |
} | |
try { | |
console.log("get ready"); | |
run(); | |
} catch (e) { | |
console.log(e); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment