gql

GitHub release (latest by date)

Universal GraphQL HTTP middleware for Deno.

Features

  • supports vanilla, tinyhttp and any other backend framework
  • re-exports GraphQL interfaces from graphql_deno

Examples

Vanilla

import { serve, ServerRequest } from 'https://deno.land/std@0.90.0/http/server.ts'
import { GraphQLHTTP, GraphQLSchema, GraphQLObjectType, GraphQLString } from 'https://deno.land/x/gql/mod.ts'

const schema = new GraphQLSchema({
  query: new GraphQLObjectType({
    name: 'Query',
    fields: {
      hello: {
        type: GraphQLString,
        resolve() {
          return 'Hello World!'
        }
      }
    }
  })
})

const s = serve({ port: 3000 })

for await (const req of s) {
  await GraphQLHTTP({ schema })(req)
}

tinyhttp

import { App, Request } from 'https://deno.land/x/tinyhttp/mod.ts'
import { GraphQLHTTP, GraphQLSchema, GraphQLObjectType, GraphQLString } from 'https://deno.land/x/gql/mod.ts'

const schema = new GraphQLSchema({
  query: new GraphQLObjectType({
    name: 'Query',
    fields: {
      hello: {
        type: GraphQLString,
        resolve() {
          return 'Hello World!'
        }
      }
    }
  })
})

const app = new App()

app.post('/graphql', GraphQLHTTP({ schema }))

app.listen(3000, () => console.log(`☁  Started on http://localhost:3000`))

Then run:

$ curl -X POST localhost:3000/graphql -d '{ "query": "{ hello }" }'
{
  "data": {
    "hello": "Hello World!"
  }
}