support new configuration option moduleNamingMode
#346
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
TL;DR
Prior to this change, schema files had to be flattened within modules; otherwise, this plugin would break. This update introduces a new option,
moduleNamingMode
, that allows for deeply nested schema files. The change is backward compatible with existing workflows.Background
When parsing GraphQL schema files, the plugin identifies the "module name" for each schema file. It currently determines the module name by taking the parent directory of each schema file. This approach works well when the last folder denotes the module name, as seen in nested-domain-modules.
Problem
The current behavior causes issues when a project organizes schema files within additional subfolders inside a module. For example, our project structure looks like this:
We configure
codegen.ts
to generate aresolvers/
directory in each module because we value separating implementation from API definitions. However, this setup breaks the plugin. In the example above, the module name for bothprofile/schema/User.graphql
anduser/schema/User.graphql
is incorrectly identified asschema
. This happens because the plugin takes the parent directory (schema
) as the module name.As a result, the definitions are merged under the same module name
schema
, andUser
resolvers are only generated for one of the modules—depending on the order in which the files are processed—leading to unpredictable behavior.Solution
To address this issue, I've added a new option
moduleNamingMode
that allows clients to specify how the module name is determined:last
(default): The module name is the last folder in a file's path (existing behavior).first
: The module name is the first folder (within the working directory) in a file's path.Using
first
means that if codegen is pointed to/path/to/modules
, the module names will be the first layer of folders within/path/to/modules
(e.g.,user
andprofile
). This allows for deeper nesting within modules without breaking the plugin.Please refer to the e2e test included in this PR for an example of how this works in practice.