Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

feat: graphql #371

Draft
wants to merge 14 commits into
base: master
Choose a base branch
from
Empty file removed .env.example
Empty file.
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ services:
MONGO_INITDB_DATABASE: hospitalrun
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: password
ports:
- '27017-27019:27017-27019'
11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,17 @@
"fastify": "~3.7.0",
"fastify-autoload": "~3.5.2",
"fastify-blipp": "~3.0.0",
"fastify-cors": "~4.1.0",
"fastify-helmet": "~5.0.1",
"fastify-http-proxy": "~4.3.0",
"fastify-mongodb": "~2.0.1",
"fastify-no-icon": "~4.0.0",
"fastify-plugin": "~3.0.0",
"graphiql": "~1.0.5",
"graphql-tools": "~7.0.0"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missed a comma here which is causing build failure. Thankyou

"make-promises-safe": "~5.1.0",
"mercurius": "~6.3.0",
"mongoose": "~5.10.7",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are not going to use mongoose since it is really slow and it will become a major bottleneck soon. For the moment we can leave just the mongodb driver

"nano": "~9.0.0",
"qs": "~6.9.1",
"require-from-string": "~2.0.2",
Expand All @@ -51,7 +59,8 @@
"@commitlint/prompt": "~12.0.0",
"@types/glob": "~7.1.1",
"@types/mkdirp": "~1.0.0",
"@types/node": "~14.14.2",
"@types/mongoose": "~5.7.36",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for this.

"@types/node": "~~14.14.2",
"@types/qs": "6.9.5",
"@types/require-from-string": "~1.2.0",
"@types/sade": "~1.7.0",
Expand Down
16 changes: 5 additions & 11 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
import { Server, IncomingMessage, ServerResponse } from 'http'
import { FastifyInstance, FastifyPluginOptions } from 'fastify'
import fastifyCors from 'fastify-cors'
import fastifyHelmet from 'fastify-helmet'
import fastifyAutoload from 'fastify-autoload'
import { join } from 'path'
import GQL from 'fastify-gql'
import schema from './graphql/schema'
import resolvers from './graphql/resolvers'
import mercurius from 'mercurius'

export default (
fastify: FastifyInstance<Server, IncomingMessage, ServerResponse>,
_: FastifyPluginOptions,
next: (error?: Error) => void,
) => {
fastify.register(fastifyCors, {
allowedHeaders: ['Content-Type', 'Authorization'],
})
fastify.register(fastifyHelmet)
// fastify.register(fastifyHelmet)
fastify.register(mercurius, {
defineMutation: true,
} as any)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are the types wrong here? If so we can make a PR to mercurius.

fastify.register(fastifyAutoload, {
dir: join(__dirname, 'services'),
})

fastify.register(GQL, { schema, resolvers })
next()
}
10 changes: 10 additions & 0 deletions src/db/PatientEntity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import mongoose, { Schema, Types } from 'mongoose'
import Patient from '../model/Patient'

const PatientSchema = new Schema({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use json schema for the schemas so we can add them to fastify too when needed.

id: Types.ObjectId,
code: String,
name: Object,
})

export default mongoose.model<Patient>('Patient', PatientSchema)
7 changes: 7 additions & 0 deletions src/graphql/info/info-resolvers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const infoResolvers = {
Query: {
info: () => 'HospitalRun GraphQL API',
},
}

export default infoResolvers
7 changes: 7 additions & 0 deletions src/graphql/info/info-schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const infoSchema = `
extend type Query {
info: String!
}
`

export default infoSchema
17 changes: 17 additions & 0 deletions src/graphql/patient/patient-resolvers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const patientResolvers = {
Query: {
info: () => 'HospitalRun GraphQL API',
},
Mutation: {
createPatient: async (_: any, args: any) => {
console.log(args.patientRequest)
return {
id: 'helloWorld',
name: args.patientRequest.name,
code: args.patientRequest.code,
}
},
},
}

export default patientResolvers
34 changes: 34 additions & 0 deletions src/graphql/patient/patient-schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const patientSchema = `
type Name {
givenName: String
familyName: String
prefix: String
suffix: String
fullName: String
}

type Patient {
id: ID!
name: Name
code: String!
}

input NameRequest {
givenName: String
familyName: String
prefix: String
suffix: String
fullName: String
}

input PatientRequest {
name: NameRequest
code: String!
}

extend type Mutation {
createPatient(patientRequest: PatientRequest!): Patient!
}
`

export default patientSchema
7 changes: 0 additions & 7 deletions src/graphql/resolvers.ts

This file was deleted.

7 changes: 0 additions & 7 deletions src/graphql/schema.ts

This file was deleted.

6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Fastify from 'fastify'
import app from './app'
import mongoose from 'mongoose'

const port = Number(process.env.PORT) || 3000
const ip = process.env.IP || 'localhost'
Expand All @@ -11,6 +12,11 @@ const fastify = Fastify({

fastify.register(app)

mongoose
.connect(process.env.DB_URL as string, { useNewUrlParser: true })
.then(() => fastify.log.info('Connected to MongoDB running on ' + process.env.DB_URL))
.catch((err) => fastify.log.error(err))

fastify.listen(port, ip, (err) => {
if (err) {
fastify.log.error(err)
Expand Down
3 changes: 3 additions & 0 deletions src/model/Entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import mongoose from 'mongoose'

export type Entity = mongoose.Document
7 changes: 7 additions & 0 deletions src/model/Name.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default interface Name {
givenName?: string
familyName?: string
suffix?: string
prefix?: string
fullName?: string
}
6 changes: 6 additions & 0 deletions src/model/Patient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Entity } from './Entity'
import Name from './Name'

export default interface Patient extends Entity {
name: Name
}
14 changes: 14 additions & 0 deletions src/services/info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Server, IncomingMessage, ServerResponse } from 'http'
import { FastifyInstance, FastifyPluginOptions } from 'fastify'
import infoSchema from '../graphql/info/info-schema'
import infoResolvers from '../graphql/info/info-resolvers'

export default (
fastify: FastifyInstance<Server, IncomingMessage, ServerResponse>,
_: FastifyPluginOptions,
next: (error?: Error) => void,
) => {
fastify.graphql.extendSchema(infoSchema)
fastify.graphql.defineResolvers(infoResolvers)
next()
}
14 changes: 14 additions & 0 deletions src/services/patients.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Server, IncomingMessage, ServerResponse } from 'http'
import { FastifyInstance, FastifyPluginOptions } from 'fastify'
import patientSchema from '../graphql/patient/patient-schema'
import patientResolvers from '../graphql/patient/patient-resolvers'

export default (
fastify: FastifyInstance<Server, IncomingMessage, ServerResponse>,
_: FastifyPluginOptions,
next: (error?: Error) => void,
) => {
fastify.graphql.extendSchema(patientSchema)
fastify.graphql.defineResolvers(patientResolvers)
next()
}
1 change: 0 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"exclude": [
"node_modules",
"dist",
"src/db",
"src/bin"
],
"compilerOptions": {
Expand Down