@elysiajs/graphql-yoga
Version:
Plugin for Elysia for using graphql-yoga
407 lines (406 loc) • 19.6 kB
TypeScript
import { type Elysia } from 'elysia';
import type { CreateMobius, Resolver } from 'graphql-mobius';
import { type GraphQLSchemaWithContext, type YogaServerOptions, type YogaInitialContext } from 'graphql-yoga';
import type { IExecutableSchemaDefinition } from '@graphql-tools/schema';
import type { TypeSource } from '@graphql-tools/utils';
type MaybePromise<T> = T | Promise<T>;
type Prettify<T> = {
[K in keyof T]: T[K];
} & {};
/**
* Extra type safety for Yoga
* can pass either schema prop defined by
* ElysiaYogaConfigWithSchema
*
* or
*
* typeDefs and resolvers props
* (to build schema with: createSchema())
* defined by
* ElysiaYogaConfigWithTypeDefsAndResolvers
*
*/
interface ElysiaYogaConfigWithSchema<TypeDefs extends TypeSource | never, Context extends undefined | MaybePromise<Record<string, unknown>> | ((initialContext: YogaInitialContext) => MaybePromise<unknown>)> extends Omit<YogaServerOptions<{}, {}>, 'typeDefs' | 'context' | 'cors'>, Omit<IExecutableSchemaDefinition<{}>, 'typeDefs'> {
/**
* @default /graphql
*
* path for GraphQL handler
*/
path?: string;
/**
* TypeDefs
*/
typeDefs?: undefined;
context?: Context;
schema: GraphQLSchemaWithContext<Context>;
/**
* If this field isn't presented, context type is null
* It must also contains params when used
* I don't know why please help
*/
useContext?: (_: this['context']) => void;
resolvers?: undefined;
}
interface ElysiaYogaConfigWithTypeDefsAndResolvers<TypeDefs extends TypeSource, Context extends undefined | MaybePromise<Record<string, unknown>> | ((initialContext: YogaInitialContext) => MaybePromise<unknown>)> extends Omit<YogaServerOptions<{}, {}>, 'typeDefs' | 'context' | 'cors'>, Omit<IExecutableSchemaDefinition<{}>, 'resolvers'> {
/**
* @default /graphql
*
* path for GraphQL handler
*/
path?: string;
/**
* TypeDefs
*/
typeDefs: TypeDefs;
context?: Context;
schema?: undefined;
/**
* If this field isn't presented, context type is null
* It must also contains params when used
* I don't know why please help
*/
useContext?: (_: this['context']) => void;
resolvers: Resolver<TypeDefs extends string ? CreateMobius<TypeDefs> : {
Query: Record<string, unknown>;
Mutation: Record<string, unknown>;
Subscription: Record<string, unknown>;
}, Context extends undefined ? {
request: Request;
} : Context extends (a: YogaInitialContext) => infer A ? Prettify<NonNullable<Awaited<A>> & {
request: Request;
}> : Prettify<NonNullable<Awaited<Context>> & {
request: Request;
}>>;
}
type ElysiaYogaConfig<TypeDefs extends TypeSource, Context extends undefined | MaybePromise<Record<string, unknown>> | ((initialContext: YogaInitialContext) => MaybePromise<unknown>)> = ElysiaYogaConfigWithSchema<TypeDefs, Context> | ElysiaYogaConfigWithTypeDefsAndResolvers<TypeDefs, Context>;
/**
* GraphQL Yoga supports for Elysia
*
* @example
* ```typescript
* import { Elysia } from 'elysia'
* import { yoga } from '@elysiajs/graphql-yoga'
*
* const app = new Elysia()
* .use(
* yoga({
* typeDefs: `
* type Query {
* hi: String
* }
* `,
* resolvers: {
* Query: {
* hi: () => 'Hi from Elysia'
* }
* }
* })
* )
* .listen(8080)
* ```
*
* * @example
* ```typescript
* import { Elysia } from 'elysia'
* import { yoga, createSchema } from '@elysiajs/graphql-yoga'
* const { loadFiles } = require('@graphql-tools/load-files')
*
* const schema = createSchema({
* typeDefs: await loadFiles('src/typeDefs/*.graphql')
* resolvers: await loadFiles('src/resolvers/*.{js,ts}')
* })
*
* const app = new Elysia()
* .use(
* yoga({
* schema
* })
* )
* .listen(8080)
* ```
*/
export declare const yoga: <TypeDefs extends TypeSource, Context extends undefined | MaybePromise<Record<string, unknown>> | ((initialContext: YogaInitialContext) => MaybePromise<unknown>), Prefix extends string = "/graphql">({ path, typeDefs, resolvers, resolverValidationOptions, inheritResolversFromInterfaces, updateResolversInPlace, schemaExtensions, schema, ...config }: ElysiaYogaConfig<TypeDefs, Context>) => (app: Elysia) => Elysia<"", {
decorator: {};
store: {};
derive: {};
resolve: {};
}, {
typebox: {};
error: {};
}, {
schema: {};
standaloneSchema: {};
macro: {};
macroFn: {};
parser: {};
response: {};
}, {} & import("elysia").CreateEden<import("elysia/dist/types").JoinPath<"", Prefix>, {
get: {
body: unknown;
params: import("elysia/dist/types").IsNever<keyof import("elysia").ResolvePath<import("elysia/dist/types").JoinPath<"", Prefix>>> extends true ? import("elysia").ResolvePath<Prefix> : import("elysia").ResolvePath<import("elysia/dist/types").JoinPath<"", Prefix>>;
query: unknown;
headers: unknown;
response: { [K_1 in keyof ({} extends {} & (import("elysia").EmptyRouteSchema extends Pick<import("elysia").UnwrapRoute<import("elysia").InputSchema<never>, {}, import("elysia/dist/types").JoinPath<"", Prefix>> & {}, keyof import("elysia").EmptyRouteSchema> ? {} : {
422: {
type: "validation";
on: string;
summary?: string;
message?: string;
found?: unknown;
property?: string;
expected?: string;
};
}) ? import("elysia/dist/types").ValueToResponseSchema<Response> : { [key in 200 | keyof ({} & (import("elysia").EmptyRouteSchema extends Pick<import("elysia").UnwrapRoute<import("elysia").InputSchema<never>, {}, import("elysia/dist/types").JoinPath<"", Prefix>> & {}, keyof import("elysia").EmptyRouteSchema> ? {} : {
422: {
type: "validation";
on: string;
summary?: string;
message?: string;
found?: unknown;
property?: string;
expected?: string;
};
}))]: key extends 200 ? key extends keyof ({} & (import("elysia").EmptyRouteSchema extends Pick<import("elysia").UnwrapRoute<import("elysia").InputSchema<never>, {}, import("elysia/dist/types").JoinPath<"", Prefix>> & {}, keyof import("elysia").EmptyRouteSchema> ? {} : {
422: {
type: "validation";
on: string;
summary?: string;
message?: string;
found?: unknown;
property?: string;
expected?: string;
};
})) ? import("elysia/dist/types").ValueToResponseSchema<Response>[key] | ({} & (import("elysia").EmptyRouteSchema extends Pick<import("elysia").UnwrapRoute<import("elysia").InputSchema<never>, {}, import("elysia/dist/types").JoinPath<"", Prefix>> & {}, keyof import("elysia").EmptyRouteSchema> ? {} : {
422: {
type: "validation";
on: string;
summary?: string;
message?: string;
found?: unknown;
property?: string;
expected?: string;
};
}))[key] : import("elysia/dist/types").ValueToResponseSchema<Response>[key] : key extends keyof ({} & (import("elysia").EmptyRouteSchema extends Pick<import("elysia").UnwrapRoute<import("elysia").InputSchema<never>, {}, import("elysia/dist/types").JoinPath<"", Prefix>> & {}, keyof import("elysia").EmptyRouteSchema> ? {} : {
422: {
type: "validation";
on: string;
summary?: string;
message?: string;
found?: unknown;
property?: string;
expected?: string;
};
})) ? ({} & (import("elysia").EmptyRouteSchema extends Pick<import("elysia").UnwrapRoute<import("elysia").InputSchema<never>, {}, import("elysia/dist/types").JoinPath<"", Prefix>> & {}, keyof import("elysia").EmptyRouteSchema> ? {} : {
422: {
type: "validation";
on: string;
summary?: string;
message?: string;
found?: unknown;
property?: string;
expected?: string;
};
}))[key] : never; })]: K_1 extends never ? unknown : ({} extends {} & (import("elysia").EmptyRouteSchema extends Pick<import("elysia").UnwrapRoute<import("elysia").InputSchema<never>, {}, import("elysia/dist/types").JoinPath<"", Prefix>> & {}, keyof import("elysia").EmptyRouteSchema> ? {} : {
422: {
type: "validation";
on: string;
summary?: string;
message?: string;
found?: unknown;
property?: string;
expected?: string;
};
}) ? import("elysia/dist/types").ValueToResponseSchema<Response> : { [key in 200 | keyof ({} & (import("elysia").EmptyRouteSchema extends Pick<import("elysia").UnwrapRoute<import("elysia").InputSchema<never>, {}, import("elysia/dist/types").JoinPath<"", Prefix>> & {}, keyof import("elysia").EmptyRouteSchema> ? {} : {
422: {
type: "validation";
on: string;
summary?: string;
message?: string;
found?: unknown;
property?: string;
expected?: string;
};
}))]: key extends 200 ? key extends keyof ({} & (import("elysia").EmptyRouteSchema extends Pick<import("elysia").UnwrapRoute<import("elysia").InputSchema<never>, {}, import("elysia/dist/types").JoinPath<"", Prefix>> & {}, keyof import("elysia").EmptyRouteSchema> ? {} : {
422: {
type: "validation";
on: string;
summary?: string;
message?: string;
found?: unknown;
property?: string;
expected?: string;
};
})) ? import("elysia/dist/types").ValueToResponseSchema<Response>[key] | ({} & (import("elysia").EmptyRouteSchema extends Pick<import("elysia").UnwrapRoute<import("elysia").InputSchema<never>, {}, import("elysia/dist/types").JoinPath<"", Prefix>> & {}, keyof import("elysia").EmptyRouteSchema> ? {} : {
422: {
type: "validation";
on: string;
summary?: string;
message?: string;
found?: unknown;
property?: string;
expected?: string;
};
}))[key] : import("elysia/dist/types").ValueToResponseSchema<Response>[key] : key extends keyof ({} & (import("elysia").EmptyRouteSchema extends Pick<import("elysia").UnwrapRoute<import("elysia").InputSchema<never>, {}, import("elysia/dist/types").JoinPath<"", Prefix>> & {}, keyof import("elysia").EmptyRouteSchema> ? {} : {
422: {
type: "validation";
on: string;
summary?: string;
message?: string;
found?: unknown;
property?: string;
expected?: string;
};
})) ? ({} & (import("elysia").EmptyRouteSchema extends Pick<import("elysia").UnwrapRoute<import("elysia").InputSchema<never>, {}, import("elysia/dist/types").JoinPath<"", Prefix>> & {}, keyof import("elysia").EmptyRouteSchema> ? {} : {
422: {
type: "validation";
on: string;
summary?: string;
message?: string;
found?: unknown;
property?: string;
expected?: string;
};
}))[key] : never; })[K_1]; } extends infer T ? { [K in keyof T]: T[K]; } : never;
};
}> & import("elysia").CreateEden<import("elysia/dist/types").JoinPath<"", Prefix>, {
post: {
body: unknown;
params: import("elysia/dist/types").IsNever<keyof import("elysia").ResolvePath<import("elysia/dist/types").JoinPath<"", Prefix>>> extends true ? import("elysia").ResolvePath<Prefix> : import("elysia").ResolvePath<import("elysia/dist/types").JoinPath<"", Prefix>>;
query: unknown;
headers: unknown;
response: { [K_1 in keyof ({} extends {} & (import("elysia").EmptyRouteSchema extends Pick<import("elysia").UnwrapRoute<import("elysia").InputSchema<never>, {}, import("elysia/dist/types").JoinPath<"", Prefix>> & {}, keyof import("elysia").EmptyRouteSchema> ? {} : {
422: {
type: "validation";
on: string;
summary?: string;
message?: string;
found?: unknown;
property?: string;
expected?: string;
};
}) ? import("elysia/dist/types").ValueToResponseSchema<Response> : { [key in 200 | keyof ({} & (import("elysia").EmptyRouteSchema extends Pick<import("elysia").UnwrapRoute<import("elysia").InputSchema<never>, {}, import("elysia/dist/types").JoinPath<"", Prefix>> & {}, keyof import("elysia").EmptyRouteSchema> ? {} : {
422: {
type: "validation";
on: string;
summary?: string;
message?: string;
found?: unknown;
property?: string;
expected?: string;
};
}))]: key extends 200 ? key extends keyof ({} & (import("elysia").EmptyRouteSchema extends Pick<import("elysia").UnwrapRoute<import("elysia").InputSchema<never>, {}, import("elysia/dist/types").JoinPath<"", Prefix>> & {}, keyof import("elysia").EmptyRouteSchema> ? {} : {
422: {
type: "validation";
on: string;
summary?: string;
message?: string;
found?: unknown;
property?: string;
expected?: string;
};
})) ? import("elysia/dist/types").ValueToResponseSchema<Response>[key] | ({} & (import("elysia").EmptyRouteSchema extends Pick<import("elysia").UnwrapRoute<import("elysia").InputSchema<never>, {}, import("elysia/dist/types").JoinPath<"", Prefix>> & {}, keyof import("elysia").EmptyRouteSchema> ? {} : {
422: {
type: "validation";
on: string;
summary?: string;
message?: string;
found?: unknown;
property?: string;
expected?: string;
};
}))[key] : import("elysia/dist/types").ValueToResponseSchema<Response>[key] : key extends keyof ({} & (import("elysia").EmptyRouteSchema extends Pick<import("elysia").UnwrapRoute<import("elysia").InputSchema<never>, {}, import("elysia/dist/types").JoinPath<"", Prefix>> & {}, keyof import("elysia").EmptyRouteSchema> ? {} : {
422: {
type: "validation";
on: string;
summary?: string;
message?: string;
found?: unknown;
property?: string;
expected?: string;
};
})) ? ({} & (import("elysia").EmptyRouteSchema extends Pick<import("elysia").UnwrapRoute<import("elysia").InputSchema<never>, {}, import("elysia/dist/types").JoinPath<"", Prefix>> & {}, keyof import("elysia").EmptyRouteSchema> ? {} : {
422: {
type: "validation";
on: string;
summary?: string;
message?: string;
found?: unknown;
property?: string;
expected?: string;
};
}))[key] : never; })]: K_1 extends never ? unknown : ({} extends {} & (import("elysia").EmptyRouteSchema extends Pick<import("elysia").UnwrapRoute<import("elysia").InputSchema<never>, {}, import("elysia/dist/types").JoinPath<"", Prefix>> & {}, keyof import("elysia").EmptyRouteSchema> ? {} : {
422: {
type: "validation";
on: string;
summary?: string;
message?: string;
found?: unknown;
property?: string;
expected?: string;
};
}) ? import("elysia/dist/types").ValueToResponseSchema<Response> : { [key in 200 | keyof ({} & (import("elysia").EmptyRouteSchema extends Pick<import("elysia").UnwrapRoute<import("elysia").InputSchema<never>, {}, import("elysia/dist/types").JoinPath<"", Prefix>> & {}, keyof import("elysia").EmptyRouteSchema> ? {} : {
422: {
type: "validation";
on: string;
summary?: string;
message?: string;
found?: unknown;
property?: string;
expected?: string;
};
}))]: key extends 200 ? key extends keyof ({} & (import("elysia").EmptyRouteSchema extends Pick<import("elysia").UnwrapRoute<import("elysia").InputSchema<never>, {}, import("elysia/dist/types").JoinPath<"", Prefix>> & {}, keyof import("elysia").EmptyRouteSchema> ? {} : {
422: {
type: "validation";
on: string;
summary?: string;
message?: string;
found?: unknown;
property?: string;
expected?: string;
};
})) ? import("elysia/dist/types").ValueToResponseSchema<Response>[key] | ({} & (import("elysia").EmptyRouteSchema extends Pick<import("elysia").UnwrapRoute<import("elysia").InputSchema<never>, {}, import("elysia/dist/types").JoinPath<"", Prefix>> & {}, keyof import("elysia").EmptyRouteSchema> ? {} : {
422: {
type: "validation";
on: string;
summary?: string;
message?: string;
found?: unknown;
property?: string;
expected?: string;
};
}))[key] : import("elysia/dist/types").ValueToResponseSchema<Response>[key] : key extends keyof ({} & (import("elysia").EmptyRouteSchema extends Pick<import("elysia").UnwrapRoute<import("elysia").InputSchema<never>, {}, import("elysia/dist/types").JoinPath<"", Prefix>> & {}, keyof import("elysia").EmptyRouteSchema> ? {} : {
422: {
type: "validation";
on: string;
summary?: string;
message?: string;
found?: unknown;
property?: string;
expected?: string;
};
})) ? ({} & (import("elysia").EmptyRouteSchema extends Pick<import("elysia").UnwrapRoute<import("elysia").InputSchema<never>, {}, import("elysia/dist/types").JoinPath<"", Prefix>> & {}, keyof import("elysia").EmptyRouteSchema> ? {} : {
422: {
type: "validation";
on: string;
summary?: string;
message?: string;
found?: unknown;
property?: string;
expected?: string;
};
}))[key] : never; })[K_1]; } extends infer T_1 ? { [K in keyof T_1]: T_1[K]; } : never;
};
}>, {
derive: {};
resolve: {};
schema: {};
standaloneSchema: {};
response: {};
}, {
derive: {};
resolve: {};
schema: {};
standaloneSchema: {};
response: {};
}>;
export default yoga;