Skip to content

Commit

Permalink
feat: allow enableIf to accept a funciton (#888)
Browse files Browse the repository at this point in the history
  • Loading branch information
saihaj authored Oct 27, 2021
1 parent c35ace2 commit 3458917
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/tame-dingos-invite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@envelop/core': minor
---

Allow functions returning a plugin in `enableIf` and lazy load plugin by avoiding running the init flow of plugin if value is false.
8 changes: 6 additions & 2 deletions packages/core/src/enable-if.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export function isPluginEnabled(t: PluginOrDisabledPlugin): t is Plugin {
/**
* Utility function to enable a plugin.
*/
export function enableIf(condition: boolean, plugin: Plugin): PluginOrDisabledPlugin {
return condition ? plugin : EnableIfBranded.DisabledPlugin;
export function enableIf(condition: boolean, plugin: Plugin | (() => Plugin)): PluginOrDisabledPlugin {
if (condition) {
return typeof plugin === 'function' ? plugin() : plugin;
} else {
return EnableIfBranded.DisabledPlugin;
}
}
16 changes: 16 additions & 0 deletions packages/core/test/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { useLogger, enableIf } from '@envelop/core';
import { createTestkit, createSpiedPlugin } from '@envelop/testing';
import { getIntrospectionQuery, parse } from 'graphql';
import { isIntrospectionDocument } from '../src/utils';
import { query, schema } from './common';

describe('Utils', () => {
describe('isIntrospectionDocument', () => {
Expand Down Expand Up @@ -39,5 +41,19 @@ describe('Utils', () => {
const plugin = enableIf(false, useLogger());
expect(plugin).toBeFalsy();
});

it('Should not init plugin', async () => {
const spiedPlugin = createSpiedPlugin();
const testkit = createTestkit([enableIf(false, spiedPlugin.plugin)], schema);
await testkit.execute(query);
expect(spiedPlugin.spies.beforeExecute).not.toHaveBeenCalled();
});

it('Should init plugin', async () => {
const spiedPlugin = createSpiedPlugin();
const testkit = createTestkit([enableIf(true, spiedPlugin.plugin)], schema);
await testkit.execute(query);
expect(spiedPlugin.spies.beforeExecute).toHaveBeenCalled();
});
});
});
2 changes: 2 additions & 0 deletions website/docs/core.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ const getEnveloped = envelop({
plugins: [
// This plugin is enabled only in production
enableIf(isProd, useMaskedErrors()),
// you can also pass function
enableIf(isProd, () => useMaskedErrors()),
// ... other plugins ...
],
});
Expand Down

1 comment on commit 3458917

@vercel
Copy link

@vercel vercel bot commented on 3458917 Oct 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.