Skip to content

Commit

Permalink
feat: more cleaner GraphQL Engine
Browse files Browse the repository at this point in the history
  • Loading branch information
saihaj committed Aug 17, 2022
1 parent ad5f346 commit d5c17ce
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 15 deletions.
8 changes: 6 additions & 2 deletions packages/core/src/create.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import { GetEnvelopedFn, ComposeContext, Plugin, ArbitraryObject } from '@envelop/types';
import { isPluginEnabled, PluginOrDisabledPlugin } from './enable-if.js';
import { createEnvelopOrchestrator, EnvelopOrchestrator } from './orchestrator.js';
import { createEnvelopOrchestrator, EnvelopOrchestrator, GraphQLEngine, Engine } from './orchestrator.js';
import { traceOrchestrator } from './traced-orchestrator.js';
import { parse, validate, subscribe, execute } from 'graphql';

const defaultEngine = GraphQLEngine({ parse, validate, subscribe, execute });

export function envelop<PluginsType extends Plugin<any>[]>(options: {
plugins: Array<PluginOrDisabledPlugin>;
enableInternalTracing?: boolean;
engine?: Engine;
}): GetEnvelopedFn<ComposeContext<PluginsType>> {
const plugins = options.plugins.filter(isPluginEnabled);
let orchestrator = createEnvelopOrchestrator<ComposeContext<PluginsType>>(plugins);
let orchestrator = createEnvelopOrchestrator<ComposeContext<PluginsType>>(plugins, options?.engine ?? defaultEngine);

if (options.enableInternalTracing) {
orchestrator = traceOrchestrator(orchestrator);
Expand Down
43 changes: 30 additions & 13 deletions packages/core/src/orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,7 @@ import {
DefaultContext,
Maybe,
} from '@envelop/types';
import {
DocumentNode,
execute,
ExecutionResult,
GraphQLError,
GraphQLSchema,
parse,
specifiedRules,
subscribe,
validate,
ValidationRule,
} from 'graphql';
import { DocumentNode, ExecutionResult, GraphQLError, GraphQLSchema, specifiedRules, ValidationRule } from 'graphql';
import { prepareTracedSchema, resolversHooksSymbol } from './traced-schema.js';
import {
errorAsyncIterator,
Expand All @@ -64,10 +53,38 @@ export type EnvelopOrchestrator<
getCurrentSchema: () => Maybe<GraphQLSchema>;
};

export const GraphQLEngine = ({
parse,
execute,
validate,
subscribe,
}: {
parse?: Function;
execute?: Function;
validate?: Function;
subscribe?: Function;
}) => {
const noop = (name: string) => {
throw new Error(`'${name}' is not implemented.`);
};

return () => {
return {
parse: parse ?? noop('parse'),
execute: execute ?? noop('execute'),
validate: validate ?? noop('validate'),
subscribe: subscribe ?? noop('subscribe'),
};
};
};
export type Engine = ReturnType<typeof GraphQLEngine>;

export function createEnvelopOrchestrator<PluginsContext extends DefaultContext>(
plugins: Plugin[]
plugins: Plugin[],
engine: Engine
): EnvelopOrchestrator<any, PluginsContext> {
let schema: GraphQLSchema | undefined | null = null;
const { parse, execute, validate, subscribe } = engine();
let initDone = false;
const onResolversHandlers: OnResolverCalledHook[] = [];
for (const plugin of plugins) {
Expand Down

0 comments on commit d5c17ce

Please sign in to comment.