a bit of refactoring
This commit is contained in:
1 parent
2ae9cf9ef6
commit
73824d4303
3 files changed
+56
-52
No files matched your search
@@ -0,0 +1,46 @@
|
||||
import fetch from 'node-fetch'
|
||||
import { createHttpLink } from 'apollo-link-http'
|
||||
import { setContext } from 'apollo-link-context'
|
||||
import {
|
||||
makeRemoteExecutableSchema,
|
||||
introspectSchema,
|
||||
transformSchema,
|
||||
RenameTypes,
|
||||
RenameRootFields
|
||||
} from 'graphql-tools'
|
||||
|
||||
import { capitalize } from './utils'
|
||||
|
||||
export const createRemoteSchema = async (uri) => {
|
||||
const makeDatabaseServiceLink = () => createHttpLink({
|
||||
uri: uri,
|
||||
fetch
|
||||
})
|
||||
const databaseServiceSchemaDefinition = await introspectSchema(makeDatabaseServiceLink())
|
||||
|
||||
const http = makeDatabaseServiceLink()
|
||||
|
||||
const link = setContext((request, previousContext) => (
|
||||
{
|
||||
headers: {
|
||||
authorization: previousContext.graphqlContext.headers.authorization
|
||||
}
|
||||
}
|
||||
)).concat(http)
|
||||
|
||||
return makeRemoteExecutableSchema({
|
||||
schema: databaseServiceSchemaDefinition,
|
||||
link: link
|
||||
})
|
||||
}
|
||||
|
||||
export const createTransformedRemoteSchema = async (prefix, uri) => {
|
||||
const schema = await createRemoteSchema(uri)
|
||||
const transformedSchema = transformSchema(schema, [
|
||||
new RenameTypes((name) => prefix + capitalize(name)),
|
||||
new RenameRootFields((operation, name) => prefix + capitalize(name))
|
||||
])
|
||||
return transformedSchema
|
||||
}
|
||||
|
||||
export default createRemoteSchema
|
||||
+5
-52
@@ -2,60 +2,13 @@ import express from 'express'
|
||||
import bodyParser from 'body-parser'
|
||||
import 'dotenv/config'
|
||||
import { graphqlExpress, graphiqlExpress } from 'apollo-server-express'
|
||||
import {
|
||||
makeRemoteExecutableSchema,
|
||||
mergeSchemas,
|
||||
introspectSchema,
|
||||
transformSchema,
|
||||
RenameTypes,
|
||||
RenameRootFields
|
||||
} from 'graphql-tools'
|
||||
import fetch from 'node-fetch'
|
||||
import { createHttpLink } from 'apollo-link-http'
|
||||
import { setContext } from 'apollo-link-context'
|
||||
import { mergeSchemas } from 'graphql-tools'
|
||||
import { createTransformedRemoteSchema } from './createRemoteSchema'
|
||||
|
||||
async function run () {
|
||||
const createRemoteSchema = async (uri) => {
|
||||
const makeDatabaseServiceLink = () => createHttpLink({
|
||||
uri: uri,
|
||||
fetch
|
||||
})
|
||||
const databaseServiceSchemaDefinition = await introspectSchema(makeDatabaseServiceLink())
|
||||
|
||||
const http = makeDatabaseServiceLink()
|
||||
|
||||
const link = setContext((request, previousContext) => (
|
||||
{ headers: previousContext.graphqlContext.headers }
|
||||
)).concat(http)
|
||||
|
||||
return makeRemoteExecutableSchema({
|
||||
schema: databaseServiceSchemaDefinition,
|
||||
link: link
|
||||
})
|
||||
}
|
||||
|
||||
const teamsSchema = await createRemoteSchema(process.env.TEAMS_API_URL)
|
||||
const usersSchema = await createRemoteSchema(process.env.USERS_API_URL)
|
||||
const keyVaultSchema = await createRemoteSchema(process.env.KEYVAULT_API_URL)
|
||||
|
||||
const capitalize = (string) => {
|
||||
return (string).charAt(0).toUpperCase() + (string).slice(1)
|
||||
}
|
||||
|
||||
const transformedTeamsSchema = transformSchema(teamsSchema, [
|
||||
new RenameTypes((name) => `TeamsModule${capitalize(name)}`),
|
||||
new RenameRootFields((operation, name) => `TeamsModule${capitalize(name)}`)
|
||||
])
|
||||
|
||||
const transformedUsersSchema = transformSchema(usersSchema, [
|
||||
new RenameTypes((name) => `UsersModule${capitalize(name)}`),
|
||||
new RenameRootFields((operation, name) => `UsersModule${capitalize(name)}`)
|
||||
])
|
||||
|
||||
const transformedKeyVaultSchema = transformSchema(keyVaultSchema, [
|
||||
new RenameTypes((name) => `KeyVaultModule${capitalize(name)}`),
|
||||
new RenameRootFields((operation, name) => `KeyVaultModule${capitalize(name)}`)
|
||||
])
|
||||
const transformedTeamsSchema = await createTransformedRemoteSchema('TeamsModule', process.env.TEAMS_API_URL)
|
||||
const transformedUsersSchema = await createTransformedRemoteSchema('UsersModule', process.env.USERS_API_URL)
|
||||
const transformedKeyVaultSchema = await createTransformedRemoteSchema('KeyVaultModule', process.env.KEYVAULT_API_URL)
|
||||
|
||||
const linkSchemaDefs =
|
||||
`
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
export const capitalize = (string) => {
|
||||
return (string).charAt(0).toUpperCase() + (string).slice(1)
|
||||
}
|
||||
|
||||
export default capitalize
|
||||
Reference in new issue
Block a user