gql
Universal GraphQL HTTP middleware for Deno.
Features
- ✨ Works with
std/http
, Opine and oak - ⚡
GraphQL Playground
integration (via
graphiql: true
)
Get started
Vanilla
The simplest setup with std/http
:
import { Server } from 'https://deno.land/std@0.179.0/http/server.ts'
import { GraphQLHTTP } from 'https://deno.land/x/gql/mod.ts'
import { makeExecutableSchema } from 'https://esm.sh/@graphql-tools/schema@9.0.17'
import { gql } from 'https://deno.land/x/graphql_tag@0.1.1/mod.ts'
const typeDefs = gql`
type Query {
hello: String
}
`
const resolvers = { Query: { hello: () => `Hello World!` } }
const s = new Server({
handler: async (req) => {
const { pathname } = new URL(req.url)
return pathname === '/graphql'
? await GraphQLHTTP<Request>({
schema: makeExecutableSchema({ resolvers, typeDefs }),
graphiql: true
})(req)
: new Response('Not Found', { status: 404 })
},
addr: ':3000'
})
s.listenAndServe()
Then run:
$ curl -X POST localhost:3000/graphql -d '{ "query": "{ hello }" }'
{
"data": {
"hello": "Hello World!"
}
}
Or in GraphQL Playground: