Skip to content

Commit

Permalink
Improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan committed Aug 15, 2024
1 parent 78624ad commit 393d669
Show file tree
Hide file tree
Showing 12 changed files with 156 additions and 153 deletions.
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@
"typescript.tsdk": "./node_modules/typescript/lib",
"typescript.enablePromptUseWorkspaceTsdk": true,
"deno.enablePaths": ["examples/deno", "examples/netlify-edge/netlify/edge-functions"],
"deno.importMap": "examples/netlify-edge/.netlify/edge-functions-import-map.json"
"deno.importMap": "examples/netlify-edge/.netlify/edge-functions-import-map.json",
"[typescript]": {
"editor.defaultFormatter": "vscode.typescript-language-features"
}
}
46 changes: 10 additions & 36 deletions packages/graphql-yoga/src/plugins/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export type Plugin<
// eslint-disable-next-line @typescript-eslint/ban-types
TServerContext extends Record<string, any> = {},
// eslint-disable-next-line @typescript-eslint/ban-types
TUserContext extends Record<string, any> = {},
TUserContext = {},
> = EnvelopPlugin<YogaInitialContext & PluginContext> &
ServerAdapterPlugin<TServerContext> & {
/**
Expand All @@ -45,12 +45,12 @@ export type Plugin<
* Use this hook with your own risk. It is still experimental and may change in the future.
* @internal
*/
onYogaInit?: OnYogaInitHook<TServerContext, TUserContext>;
onYogaInit?: OnYogaInitHook<TServerContext>;
/**
* Use this hook with your own risk. It is still experimental and may change in the future.
* @internal
*/
onRequestParse?: OnRequestParseHook<TServerContext & ServerAdapterInitialContext>;
onRequestParse?: OnRequestParseHook<TServerContext>;
/**
* Use this hook with your own risk. It is still experimental and may change in the future.
* @internal
Expand All @@ -60,33 +60,17 @@ export type Plugin<
* Use this hook with your own risk. It is still experimental and may change in the future.
* @internal
*/
onResultProcess?: OnResultProcess<TServerContext & ServerAdapterInitialContext>;
onResultProcess?: OnResultProcess<TServerContext>;
};

export type OnYogaInitHook<
TServerContext extends Record<string, any>,
TUserContext extends Record<string, any>,
> = (payload: OnYogaInitEventPayload<TServerContext, TUserContext>) => void;
export type OnYogaInitHook<TServerContext extends Record<string, any>> = (
payload: OnYogaInitEventPayload<TServerContext>,
) => void;

