Type-graphql: Fastify instead of Express ?

Created on 21 Nov 2018  路  2Comments  路  Source: MichalLytek/type-graphql

I were checking dependencies and noticed Type-graphql is based on Express and apollo-server-express.

How hard would it be to switch to a faster Node framework like Fastify (and apollo-server-fastify) ?

Out of scope Question

Most helpful comment

I'm doing like this.

// schema.ts

import { buildSchema } from 'type-graphql'
import { Container } from 'typedi'
import { QueryResolver } from './resolvers/QueryResolver'
import { MutationResolver } from './resolvers/MutationResolver'

export const getSchema = () =>
  buildSchema({
    resolvers: [QueryResolver, MutationResolver],
    emitSchemaFile: true,
    container: Container,
  })
// graphqlPlugin.ts

import { getSchema } from './schema'
import { graphql } from 'graphql'
import { FastifyPluginAsync } from 'fastify'

const graphqlPlugin: FastifyPluginAsync = async (app) => {
  const schema = await getSchema()
  app.post<{ Body: { query: string; variables?: object } }>(
    '/',
    async (request, reply) => {
      const res = await graphql({
        schema,
        source: request.body.query,
        variableValues: request.body.variables,
        contextValue: {},
      })
      reply.send(res)
    },
  )
}

All 2 comments

TypeGraphQL depends only on graphql-js and produce only a GraphQLSchema.

So you can use Express, Fastify, Websockets, MQTT or even TCP to expose the graphql api to the world. It's all your choice which integration library (apollo-server-fastify) you will use 馃槈

I'm doing like this.

// schema.ts

import { buildSchema } from 'type-graphql'
import { Container } from 'typedi'
import { QueryResolver } from './resolvers/QueryResolver'
import { MutationResolver } from './resolvers/MutationResolver'

export const getSchema = () =>
  buildSchema({
    resolvers: [QueryResolver, MutationResolver],
    emitSchemaFile: true,
    container: Container,
  })
// graphqlPlugin.ts

import { getSchema } from './schema'
import { graphql } from 'graphql'
import { FastifyPluginAsync } from 'fastify'

const graphqlPlugin: FastifyPluginAsync = async (app) => {
  const schema = await getSchema()
  app.post<{ Body: { query: string; variables?: object } }>(
    '/',
    async (request, reply) => {
      const res = await graphql({
        schema,
        source: request.body.query,
        variableValues: request.body.variables,
        contextValue: {},
      })
      reply.send(res)
    },
  )
}
Was this page helpful?
0 / 5 - 0 ratings