Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .changeset/quiet-mice-jam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
'@envelop/core': major
---

Remove `enableIf` utility in favor of more type safe way to conditionally enable plugins. It wasn't a great experience to have a utility

We can easily replace usage like this:

```diff
- import { envelop, useMaskedErrors, enableIf } from '@envelop/core'
+ import { envelop, useMaskedErrors } from '@envelop/core'
import { parse, validate, execute, subscribe } from 'graphql'

const isProd = process.env.NODE_ENV === 'production'

const getEnveloped = envelop({
parse,
validate,
execute,
subscribe,
plugins: [
// This plugin is enabled only in production
- enableIf(isProd, useMaskedErrors())
+ isProd && useMaskedErrors()
]
})
```
25 changes: 17 additions & 8 deletions packages/core/src/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,26 @@ import {
SubscribeFunction,
ParseFunction,
ValidateFunction,
Optional,
} from '@envelop/types';
import { isPluginEnabled, PluginOrDisabledPlugin } from './enable-if.js';
import { createEnvelopOrchestrator, EnvelopOrchestrator } from './orchestrator.js';

export function envelop<PluginsType extends Plugin<any>[]>(options: {
plugins: Array<PluginOrDisabledPlugin>;
type ExcludeFalsy<TArray extends any[]> = Exclude<TArray[0], null | undefined | false>[];

function notEmpty<T>(value: Optional<T>): value is T {
return value != null;
}

export function envelop<PluginsType extends Optional<Plugin<any>>[]>(options: {
plugins: PluginsType;
enableInternalTracing?: boolean;
parse: ParseFunction;
execute: ExecuteFunction;
validate: ValidateFunction;
subscribe: SubscribeFunction;
}): GetEnvelopedFn<ComposeContext<PluginsType>> {
const plugins = options.plugins.filter(isPluginEnabled);
const orchestrator = createEnvelopOrchestrator<ComposeContext<PluginsType>>({
}): GetEnvelopedFn<ComposeContext<ExcludeFalsy<PluginsType>>> {
const plugins = options.plugins.filter(notEmpty);
const orchestrator = createEnvelopOrchestrator<ComposeContext<ExcludeFalsy<PluginsType>>>({
plugins,
parse: options.parse,
execute: options.execute,
Expand All @@ -31,7 +37,10 @@ export function envelop<PluginsType extends Plugin<any>[]>(options: {
const getEnveloped = <TInitialContext extends ArbitraryObject>(
initialContext: TInitialContext = {} as TInitialContext
) => {
const typedOrchestrator = orchestrator as EnvelopOrchestrator<TInitialContext, ComposeContext<PluginsType>>;
const typedOrchestrator = orchestrator as EnvelopOrchestrator<
TInitialContext,
ComposeContext<ExcludeFalsy<PluginsType>>
>;
typedOrchestrator.init(initialContext);

return {
Expand All @@ -46,5 +55,5 @@ export function envelop<PluginsType extends Plugin<any>[]>(options: {

getEnveloped._plugins = plugins;

return getEnveloped as GetEnvelopedFn<ComposeContext<PluginsType>>;
return getEnveloped as GetEnvelopedFn<ComposeContext<ExcludeFalsy<PluginsType>>>;
}
27 changes: 0 additions & 27 deletions packages/core/src/enable-if.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@ export * from './plugins/use-schema.js';
export * from './plugins/use-error-handler.js';
export * from './plugins/use-extend-context.js';
export * from './plugins/use-payload-formatter.js';
export * from './enable-if.js';
export { resolversHooksSymbol } from './traced-schema.js';
31 changes: 0 additions & 31 deletions packages/core/test/utils.spec.ts

This file was deleted.

6 changes: 3 additions & 3 deletions packages/testing/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import {
subscribe,
validate,
} from 'graphql';
import { useSchema, envelop, PluginOrDisabledPlugin, isAsyncIterable } from '@envelop/core';
import { GetEnvelopedFn, Plugin } from '@envelop/types';
import { useSchema, envelop, isAsyncIterable } from '@envelop/core';
import { GetEnvelopedFn, Optional, Plugin } from '@envelop/types';
import { mapSchema as cloneSchema, isDocumentNode } from '@graphql-tools/utils';

export type ModifyPluginsFn = (plugins: Plugin<any>[]) => Plugin<any>[];
Expand Down Expand Up @@ -97,7 +97,7 @@ export type TestkitInstance = {
};

export function createTestkit(
pluginsOrEnvelop: GetEnvelopedFn<any> | Array<PluginOrDisabledPlugin>,
pluginsOrEnvelop: GetEnvelopedFn<any> | Parameters<typeof envelop>['0']['plugins'],
schema?: GraphQLSchema
): TestkitInstance {
const toGraphQLErrorOrThrow = (thrownThing: unknown): GraphQLError => {
Expand Down
3 changes: 1 addition & 2 deletions packages/testing/test/test.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { enableIf } from '@envelop/core';
import { assertSingleExecutionValue, createTestkit } from '@envelop/testing';
import { Plugin } from '@envelop/types';
import { makeExecutableSchema } from '@graphql-tools/schema';
Expand Down Expand Up @@ -98,7 +97,7 @@ describe('Test the testkit', () => {
onValidate: jest.fn().mockReturnValue(undefined),
};

const testkit = createTestkit([plugin1, enableIf(false, plugin2)], createSchema());
const testkit = createTestkit([plugin1, false && plugin2], createSchema());
const result = await testkit.execute('query test { foo }');
assertSingleExecutionValue(result);
expect(plugin1.onParse).toBeCalled();
Expand Down
2 changes: 1 addition & 1 deletion packages/types/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export type ArbitraryObject = Record<string | number | symbol, any>;
export type PromiseOrValue<T> = T | Promise<T>;
export type AsyncIterableIteratorOrValue<T> = T | AsyncIterableIterator<T>;
export type Maybe<T> = T | null | undefined;

export type Optional<T> = T | Maybe<T> | false;
export interface ObjMap<T> {
[key: string]: T;
}
Expand Down
27 changes: 0 additions & 27 deletions website/docs/core.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -153,30 +153,3 @@ const getEnveloped = envelop({
]
})
```

### Utilities

#### enableIf

This utility is helpful when you want to enable a plugin only when a certain condition is met.

```ts
import { envelop, useMaskedErrors, enableIf } from '@envelop/core'
import { parse, validate, execute, subscribe } from 'graphql'

const isProd = process.env.NODE_ENV === 'production'

const getEnveloped = envelop({
parse,
validate,
execute,
subscribe,
plugins: [
// This plugin is enabled only in production
enableIf(isProd, useMaskedErrors()),
// you can also pass function
enableIf(isProd, () => useMaskedErrors())
// ... other plugins ...
]
})
```