export type OnYogaInitEventPayload<
TServerContext extends Record<string, any>,
TUserContext extends Record<string, any>,
> = {
yoga: YogaServer<TServerContext, TUserContext>;
export type OnYogaInitEventPayload<TServerContext extends Record<string, any>> = {
yoga: YogaServer<TServerContext, any>;
};

export type OnRequestHook<TServerContext> = (
payload: OnRequestEventPayload<TServerContext>,
) => PromiseOrValue<void>;

export interface OnRequestEventPayload<TServerContext> {
request: Request;
serverContext: TServerContext & ServerAdapterInitialContext;
fetchAPI: FetchAPI;
endResponse(response: Response): void;
url: URL;
}

export type OnRequestParseHook<TServerContext> = (
payload: OnRequestParseEventPayload<TServerContext>,
) => PromiseOrValue<void | OnRequestParseHookResult>;
Expand Down Expand Up @@ -154,17 +138,7 @@ export interface OnResultProcessEventPayload<TServerContext> {
resultProcessor?: ResultProcessor;
acceptableMediaTypes: string[];
setResultProcessor(resultProcessor: ResultProcessor, acceptedMediaType: string): void;
serverContext: TServerContext;
}

export type OnResponseHook<TServerContext> = (
payload: OnResponseEventPayload<TServerContext>,
) => PromiseOrValue<void>;

export interface OnResponseEventPayload<TServerContext> {
request: Request;
serverContext: TServerContext | undefined;
response: Response;
serverContext: TServerContext & ServerAdapterInitialContext;
}

/**
Expand Down
9 changes: 4 additions & 5 deletions packages/graphql-yoga/src/plugins/use-graphiql.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { PromiseOrValue } from '@envelop/core';
import { YogaLogger } from '@graphql-yoga/logger';
import type { ServerAdapterInitialContext } from '@whatwg-node/server';
import graphiqlHTML from '../graphiql-html.js';
import { FetchAPI } from '../types.js';
import { Plugin } from './types.js';
Expand Down Expand Up @@ -155,11 +154,11 @@ export interface GraphiQLPluginConfig<TServerContext> {

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function useGraphiQL<TServerContext extends Record<string, any>>(
config: GraphiQLPluginConfig<TServerContext & ServerAdapterInitialContext>,
config: GraphiQLPluginConfig<TServerContext>,
// eslint-disable-next-line @typescript-eslint/ban-types
): Plugin<{}, TServerContext & ServerAdapterInitialContext> {
): Plugin<{}, TServerContext> {
const logger = config.logger ?? console;
let graphiqlOptionsFactory: GraphiQLOptionsFactory<TServerContext & ServerAdapterInitialContext>;
let graphiqlOptionsFactory: GraphiQLOptionsFactory<TServerContext>;
if (typeof config?.options === 'function') {
graphiqlOptionsFactory = config?.options;
} else if (typeof config?.options === 'object') {
Expand Down Expand Up @@ -191,7 +190,7 @@ export function useGraphiQL<TServerContext extends Record<string, any>>(
logger.debug(`Rendering GraphiQL`);
const graphiqlOptions = await graphiqlOptionsFactory(
request,
serverContext as TServerContext & ServerAdapterInitialContext,
serverContext as TServerContext,
);

if (graphiqlOptions) {
Expand Down
3 changes: 2 additions & 1 deletion packages/graphql-yoga/src/process-request.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { getOperationAST } from 'graphql';
import { GetEnvelopedFn } from '@envelop/core';
import { ExecutionArgs } from '@graphql-tools/executor';
import { ServerAdapterInitialContext } from '@whatwg-node/server';
import { OnResultProcess, ResultProcessor, ResultProcessorInput } from './plugins/types.js';
import { FetchAPI, GraphQLParams } from './types.js';

Expand All @@ -18,7 +19,7 @@ export async function processResult<TServerContext>({
* Response Hooks
*/
onResultProcessHooks: OnResultProcess<TServerContext>[];
serverContext: TServerContext;
serverContext: TServerContext & ServerAdapterInitialContext;
}) {
let resultProcessor: ResultProcessor | undefined;

Expand Down
5 changes: 2 additions & 3 deletions packages/graphql-yoga/src/schema.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { IExecutableSchemaDefinition, makeExecutableSchema } from '@graphql-tools/schema';
import type { ServerAdapterInitialContext } from '@whatwg-node/server';
import { GraphQLSchemaWithContext, YogaInitialContext } from './types.js';

// eslint-disable-next-line @typescript-eslint/ban-types
export function createSchema<TContext = {}>(
opts: IExecutableSchemaDefinition<TContext & YogaInitialContext>,
): GraphQLSchemaWithContext<TContext & YogaInitialContext & ServerAdapterInitialContext> {
return makeExecutableSchema<TContext & YogaInitialContext & ServerAdapterInitialContext>(opts);
): GraphQLSchemaWithContext<TContext & YogaInitialContext> {
return makeExecutableSchema<TContext & YogaInitialContext>(opts);
}
46 changes: 18 additions & 28 deletions packages/graphql-yoga/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
ServerAdapterRequestHandler,
useCORS,
useErrorHandling,
type ServerAdapterInitialContext,
} from '@whatwg-node/server';
import { handleError, isAbortError } from './error.js';
import { isGETRequest, parseGETRequest } from './plugins/request-parser/get.js';
Expand Down Expand Up @@ -96,7 +95,7 @@ export type YogaServerOptions<TServerContext, TUserContext> = {
*/
context?:
| ((
initialContext: YogaInitialContext & TServerContext & ServerAdapterInitialContext,
initialContext: YogaInitialContext & TServerContext,
) => Promise<TUserContext> | TUserContext)
| Promise<TUserContext>
| TUserContext
Expand Down Expand Up @@ -129,21 +128,19 @@ export type YogaServerOptions<TServerContext, TUserContext> = {
*
* @default true
*/
graphiql?: GraphiQLOptionsOrFactory<TServerContext & ServerAdapterInitialContext> | undefined;
graphiql?: GraphiQLOptionsOrFactory<TServerContext> | undefined;

renderGraphiQL?: ((options?: GraphiQLOptions) => PromiseOrValue<BodyInit>) | undefined;

schema?:
| YogaSchemaDefinition<TServerContext & ServerAdapterInitialContext, TUserContext>
| undefined;
schema?: YogaSchemaDefinition<TServerContext, TUserContext> | undefined;

/**
* Envelop Plugins
* @see https://envelop.dev/plugins
*/
plugins?:
| Array<
| Plugin<TUserContext & TServerContext & YogaInitialContext & ServerAdapterInitialContext>
| Plugin<TUserContext & TServerContext & YogaInitialContext>
| Plugin
// eslint-disable-next-line @typescript-eslint/ban-types
| {}
Expand Down Expand Up @@ -201,27 +198,21 @@ export type BatchingOptions =
export class YogaServer<
TServerContext extends Record<string, any>,
TUserContext extends Record<string, any>,
> implements ServerAdapterBaseObject<TServerContext & ServerAdapterInitialContext>
> implements ServerAdapterBaseObject<TServerContext>
{
/**
* Instance of envelop
*/
public readonly getEnveloped: GetEnvelopedFn<
TUserContext & TServerContext & YogaInitialContext & ServerAdapterInitialContext
>;
public readonly getEnveloped: GetEnvelopedFn<TUserContext & TServerContext & YogaInitialContext>;
public logger: YogaLogger;
public readonly graphqlEndpoint: string;
public fetchAPI: FetchAPI;
protected plugins: Array<
Plugin<
TUserContext & TServerContext & YogaInitialContext & ServerAdapterInitialContext,
TServerContext & ServerAdapterInitialContext,
TUserContext
>
Plugin<TUserContext & TServerContext & YogaInitialContext, TServerContext, TUserContext>
>;
private onRequestParseHooks: OnRequestParseHook<TServerContext & ServerAdapterInitialContext>[];
private onRequestParseHooks: OnRequestParseHook<TServerContext>[];
private onParamsHooks: OnParamsHook[];
private onResultProcessHooks: OnResultProcess<TServerContext & ServerAdapterInitialContext>[];
private onResultProcessHooks: OnResultProcess<TServerContext>[];
private maskedErrorsOpts: YogaMaskedErrorOpts | null;
private id: string;

Expand Down Expand Up @@ -350,8 +341,8 @@ export class YogaServer<
}),
// Middlewares after the GraphQL execution
useResultProcessors(),
useErrorHandling<TServerContext & YogaInitialContext & ServerAdapterInitialContext>(
(error, request, serverContext) => {
useErrorHandling(
(error, request, serverContext: TServerContext & ServerAdapterInitialContext) => {
const errors = handleError(error, this.maskedErrorsOpts, this.logger);

const result = {
Expand Down Expand Up @@ -428,13 +419,11 @@ export class YogaServer<

this.getEnveloped = envelop({
plugins: this.plugins,
}) as unknown as GetEnvelopedFn<
TUserContext & TServerContext & YogaInitialContext & ServerAdapterInitialContext
>;
}) as unknown as GetEnvelopedFn<TUserContext & TServerContext & YogaInitialContext>;

this.plugins = this.getEnveloped._plugins as Plugin<
TUserContext & TServerContext & YogaInitialContext & ServerAdapterInitialContext,
TServerContext & ServerAdapterInitialContext,
TUserContext & TServerContext & YogaInitialContext,
TServerContext,
TUserContext
>[];

Expand Down Expand Up @@ -471,7 +460,7 @@ export class YogaServer<
request: Request;
batched: boolean;
},
serverContext: TServerContext & ServerAdapterInitialContext,
serverContext: TServerContext,
) {
try {
let result: ExecutionResult | AsyncIterable<ExecutionResult> | undefined;
Expand All @@ -492,7 +481,7 @@ export class YogaServer<

if (result == null) {
const additionalContext =
serverContext?.request === request
serverContext.request === request
? {
params,
}
Expand Down Expand Up @@ -520,8 +509,9 @@ export class YogaServer<
/** Ensure that error thrown from subscribe is sent to client */
// TODO: this should probably be something people can customize via a hook?
if (isAsyncIterable(result)) {
const iterator = result[Symbol.asyncIterator]();
result = mapAsyncIterator(
result,
iterator,
v => v,
(err: Error) => {
if (err.name === 'AbortError') {
Expand Down
5 changes: 2 additions & 3 deletions packages/graphql-yoga/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
import type { GraphQLSchema } from 'graphql';
import type { PromiseOrValue } from '@envelop/core';
import type { createFetch } from '@whatwg-node/fetch';

export type { ServerAdapterInitialContext } from '@whatwg-node/server';
import { ServerAdapterInitialContext } from '@whatwg-node/server';

export type GraphQLSchemaWithContext<TContext> = GraphQLSchema & {
_context?: TContext;
Expand All @@ -19,7 +18,7 @@ export interface GraphQLParams<
extensions?: TExtensions;
}

export interface YogaInitialContext {
export interface YogaInitialContext extends ServerAdapterInitialContext {
/**
* GraphQL Parameters
*/
Expand Down
4 changes: 2 additions & 2 deletions packages/nestjs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export abstract class AbstractYogaDriver<

const schema = this.mergeConditionalSchema<'express'>(conditionalSchema, options.schema);

const yoga = createYoga<YogaDriverServerContext<'express'>, never>({
const yoga = createYoga<YogaDriverServerContext<'express'>>({
...options,
schema,
graphqlEndpoint: options.path,
Expand Down Expand Up @@ -152,7 +152,7 @@ export abstract class AbstractYogaDriver<

const schema = this.mergeConditionalSchema<'fastify'>(conditionalSchema, options.schema);

const yoga = createYoga<YogaDriverServerContext<'fastify'>, never>({
const yoga = createYoga<YogaDriverServerContext<'fastify'>>({
...options,
schema,
graphqlEndpoint: options.path,
Expand Down
Loading

0 comments on commit 393d669

Please sign in to comment.