gql ☁
Universal GraphQL HTTP middleware for 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, context: (request: ServerRequest) => ({ request }) })(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, context: (request: Request) => ({ request }) }))
.listen(3000, () => console.log(`☁ Started on http://localhost:3000`))
Then run:
$ curl -X POST localhost:3000/graphql -d '{ "query": "{ hello }" }'
{
"data": {
"hello": "Hello World!"
}
}