Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow enableIf to accept a funciton #888

Merged
merged 5 commits into from
Oct 27, 2021
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
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