From e0f2d80f30099fc2db8791c3728e8b996a8f9436 Mon Sep 17 00:00:00 2001 From: Jack Meyer Date: Wed, 21 Oct 2020 21:32:35 -0500 Subject: [PATCH] feat: adding some graphql setup --- .env.example | 0 docker-compose.yml | 2 ++ package.json | 16 ++++++----- src/app.ts | 16 ++++------- src/db/PatientEntity.ts | 10 +++++++ src/graphql/info/info-resolvers.ts | 7 +++++ src/graphql/info/info-schema.ts | 7 +++++ src/graphql/patient/patient-resolvers.ts | 17 ++++++++++++ src/graphql/patient/patient-schema.ts | 34 ++++++++++++++++++++++++ src/graphql/resolvers.ts | 7 ----- src/graphql/schema.ts | 7 ----- src/index.ts | 6 +++++ src/model/Entity.ts | 3 +++ src/model/Name.ts | 7 +++++ src/model/Patient.ts | 6 +++++ src/services/info.ts | 14 ++++++++++ src/services/patients.ts | 14 ++++++++++ tsconfig.json | 1 - 18 files changed, 142 insertions(+), 32 deletions(-) delete mode 100644 .env.example create mode 100644 src/db/PatientEntity.ts create mode 100644 src/graphql/info/info-resolvers.ts create mode 100644 src/graphql/info/info-schema.ts create mode 100644 src/graphql/patient/patient-resolvers.ts create mode 100644 src/graphql/patient/patient-schema.ts delete mode 100644 src/graphql/resolvers.ts delete mode 100644 src/graphql/schema.ts create mode 100644 src/model/Entity.ts create mode 100644 src/model/Name.ts create mode 100644 src/model/Patient.ts create mode 100644 src/services/info.ts create mode 100644 src/services/patients.ts diff --git a/.env.example b/.env.example deleted file mode 100644 index e69de29b..00000000 diff --git a/docker-compose.yml b/docker-compose.yml index 76b956bc..a8dd194d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -8,3 +8,5 @@ services: MONGO_INITDB_DATABASE: hospitalrun MONGO_INITDB_ROOT_USERNAME: root MONGO_INITDB_ROOT_PASSWORD: password + ports: + - '27017-27019:27017-27019' diff --git a/package.json b/package.json index 9db70038..89b7cb4c 100644 --- a/package.json +++ b/package.json @@ -33,17 +33,20 @@ "fastify": "~3.5.0", "fastify-autoload": "~3.3.0", "fastify-blipp": "~3.0.0", + "fastify-cors": "~4.1.0", + "fastify-helmet": "~5.0.1", "fastify-http-proxy": "~4.0.2", + "fastify-mongodb": "~2.0.1", + "fastify-no-icon": "~4.0.0", "fastify-plugin": "~2.3.3", + "graphiql": "~1.0.5", + "graphql-tools": "~6.2.0", "make-promises-safe": "~5.1.0", + "mercurius": "~6.3.0", + "mongoose": "~5.10.7", "nano": "~8.2.1", "qs": "~6.9.1", - "require-from-string": "~2.0.2", - "fastify-cors": "~4.1.0", - "fastify-gql": "~5.8.1", - "fastify-helmet": "~5.0.1", - "fastify-no-icon": "~4.0.0", - "graphql-tools": "~6.2.0" + "require-from-string": "~2.0.2" }, "devDependencies": { "@commitlint/cli": "~11.0.0", @@ -51,6 +54,7 @@ "@commitlint/prompt": "~11.0.0", "@types/glob": "~7.1.1", "@types/mkdirp": "~1.0.0", + "@types/mongoose": "~5.7.36", "@types/node": "~14.11.1", "@types/qs": "6.9.5", "@types/require-from-string": "~1.2.0", diff --git a/src/app.ts b/src/app.ts index 4b76ac52..ac68358d 100644 --- a/src/app.ts +++ b/src/app.ts @@ -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, _: 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) fastify.register(fastifyAutoload, { dir: join(__dirname, 'services'), }) - - fastify.register(GQL, { schema, resolvers }) next() } diff --git a/src/db/PatientEntity.ts b/src/db/PatientEntity.ts new file mode 100644 index 00000000..abbf207f --- /dev/null +++ b/src/db/PatientEntity.ts @@ -0,0 +1,10 @@ +import mongoose, { Schema, Types } from 'mongoose' +import Patient from '../model/Patient' + +const PatientSchema = new Schema({ + id: Types.ObjectId, + code: String, + name: Object, +}) + +export default mongoose.model('Patient', PatientSchema) diff --git a/src/graphql/info/info-resolvers.ts b/src/graphql/info/info-resolvers.ts new file mode 100644 index 00000000..65a67bad --- /dev/null +++ b/src/graphql/info/info-resolvers.ts @@ -0,0 +1,7 @@ +const infoResolvers = { + Query: { + info: () => 'HospitalRun GraphQL API', + }, +} + +export default infoResolvers diff --git a/src/graphql/info/info-schema.ts b/src/graphql/info/info-schema.ts new file mode 100644 index 00000000..051b7714 --- /dev/null +++ b/src/graphql/info/info-schema.ts @@ -0,0 +1,7 @@ +const infoSchema = ` +extend type Query { + info: String! +} +` + +export default infoSchema diff --git a/src/graphql/patient/patient-resolvers.ts b/src/graphql/patient/patient-resolvers.ts new file mode 100644 index 00000000..127e511f --- /dev/null +++ b/src/graphql/patient/patient-resolvers.ts @@ -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 diff --git a/src/graphql/patient/patient-schema.ts b/src/graphql/patient/patient-schema.ts new file mode 100644 index 00000000..70fc459b --- /dev/null +++ b/src/graphql/patient/patient-schema.ts @@ -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 diff --git a/src/graphql/resolvers.ts b/src/graphql/resolvers.ts deleted file mode 100644 index d47818d0..00000000 --- a/src/graphql/resolvers.ts +++ /dev/null @@ -1,7 +0,0 @@ -const resolvers = { - Query: { - add: async (_: any, { x, y }: any) => x + y, - }, -} - -export default resolvers diff --git a/src/graphql/schema.ts b/src/graphql/schema.ts deleted file mode 100644 index c13a2253..00000000 --- a/src/graphql/schema.ts +++ /dev/null @@ -1,7 +0,0 @@ -const schema = ` - type Query { - add(x: Int, y: Int): Int - } -` - -export default schema diff --git a/src/index.ts b/src/index.ts index 0d41d15d..3bc32b17 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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' @@ -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) diff --git a/src/model/Entity.ts b/src/model/Entity.ts new file mode 100644 index 00000000..c8c665d9 --- /dev/null +++ b/src/model/Entity.ts @@ -0,0 +1,3 @@ +import mongoose from 'mongoose' + +export type Entity = mongoose.Document diff --git a/src/model/Name.ts b/src/model/Name.ts new file mode 100644 index 00000000..1a98ad11 --- /dev/null +++ b/src/model/Name.ts @@ -0,0 +1,7 @@ +export default interface Name { + givenName?: string + familyName?: string + suffix?: string + prefix?: string + fullName?: string +} diff --git a/src/model/Patient.ts b/src/model/Patient.ts new file mode 100644 index 00000000..c1caf50c --- /dev/null +++ b/src/model/Patient.ts @@ -0,0 +1,6 @@ +import { Entity } from './Entity' +import Name from './Name' + +export default interface Patient extends Entity { + name: Name +} diff --git a/src/services/info.ts b/src/services/info.ts new file mode 100644 index 00000000..67eb192c --- /dev/null +++ b/src/services/info.ts @@ -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, + _: FastifyPluginOptions, + next: (error?: Error) => void, +) => { + fastify.graphql.extendSchema(infoSchema) + fastify.graphql.defineResolvers(infoResolvers) + next() +} diff --git a/src/services/patients.ts b/src/services/patients.ts new file mode 100644 index 00000000..6763061e --- /dev/null +++ b/src/services/patients.ts @@ -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, + _: FastifyPluginOptions, + next: (error?: Error) => void, +) => { + fastify.graphql.extendSchema(patientSchema) + fastify.graphql.defineResolvers(patientResolvers) + next() +} diff --git a/tsconfig.json b/tsconfig.json index fd8018e4..da6e76e7 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -5,7 +5,6 @@ "exclude": [ "node_modules", "dist", - "src/db", "src/bin" ], "compilerOptions": {