Universal and spec-compliant GraphQL HTTP middleware for Deno and Bun. Based on graphql-http.
Deno.serve
, Bun.serve
and oakgraphiql: true
)The simplest setup with Deno.serve
:
import { GraphQLHTTP } from 'jsr:@deno-libs/gql@3.0.1/mod.ts' import { makeExecutableSchema } from 'npm:@graphql-tools/schema@10.0.3' import { gql } from 'https://deno.land/x/graphql_tag@0.1.2/mod.ts' const typeDefs = gql` type Query { hello: String } ` const resolvers = { Query: { hello: () => `Hello World!`, }, } const schema = makeExecutableSchema({ resolvers, typeDefs }) Deno.serve({ port: 3000, onListen({ hostname, port }) { console.log(`☁ Started on http://${hostname}:${port}`) }, }, async (req) => { const { pathname } = new URL(req.url) return pathname === '/graphql' ? await GraphQLHTTP<Request>({ schema, graphiql: true, })(req) : new Response('Not Found', { status: 404 }) })
Then run:
$ curl -X POST localhost:3000/graphql -d '{ "query": "{ hello }" }' -H "Content-Type: application/json" { "data": { "hello": "Hello World!" } }
Or in the GraphQL Playground:
Add Package
deno add jsr:@deno-libs/gql
Import symbol
import * as gql from "@deno-libs/gql";
---- OR ----
Import directly with a jsr specifier
import * as gql from "jsr:@deno-libs/gql";
Add Package
bunx jsr add @deno-libs/gql
Import symbol
import * as gql from "@deno-libs/gql";