0.2.2 • Published 2 months ago

edgeql v0.2.2

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

EdgeQL

Effortlessly craft GraphQL APIs on the Edge, designed to thrive across various JavaScript runtimes.

RuntimeStatusExample
Cloudflare Workers:white_check_mark:cloudflare
Node:white_check_mark:node
Bun:white_check_mark:Bun
Deno:white_large_square: Pending
Vercel:white_large_square: Pending

EdgeQL on JavaScript Runtimes

Node

import { NodeEdgeQL } from 'edgeql/node'

const app = new NodeEdgeQL()

app.handle(`
type Query {
  hello: String
}
`, (ctx) => {
  return  `hello from EdgeQL on ${ctx.runtime.runtime}`
})

app.listen({port: 4000}, ({address, family, port}) => {
  console.log(address, family, port)
})

Bun

import { EdgeQL } from 'edgeql'
import type {  Context } from 'edgeql'

const app = new EdgeQL()

app.handle(`
type Query {
  hello: String
}
`, (_ctx: Context) => {
  return  'hello from EdgeQL on Bun'
})

const port =  Bun.env['PORT'] ? parseInt(Bun.env['PORT']) : 3000
console.log(`Running at http://localhost:${port}`)

export default {
  port,
  fetch: app.fetch
}

Cloudflare Workers

import { EdgeQL } from 'edgeql'
import type { Context } from 'edgeql'

const app = new EdgeQL()

app.handle(
`
type Query {
  whereami: String
}
`,
(ctx: Context) => {
  return `EdgeQL is running on ${ctx.runtime.runtime}`
})

Schema Design Approaches

EdgeQL supports both Schema-First and Code-First.

  • Schema First
import { EdgeQL } from 'edgeql'

const app = new EdgeQL()
const schema = `
type Query {
  hello: String
}
    `
app.handle(schema, (ctx: Context) => 'world')

export default app
  • Code First
import { EdgeQL } from 'edgeql'
import type { Context } from 'edgeql'
import {
  GraphQLSchema,
  GraphQLObjectType,
  GraphQLString,
} from 'graphql'

const app = new EdgeQL()

const helloworld: GraphQLSchema = new GraphQLSchema({
  query: new GraphQLObjectType({
    name: 'Query',
    fields: {
      helloworld: {
        type: GraphQLString,
        resolve: (parent: any, args: any, ctx: Context, info: any) => {
          return 'helloworld, EdgeQL'
        },
      },
    },
  })
})

app.handle(helloworld)

export default app

Middlewares

EdgeQL adopts the same middleware style like Koa, middleware are simple functions which return a MiddlewareFunction with signature (ctx, next). When the middleware is run, it must manually invoke next() to run the "downstream" middleware.

For example if you wanted to track how long it takes for a request to propagate through EdgeQL by adding an X-Response-Time header field the middleware would look like the following:

async function responseTime(ctx: Context, next: Next) {
  const start = Date.now();
  await next();
  const ms = Date.now() - start;
  ctx.set('X-Response-Time', `${ms}ms`);
}

app.use(responseTime);

The builtin middlewares are,

0.2.2

2 months ago

0.2.1

2 months ago

0.2.0

2 months ago

0.1.8

2 months ago

0.1.7

2 months ago

0.1.9

2 months ago

0.1.6

2 months ago

0.1.5

2 months ago

0.1.4

2 months ago

0.1.2

2 months ago

0.1.3

2 months ago

0.1.0

2 months ago

0.1.1

2 months ago

0.0.10

2 months ago

0.0.8

2 months ago

0.0.7

2 months ago

0.0.6

2 months ago

0.0.5

3 months ago

0.0.4

3 months ago

0.0.3

3 months ago

0.0.2

3 months ago

0.0.1

3 months ago