This repository has been archived by the owner on Jan 9, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 650
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(grahql): working example with gql
- Loading branch information
1 parent
53c48d6
commit 201cf56
Showing
15 changed files
with
75 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
}) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }) | ||
}) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.