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

plugin-mock: does not respect the if configuration at runtime #5029

Closed
matthewdfleming opened this issue Jan 12, 2023 · 2 comments
Closed

plugin-mock: does not respect the if configuration at runtime #5029

matthewdfleming opened this issue Jan 12, 2023 · 2 comments

Comments

@matthewdfleming
Copy link

In the api documentation for the mock plugin. The document refers to the 'if' block as follows:

if (type: Boolean) - If this expression is truthy, mocking would be enabled You can use environment variables expression, for example: ${MOCKING_ENABLED}

The YAML type here is Boolean and the plugin evaluates this data but doesn't work for expressions:
const configIf = config != null && 'if' in config ? config.if : true;

So where does that leave us?

With this config:

plugins:
  - mock:
      if: false
      mocks:
        - apply: DateTime
          custom: graphql-scalars#DateTimeMock
      ....

The mocking will not be enabled. The boolean value will be checked according to how YAML works. So things like Yes, No, y, n, true, false, etc will work as expected.

However, this really only works at 'build' time and is not dynamic during runtime as the documentation seems to indicate.

With this config:

plugins:
  - mock:
      if: ${ENV_VAR}
      mocks:
        - apply: DateTime
          custom: graphql-scalars#DateTimeMock
      ....

The string interpolation works for the 'build' but is hardcoded to a string specified in the environment variable during the build step.

Results

So the result of all this is that putting ${MOCKING_ENABLED} into the yaml (as the documents say) means:

  • at build time only
  • is there any environment variable specified when running the build
  • if so, the if (and mocking) will be enabled. It doesn't matter what the value of the env var is.
  • if not specified (at the root only), the mocking will be disabled but this seems like it is on accident (due to null being specified).
@matthewdfleming
Copy link
Author

I'm not sure if the intent of these plugins is supposed to be a runtime or a build time only situation. However, for me runtime is much more useful than build time as I want to be able to toggle mocking on and off with environment variables like we do with the endpoint URL specifications in handlers.

@ardatan
Copy link
Owner

ardatan commented Mar 31, 2023

Now mock plugin supports JS expressions like process.env.MOCK != null;
e587b14

@ardatan ardatan closed this as completed Mar 31, 2023
This was referenced Apr 30, 2024
This was referenced May 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants