Comparing version 0.0.10 to 0.1.0
@@ -1,4 +0,4 @@ | ||
import type { GraphQLFieldResolver, GraphQLSchema } from 'graphql'; | ||
import { Context } from './context'; | ||
import type { ExecutionContext, Environment, Middleware } from './types'; | ||
import type { GraphQLSchema } from 'graphql'; | ||
import { Context } from './context/context'; | ||
import type { ExecutionContext, Environment, Middleware, Handler } from './types'; | ||
export declare class EdgeQL { | ||
@@ -9,7 +9,7 @@ private schemas; | ||
fetch: (request: Request, env?: Environment, exeContext?: ExecutionContext) => Promise<Response>; | ||
handle(ctx: Context): Promise<void>; | ||
execute(ctx: Context): Promise<void>; | ||
use(fn: Middleware): void; | ||
register(schema: GraphQLSchema): void; | ||
register(schema: string, resolve: GraphQLFieldResolver<any, any, any, any>): void; | ||
register(schema: string, resolves: Record<string, GraphQLFieldResolver<any, any, any, any>>): void; | ||
handle(schema: GraphQLSchema): void; | ||
handle(schema: string, handler: Handler): void; | ||
handle(schema: string, handlers: Record<string, Handler>): void; | ||
} |
import { mergeSchemas } from '@graphql-tools/schema'; | ||
import { buildSchema, validateSchema, GraphQLObjectType, execute, GraphQLError } from 'graphql'; | ||
import { compose } from './compose'; | ||
import { Context } from './context'; | ||
import { Context } from './context/context'; | ||
export class EdgeQL { | ||
@@ -17,3 +17,3 @@ constructor() { | ||
try { | ||
ctx = await Context.create(request, env, exeContext, this.graph); | ||
ctx = new Context(request, env, exeContext); | ||
} | ||
@@ -32,10 +32,12 @@ catch (e) { | ||
} | ||
await compose([...this.middlewares, this.handle])(ctx); | ||
await ctx.graphql.init(ctx.http.request); | ||
ctx.graphql.schema = this.graph; | ||
await compose([...this.middlewares, this.execute])(ctx); | ||
return ctx.json(); | ||
}; | ||
} | ||
async handle(ctx) { | ||
if (!ctx.req?.query) { | ||
ctx.res.status = 400; | ||
ctx.res.body = { | ||
async execute(ctx) { | ||
if (!ctx.graphql.query) { | ||
ctx.http.status = 400; | ||
ctx.http.body = { | ||
data: null, | ||
@@ -52,8 +54,8 @@ errors: [ | ||
} | ||
if (!ctx.req?.document) { | ||
ctx.res.status = 400; | ||
ctx.res.body = { | ||
if (!ctx.graphql.document) { | ||
ctx.http.status = 400; | ||
ctx.http.body = { | ||
data: null, | ||
errors: [ | ||
new GraphQLError(`could not generate document from query: ${ctx.req?.query}`, { | ||
new GraphQLError(`could not generate document from query: ${ctx.graphql.query}`, { | ||
extensions: { | ||
@@ -67,5 +69,5 @@ status: 400, | ||
} | ||
if (!ctx.schema) { | ||
ctx.res.status = 400; | ||
ctx.res.body = { | ||
if (!ctx.graphql.schema) { | ||
ctx.http.status = 400; | ||
ctx.http.body = { | ||
data: null, | ||
@@ -84,16 +86,16 @@ errors: [ | ||
const res = await execute({ | ||
schema: ctx.schema, | ||
document: ctx.req?.document ?? null, | ||
schema: ctx.graphql.schema, | ||
document: ctx.graphql.document ?? null, | ||
rootValue: null, | ||
contextValue: ctx, | ||
variableValues: ctx.req?.variables, | ||
operationName: ctx.req?.operationName, | ||
variableValues: ctx.graphql.variables, | ||
operationName: ctx.graphql.operationName, | ||
}); | ||
ctx.res.status = 200; | ||
ctx.res.body = res; | ||
ctx.http.status = 200; | ||
ctx.http.body = res; | ||
return; | ||
} | ||
catch (contextError) { | ||
ctx.res.status = 500; | ||
ctx.res.body = { | ||
ctx.http.status = 500; | ||
ctx.http.body = { | ||
data: null, | ||
@@ -113,3 +115,3 @@ errors: [ | ||
} | ||
register(...args) { | ||
handle(...args) { | ||
if (args.length === 1 && typeof args[0] === 'object') { | ||
@@ -138,3 +140,8 @@ const schemaValidationErrors = validateSchema(args[0]); | ||
else { | ||
fields[0].resolve = args[1]; | ||
fields[0].resolve = async (parents, arg, ctx, info) => { | ||
ctx.graphql.args = arg; | ||
ctx.graphql.parents = parents; | ||
ctx.graphql.info = info; | ||
return await args[1](ctx); | ||
}; | ||
} | ||
@@ -151,3 +158,9 @@ this.schemas.push(s); | ||
if (args[1][f]) { | ||
obj.getFields()[f].resolve = args[1][f]; | ||
obj.getFields()[f].resolve = async (parents, arg, ctx, info) => { | ||
ctx.graphql.parents = parents; | ||
ctx.graphql.args = arg; | ||
ctx.graphql.info = info; | ||
const fn = args[1][f]; | ||
return await fn(ctx); | ||
}; | ||
} | ||
@@ -154,0 +167,0 @@ else { |
@@ -44,3 +44,3 @@ import { GraphQLObjectType, GraphQLString, GraphQLSchema } from 'graphql'; | ||
}); | ||
app.register(new GraphQLSchema({ query: queryType })); | ||
app.handle(new GraphQLSchema({ query: queryType })); | ||
const req = new Request('http://localhost', { | ||
@@ -81,3 +81,3 @@ method: 'POST', | ||
}); | ||
app.register(new GraphQLSchema({ query: queryType })); | ||
app.handle(new GraphQLSchema({ query: queryType })); | ||
const req = new Request('http://localhost', { | ||
@@ -110,8 +110,8 @@ method: 'POST', | ||
`; | ||
app.register(schema, () => 'world'); | ||
app.register(` | ||
app.handle(schema, (ctx) => 'world'); | ||
app.handle(` | ||
type Query { | ||
hi: String | ||
} | ||
`, () => 'world'); | ||
`, (ctx) => 'world'); | ||
let req = new Request('http://localhost', { | ||
@@ -158,3 +158,3 @@ method: 'POST', | ||
const app = new EdgeQL(); | ||
app.register(` | ||
app.handle(` | ||
type Query { | ||
@@ -166,5 +166,5 @@ hi: String | ||
`, { | ||
hello: () => 'world', | ||
hi: () => 'world', | ||
ping: () => 'pong', | ||
hello: async (ctx) => 'world', | ||
hi: async (ctx) => 'world', | ||
ping: async (ctx) => 'pong', | ||
}); | ||
@@ -240,4 +240,4 @@ let req = new Request('http://localhost', { | ||
`; | ||
app.register(schema, (parent, args, ctx) => { | ||
return `${ctx.env.db} world`; | ||
app.handle(schema, (ctx) => { | ||
return `${ctx.runtime?.env?.db} world`; | ||
}); | ||
@@ -244,0 +244,0 @@ const req = new Request('http://localhost', { |
@@ -1,3 +0,3 @@ | ||
import type { Context } from './context'; | ||
import type { Context } from './context/context'; | ||
import type { Middleware, Next } from './types'; | ||
export declare function compose(middleware: Middleware[]): (context: Context, next?: Next) => Promise<void>; |
export { EdgeQL } from './app'; | ||
export { Context } from './context'; | ||
export { Context } from './context/context'; | ||
export type { Next } from './types'; | ||
export type { Handler, Middleware } from './types'; |
export { EdgeQL } from './app'; | ||
export { Context } from './context'; | ||
export { Context } from './context/context'; |
@@ -10,3 +10,3 @@ import { Jwt } from '../../utils/jwt'; | ||
return async (ctx, next) => { | ||
const credentials = ctx.request.headers.get('Authorization'); | ||
const credentials = ctx.http.request.headers.get('Authorization'); | ||
let token; | ||
@@ -16,5 +16,5 @@ if (credentials) { | ||
if (parts.length !== 2) { | ||
ctx.res.status = 401; | ||
ctx.res.statusText = 'Unauthorized'; | ||
ctx.res.headers.set('WWW-Authenticate', `Bearer realm="${ctx.request.url}",error="invalid_request",error_description="invalid credentials structure"`); | ||
ctx.http.status = 401; | ||
ctx.http.statusText = 'Unauthorized'; | ||
ctx.http.headers.set('WWW-Authenticate', `Bearer realm="${ctx.http.request.url}",error="invalid_request",error_description="invalid credentials structure"`); | ||
return; | ||
@@ -27,5 +27,5 @@ } | ||
if (!token) { | ||
ctx.res.status = 401; | ||
ctx.res.statusText = 'Unauthorized'; | ||
ctx.res.headers.set('WWW-Authenticate', `Bearer realm="${ctx.request.url}",error="invalid_request",error_description="no authorization included in request"`); | ||
ctx.http.status = 401; | ||
ctx.http.statusText = 'Unauthorized'; | ||
ctx.http.headers.set('WWW-Authenticate', `Bearer realm="${ctx.http.request.url}",error="invalid_request",error_description="no authorization included in request"`); | ||
return; | ||
@@ -42,5 +42,5 @@ } | ||
if (!authorized) { | ||
ctx.res.status = 401; | ||
ctx.res.statusText = msg; | ||
ctx.res.headers.set('WWW-Authenticate', `Bearer realm="${ctx.request.url}",error="invalid_token",error_description="token verification failure"`); | ||
ctx.http.status = 401; | ||
ctx.http.statusText = msg; | ||
ctx.http.headers.set('WWW-Authenticate', `Bearer realm="${ctx.http.request.url}",error="invalid_token",error_description="token verification failure"`); | ||
return; | ||
@@ -47,0 +47,0 @@ } |
@@ -10,3 +10,3 @@ /** | ||
const app = new EdgeQL(); | ||
app.register(` | ||
app.handle(` | ||
type Query { | ||
@@ -35,3 +35,3 @@ name: String | ||
const app = new EdgeQL(); | ||
app.register(` | ||
app.handle(` | ||
type Query { | ||
@@ -62,3 +62,3 @@ name: String | ||
const app = new EdgeQL(); | ||
app.register(` | ||
app.handle(` | ||
type Query { | ||
@@ -65,0 +65,0 @@ name: String |
@@ -5,3 +5,3 @@ export const wallclock = async (ctx, next) => { | ||
const endedAt = new Date(); | ||
ctx.res.headers.set('x-edgeql-wallclock', `${endedAt.getTime() - startedAt.getTime()}`); | ||
ctx.http.headers.set('x-edgeql-wallclock', `${endedAt.getTime() - startedAt.getTime()}`); | ||
}; |
@@ -1,10 +0,5 @@ | ||
import type { Context } from './context'; | ||
import type { Context } from './context/context'; | ||
export type Next = () => Promise<void>; | ||
export type Handler = (ctx: Context) => Promise<any> | any; | ||
export type Middleware = (ctx: Context, next: Next) => Promise<void | undefined>; | ||
export interface GraphQLRequest<TVariables extends VariableValues = VariableValues> { | ||
query?: string; | ||
operationName?: string; | ||
variables?: TVariables; | ||
extensions?: Record<string, any>; | ||
} | ||
export type VariableValues = { | ||
@@ -11,0 +6,0 @@ [name: string]: any; |
{ | ||
"name": "edgeql", | ||
"version": "0.0.10", | ||
"version": "0.1.0", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -20,3 +20,3 @@ # EdgeQL | ||
` | ||
app.register(schema, () => 'world') | ||
app.handle(schema, (ctx: Context) => 'world') | ||
@@ -52,2 +52,6 @@ export default app | ||
}) | ||
app.handle(helloworld) | ||
export default app | ||
``` | ||
@@ -54,0 +58,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
83
57400
44
1616