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

Commit

Permalink
refactor(grahql): working example with gql
Browse files Browse the repository at this point in the history
  • Loading branch information
jackcmeyer committed Aug 31, 2020
1 parent 53c48d6 commit 201cf56
Show file tree
Hide file tree
Showing 15 changed files with 75 additions and 119 deletions.
27 changes: 13 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,15 @@
"author": "Maksim Sinik <maksim@sinik.it>",
"license": "MIT",
"dependencies": {
"fastify": "~2.15.0",
"fastify-autoload": "~1.2.2",
"fastify": "~3.3.0",
"fastify-autoload": "~3.0.8",
"fastify-blipp": "~3.0.0",
"fastify-cors": "~3.0.2",
"fastify-helmet": "~3.0.2",
"fastify-http-proxy": "~3.1.0",
"fastify-no-icon": "~3.0.0",
"fastify-plugin": "~1.6.1",
"make-promises-safe": "~5.1.0",
"nano": "~8.2.1",
"qs": "~6.9.1",
"require-from-string": "~2.0.2"
"fastify-cors": "~4.1.0",
"fastify-gql": "~5.6.0",
"fastify-helmet": "~5.0.1",
"fastify-no-icon": "~4.0.0",
"fastify-plugin": "~2.3.3",
"graphql-tools": "~6.1.0"
},
"devDependencies": {
"@commitlint/cli": "~8.3.5",
Expand All @@ -51,10 +48,12 @@
"@types/mkdirp": "~1.0.0",
"@types/node": "~14.6.0",
"@types/qs": "6.9.1",
"@typescript-eslint/eslint-plugin": "~2.34.0",
"@typescript-eslint/parser": "~2.34.0",
"@types/require-from-string": "~1.2.0",
"@types/sade": "~1.7.0",
"@types/tap": "~14.10.0",
"@types/ws": "~7.2.6",
"@typescript-eslint/eslint-plugin": "~2.34.0",
"@typescript-eslint/parser": "~2.34.0",
"chalk": "~4.1.0",
"commitizen": "~4.2.1",
"commitlint-config-cz": "~0.13.0",
Expand All @@ -76,7 +75,7 @@
"rimraf": "~3.0.0",
"sade": "~1.7.3",
"source-map-support": "~0.5.16",
"tap": "~14.10.5",
"tap": "~14.10.8",
"tap-mocha-reporter": "~5.0.0",
"typescript": "~3.9.2"
},
Expand Down
48 changes: 17 additions & 31 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,26 @@
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 AutoLoad from 'fastify-autoload'
import { FastifyInstance } from 'fastify'
import { nextCallback } from 'fastify-plugin'
import noIcon from 'fastify-no-icon'
import helmet from 'fastify-helmet'
import qs from 'qs'
import cors from 'fastify-cors'
import GQL from 'fastify-gql'
import schema from './graphql/schema'
import resolvers from './graphql/resolvers'

