Skip to content

Instantly share code, notes, and snippets.

@adamgoose
Last active March 21, 2018 01:44
Show Gist options
  • Save adamgoose/e953899b0db745f21ad4bea7f85b44c3 to your computer and use it in GitHub Desktop.
Save adamgoose/e953899b0db745f21ad4bea7f85b44c3 to your computer and use it in GitHub Desktop.
import cors from 'cors'
import dotenv from 'dotenv'
import express from 'express'
import getSchemas from './schemas'
import resolvers from './resolvers'
import bodyParser from 'body-parser'
import { mergeSchemas } from 'graphql-tools'
import { graphqlExpress } from 'apollo-server-express'
import { express as voyager } from 'graphql-voyager/middleware'
import expressPlayground from 'graphql-playground-middleware-express'
dotenv.load();
(async () => {
const schemas = await getSchemas()
// Initialize the app
const app = express();
app.use('*', cors())
// The GraphQL endpoint
app.use('/api/graphql', bodyParser.json(), graphqlExpress(function (req, res) {
return {
tracing: process.env.APP_DEBUG || false,
schema: mergeSchemas({ schemas, resolvers }),
context: {
headers: req.headers
}
}
}))
// The Manifest Endpoint
app.use('/manifest', function (req, res) {
res.send({
client_id: process.env.MICROPM_CLIENT_ID,
})
})
app.use('/voyager', voyager({ endpointUrl: './api/graphql' }));
app.use('/', expressPlayground({ endpoint: './api/graphql', settings: { 'request.credentials': 'include' } }));
// Start the server
app.listen(process.env.PORT || 3000, () => {
console.log('MicroPM GraphQL')
console.log(`Listening on Port ${process.env.PORT || 3000}`)
console.log('GraphQL Endpoint: /api/graphql')
console.log('Voyager Endpoint: /voyager')
console.log('GraphQL Playground Endpoint: /')
});
})()
query GetAppointmentsByDate($page: Int!, $dos: String!, $personIds: [ID], $statuses: [String], $placeIds: [ID], $notTagged: String) {
appointments: EventsQuery(page: $page, date: $dos, person_id_in: $personIds, status_in: $statuses, place_id_in: $placeIds, not_tagged: $notTagged) {
total
current_page
last_page
per_page
data {
id
patient {
id
first_name
last_name
tags
policies {
total
__typename
}
__typename
}
place {
id
name
__typename
}
verifications {
data {
id
status
benefits {
active_coverage
__typename
}
__typename
}
__typename
}
tags
__typename
}
__typename
}
}
export default mergeInfo => ({
PersonType: {
events: {
fragment: `fragment PersonTypeFragment on PersonType { id }`,
resolve(parent, args, context, info) {
let person_id = parent.id
return mergeInfo.delegate('query', 'EventsQuery', { person_id }, context, info)
}
},
policies: {
fragment: `fragment PersonTypeFragment on PersonType { id }`,
resolve(parent, args, context, info) {
let person_id = parent.id
return mergeInfo.delegate('query', 'PoliciesQuery', { person_id }, context, info)
}
}
},
EventType: {
verifications: {
fragment: `fragment EventTypeFragment on EventType { id }`,
resolve(root, args, context, info) {
let appointment_id = root.id
return mergeInfo.delegate('query', 'VerificationsQuery', { appointment_id }, context, info)
}
},
patient: {
fragment: `fragment EventTypeFragment on EventType { patient_id }`,
resolve(parent, args, context, info) {
let id = parent.patient_id
return mergeInfo.delegate('query', 'Person', { id }, context, info)
}
},
doctor: {
fragment: `fragment EventTypeFragment on EventType { doctor_id }`,
resolve(parent, args, context, info) {
let id = parent.doctor_id
return mergeInfo.delegate('query', 'Person', { id }, context, info)
}
}
},
AttendeeType: {
person: {
fragment: `fragment AttendeeTypeFragment on AttendeeType { person_id }`,
resolve(parent, args, context, info) {
let id = parent.person_id
return mergeInfo.delegate('query', 'Person', { id }, context, info)
}
}
},
PolicyType: {
payer: {
fragment: `fragment PolicyTypeFragment on PolicyType { payer_id }`,
resolve(parent, args, context, info) {
let id = parent.payer_id
return mergeInfo.delegate('query', 'Payer', { id }, context, info)
}
},
person: {
fragment: `fragment PolicyTypeFragment on PolicyType { person_id }`,
resolve(parent, args, context, info) {
let id = parent.person_id
return mergeInfo.delegate('query', 'Person', { id }, context, info)
}
}
},
VerificationType: {
provider: {
fragment: `fragment VerificationTypeFragment on VerificationType { provider_id }`,
resolve(parent, args, context, info) {
let id = parent.provider_id
return mergeInfo.delegate('query', 'Person', { id }, context, info)
}
},
appointment: {
fragment: `fragment VerificationTypeFragment on VerificationType { appointment_id }`,
resolve(parent, args, context, info) {
let id = parent.appointment_id
return mergeInfo.delegate('query', 'Event', { id }, context, info)
}
}
}
})
import _ from 'lodash'
import {
introspectSchema,
makeRemoteExecutableSchema
} from 'graphql-tools'
import fetch from 'node-fetch'
import { HttpLink } from 'apollo-link-http'
import { setContext } from 'apollo-link-context'
module.exports = (async () => {
let links = {
persons: `${process.env.PERSONS_HOST || 'http://persons'}/api/graphql/`,
events: `${process.env.EVENTS_HOST || 'http://events'}/api/graphql/`,
payers: `${process.env.PAYERS_HOST || 'http://payers'}/api/graphql/`,
policies: `${process.env.POLICIES_HOST || 'http://policies'}/api/graphql/`
}
var schemas = []
for (let name in links) {
let http = new HttpLink({
uri: links[name],
fetch,
})
let link = setContext((request, previousContext) => ({
headers: {
Authorization: _.get(previousContext, 'graphqlContext.headers.authorization'),
Accept: 'application/json'
}
})).concat(http)
let schema = await introspectSchema(link)
schemas.push(makeRemoteExecutableSchema({ schema, link }))
}
schemas.push(`
extend type PersonType {
events(perPage: Int, page: Int): EventPaginator
policies(perPage: Int, page: Int, priority: Int): PolicyPaginator
}
extend type EventType {
verifications(perPage: Int, page: Int): VerificationPaginator
patient: PersonType
doctor: PersonType
}
extend type AttendeeType {
person: PersonType
}
extend type PolicyType {
person: PersonType
payer: PayerType
}
extend type VerificationType {
provider: PersonType
appointment: EventType
}
`)
return schemas
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment