Skip to content

Commit

Permalink
feat: make envelop take in engine functions
Browse files Browse the repository at this point in the history
  • Loading branch information
saihaj committed Aug 22, 2022
1 parent 726519d commit a84ea46
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 28 deletions.
16 changes: 13 additions & 3 deletions packages/core/src/create.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
import { GetEnvelopedFn, ComposeContext, Plugin, ArbitraryObject } from '@envelop/types';
import type { execute, parse, subscribe, validate } from 'graphql';
import { isPluginEnabled, PluginOrDisabledPlugin } from './enable-if.js';
import { createEnvelopOrchestrator, EnvelopOrchestrator, GraphQLEngine } from './orchestrator.js';
import { createEnvelopOrchestrator, EnvelopOrchestrator } from './orchestrator.js';
import { traceOrchestrator } from './traced-orchestrator.js';

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

if (options.enableInternalTracing) {
orchestrator = traceOrchestrator(orchestrator);
Expand Down
35 changes: 13 additions & 22 deletions packages/core/src/orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,31 +64,22 @@ export type EnvelopOrchestrator<
getCurrentSchema: () => Maybe<GraphQLSchema>;
};

export const GraphQLEngine = ({
parseFn,
executeFn,
validateFn,
subscribeFn,
}: {
parseFn?: typeof parse;
executeFn?: typeof execute;
validateFn?: typeof validate;
subscribeFn?: typeof subscribe;
}) => {
return {
parse: parseFn ?? parse,
execute: executeFn ?? execute,
validate: validateFn ?? validate,
subscribe: subscribeFn ?? subscribe,
};
type EnvelopOrchestratorOptions = {
plugins: Plugin[];
parse: typeof parse;
execute: typeof execute;
subscribe: typeof subscribe;
validate: typeof validate;
};

export function createEnvelopOrchestrator<PluginsContext extends DefaultContext>(
plugins: Plugin[],
engine: typeof GraphQLEngine = GraphQLEngine
): EnvelopOrchestrator<any, PluginsContext> {
export function createEnvelopOrchestrator<PluginsContext extends DefaultContext>({
plugins,
parse,
execute,
subscribe,
validate,
}: EnvelopOrchestratorOptions): 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
9 changes: 9 additions & 0 deletions packages/core/test/extends.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ import { createSpiedPlugin, createTestkit } from '@envelop/testing';
import { envelop, useExtendContext, useLogger, useSchema } from '../src/index.js';
import { useEnvelop } from '../src/plugins/use-envelop.js';
import { schema, query } from './common.js';
import { parse, execute, validate, subscribe } from 'graphql';

describe('extending envelops', () => {
it('should allow to extend envelops', async () => {
const spiedPlugin = createSpiedPlugin();

const baseEnvelop = envelop({
plugins: [useLogger(), spiedPlugin.plugin],
parse,
execute,
validate,
subscribe,
});

const onExecuteChildSpy = jest.fn();
Expand All @@ -21,6 +26,10 @@ describe('extending envelops', () => {
onExecute: onExecuteChildSpy,
},
],
parse,
execute,
validate,
subscribe,
});

const teskit = createTestkit(instance);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { GraphQLSchema } from 'graphql';
import { envelop, useSchema } from '@envelop/core';
import { useApolloServerErrors } from '../src/index.js';
import { assertSingleExecutionValue } from '@envelop/testing';
import { parse, execute, validate, subscribe } from 'graphql';

// Fix compat by mocking broken function
// we can remove this once apollo fixed legacy usages of execute(schema, ...args)
Expand All @@ -15,7 +16,13 @@ jest.mock('../../../../node_modules/apollo-server-core/dist/utils/schemaHash', (
describe('useApolloServerErrors', () => {
const executeBoth = async (schema: GraphQLSchema, query: string, debug: boolean) => {
const apolloServer = new ApolloServerBase({ schema, debug });
const envelopRuntime = envelop({ plugins: [useSchema(schema), useApolloServerErrors({ debug })] })({});
const envelopRuntime = envelop({
plugins: [useSchema(schema), useApolloServerErrors({ debug })],
parse,
execute,
validate,
subscribe,
})({});

return {
apollo: await apolloServer.executeOperation({ query }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { assertSingleExecutionValue, createTestkit } from '@envelop/testing';
import { makeExecutableSchema } from '@graphql-tools/schema';
import { buildSchema, GraphQLError, parse } from 'graphql';
import { useExtendedValidation } from '../src/index.js';
import { parse as gqlParse, execute as gqlExecute, validate as gqlValidate, subscribe as gqlSubscribe } from 'graphql';

describe('useExtendedValidation', () => {
it('supports usage of multiple useExtendedValidation in different plugins', async () => {
Expand Down Expand Up @@ -114,6 +115,10 @@ describe('useExtendedValidation', () => {
rules: [() => ({})],
}),
],
parse: gqlParse,
execute: gqlExecute,
validate: gqlValidate,
subscribe: gqlSubscribe,
})();
await expect(
execute({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { oneLine, stripIndent } from 'common-tags';
import { diff } from 'jest-diff';
import { envelop, useSchema } from '@envelop/core';
import { useFragmentArguments } from '../src/index.js';
import { parse as gqlParse, execute as gqlExecute, validate as gqlValidate, subscribe as gqlSubscribe } from 'graphql';

function compareStrings(a: string, b: string): boolean {
return a.includes(b);
Expand Down Expand Up @@ -66,7 +67,13 @@ describe('useFragmentArguments', () => {
}
`);
test('can inline fragment with argument', () => {
const { parse } = envelop({ plugins: [useFragmentArguments(), useSchema(schema)] })({});
const { parse } = envelop({
plugins: [useFragmentArguments(), useSchema(schema)],
parse: gqlParse,
execute: gqlExecute,
validate: gqlValidate,
subscribe: gqlSubscribe,
})({});
const result = parse(/* GraphQL */ `
fragment TestFragment($c: String) on Query {
a(b: $c)
Expand Down
21 changes: 20 additions & 1 deletion packages/testing/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
import { DocumentNode, ExecutionResult, getOperationAST, GraphQLError, GraphQLSchema, print } from 'graphql';
import {
DocumentNode,
ExecutionResult,
getOperationAST,
GraphQLError,
GraphQLSchema,
print,
execute,
parse,
subscribe,
validate,
} from 'graphql';
import { useSchema, envelop, PluginOrDisabledPlugin, isAsyncIterable } from '@envelop/core';
import { GetEnvelopedFn, Plugin } from '@envelop/types';
import { mapSchema as cloneSchema, isDocumentNode } from '@graphql-tools/utils';
Expand Down Expand Up @@ -101,13 +112,21 @@ export function createTestkit(
let getEnveloped = Array.isArray(pluginsOrEnvelop)
? envelop({
plugins: [...(schema ? [useSchema(cloneSchema(schema))] : []), ...pluginsOrEnvelop],
parse,
execute,
validate,
subscribe,
})
: pluginsOrEnvelop;

return {
modifyPlugins(modifyPluginsFn: ModifyPluginsFn) {
getEnveloped = envelop({
plugins: [...(schema ? [useSchema(cloneSchema(schema))] : []), ...modifyPluginsFn(getEnveloped._plugins)],
parse,
execute,
validate,
subscribe,
});
},
mockPhase(phaseReplacement: PhaseReplacementParams) {
Expand Down

0 comments on commit a84ea46

Please sign in to comment.