function HospitalRun(fastify: FastifyInstance, opts: any, next: nextCallback) {
fastify.register(cors, {
export default (
fastify: FastifyInstance<Server, IncomingMessage, ServerResponse>,
_: FastifyPluginOptions,
next: (error?: Error) => void,
) => {
fastify.register(fastifyCors, {
allowedHeaders: ['Content-Type', 'Authorization'],
})
fastify.register(helmet)
fastify.register(noIcon)

// This loads all application wide plugins defined in plugins folder
fastify.register(AutoLoad, {
dir: join(__dirname, 'plugins'),
includeTypeScript: true,
options: { ...opts },
})

// This loads all routes and services defined in services folder
fastify.register(AutoLoad, {
fastify.register(fastifyHelmet)
fastify.register(fastifyAutoload, {
dir: join(__dirname, 'services'),
includeTypeScript: true,
options: { ...opts },
})

fastify.register(GQL, { schema, resolvers })
next()
}

HospitalRun.options = {
querystringParser: (str: string) => qs.parse(str),
logger: true,
ignoreTrailingSlash: true,
}

export = HospitalRun
7 changes: 7 additions & 0 deletions src/graphql/resolvers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const resolvers = {
Query: {
add: async (_: any, { x, y }: any) => x + y,
},
}

export default resolvers
7 changes: 7 additions & 0 deletions src/graphql/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const schema = `
type Query {
add(x: Int, y: Int): Int
}
`

export default schema
23 changes: 8 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
// we need this file because of this issue: https://github.com/fastify/fastify-cli/issues/131
import 'make-promises-safe'
import Fastify from 'fastify'
import hospitalRun from './app'
import app from './app'

const port = Number(process.env.PORT) || 3000
const ip = process.env.IP || '0.0.0.0'
const ip = process.env.IP || 'localhost'

const fastify = Fastify(hospitalRun.options)
fastify.register(hospitalRun)
const fastify = Fastify({
ignoreTrailingSlash: true,
logger: true,
})

if (process.env.NODE_ENV !== 'production') {
// eslint-disable-next-line
const blipp = require('fastify-blipp')
fastify.register(blipp)
}
fastify.register(app)

fastify.listen(port, ip, err => {
if (err) {
fastify.log.error(err)
console.log(err)
process.exit(1)
}
if (process.env.NODE_ENV !== 'production') {
fastify.blipp()
}
})
14 changes: 0 additions & 14 deletions src/plugins/support.ts

This file was deleted.

13 changes: 13 additions & 0 deletions src/services/health.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Server, IncomingMessage, ServerResponse } from 'http'
import { FastifyInstance, FastifyPluginOptions } from 'fastify'

export default (
fastify: FastifyInstance<Server, IncomingMessage, ServerResponse>,
_: FastifyPluginOptions,
next: (error?: Error) => void,
) => {
fastify.get('/health', (_, reply) => {
reply.send({ status: 'UP' })
})
next()
}
11 changes: 6 additions & 5 deletions src/services/root.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { Server, IncomingMessage, ServerResponse } from 'http'
import { FastifyInstance } from 'fastify'
import { nextCallback } from 'fastify-plugin'
import { FastifyInstance, FastifyPluginOptions } from 'fastify'

export default (
fastify: FastifyInstance<Server, IncomingMessage, ServerResponse>,
_: {},
next: nextCallback,
_: FastifyPluginOptions,
next: (error?: Error) => void,
) => {
fastify.get('/', (_, reply) => {
reply.send({ root: true })
const query = '{ add(x: 2, y: 2) }'
return reply.graphql(query)
})

next()
}
2 changes: 1 addition & 1 deletion test/helper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Fastify from 'fastify'
import fp from 'fastify-plugin'
import App from '../src/app'
import App from './../src/app'

// Fill in this config with all the configurations
// needed for testing the application
Expand Down
11 changes: 0 additions & 11 deletions test/plugins/support.test.ts

This file was deleted.

6 changes: 3 additions & 3 deletions test/services/root.test.ts → test/services/health.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { test } from 'tap'
import { build } from '../helper'

test('default root route', async (t: any) => {
test('default root route', async t => {
const app = build(t)

const res = await app.inject({
url: '/',
url: '/health',
})
t.deepEqual(JSON.parse(res.payload), { root: true })
t.deepEqual(JSON.parse(res.payload), { status: 'UP' })
})
7 changes: 0 additions & 7 deletions typings/fastify-blipp/index.d.ts

This file was deleted.

7 changes: 0 additions & 7 deletions typings/fastify-no-icon/index.d.ts

This file was deleted.

6 changes: 0 additions & 6 deletions typings/fastify/index.d.ts

This file was deleted.

5 changes: 0 additions & 5 deletions typings/tap/index.d.ts

This file was deleted.

0 comments on commit 201cf56

Please sign in to comment.