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

Resolve paths relative to config file #10279

Open
aaronadamsCA opened this issue Feb 4, 2025 · 4 comments
Open

Resolve paths relative to config file #10279

aaronadamsCA opened this issue Feb 4, 2025 · 4 comments

Comments

@aaronadamsCA
Copy link

Is your feature request related to a problem? Please describe.

We have a multi-project GraphQL config in our monorepo root.

projects:
  foo:
    schema: ./schemas/foo.graphql
    documents: ./apps/foo/**/*.graphql
    extensions:
      codegen:
        generates:
          ./apps/foo/graphql.ts:
            plugins:
              - typescript
              - typescript-operations
              - typed-document-node
  bar:
    schema: ./schemas/bar.graphql
    documents: ./apps/bar/**/*.graphql
    extensions:
      codegen:
        generates:
          ./apps/bar/graphql.ts:
            plugins:
              - typescript
              - typescript-operations
              - typed-document-node
              

When I try to load this config in a child package using loadContext(), it tries to resolve each configured path relative to the current working directory. Obviously that doesn't work, because those paths are relative to the config file directory.

Describe the solution you'd like

I think relative paths in config files should always be resolved relative to the location of the config file.

If that's not an option due to backward compatibility, maybe you could add a root option similar to Vite:

https://vite.dev/config/shared-options.html#root

That way we could set root: import.meta.dirname in a JS/TS config file to ensure all paths are correctly resolved.

Describe alternatives you've considered

I've tried setting absolute paths (or resolving them myself in a TS config), but this breaks GraphQL-ESLint and the VS Code extension.

I've tried restating configs in each package that needs them, but this duplication is fragile/hard to get right.

Any additional important details?

No response

@eddeee888
Copy link
Collaborator

Hi @aaronadamsCA , this looks like a config in graphql.config.ts?
If so, I wonder if it's an issue for https://github.com/kamilkisiela/graphql-config?

@aaronadamsCA
Copy link
Author

I wasn't sure, @eddeee888! I'm not familiar enough with either package, so I don't know if this is a problem with GraphQL Config, or with how GraphQL Code Generator invokes it. I saw lots of references to process.cwd() in this package's config resolution module, so I figured I'd ask here first.

If you think I should file the same issue upstream, I can do that.

@eddeee888
Copy link
Collaborator

Hi @aaronadamsCA , I think you are right, Codegen is loading GraphQL Config and use it if available.

I've had a look at the code and the cwd is either GraphQL Config dirpath OR process.cwd() (There could be other things overriding this later, I can take a deeper look)

In your example, could you help me understand:

  • Where would you run codegen for each of your project? e.g. at the workspace root or in each project root?
  • What would be the desired graphql.config.ts ?

@hexid
Copy link

hexid commented Feb 13, 2025

@eddeee888 I had done a bit of digging into vite-plugin-graphql-codegen, with my notes here.

Ideally, vite-plugin-graphql-codegen would replace the call toCodegenContext.getConfig, with CodegenContext.updateConfig like below:

codegenContext.updateConfig({
  ...configOverride,
  ...overrideConfig,
  watch: false,
});
return await generate(codegenContext);

It looks like the this wasn't originally used because it could cause side effects in watch mode. The call to getConfig() was used as a way to clone the context, but it lost some important information as a result.


For my particular use case, I have a graphql.config.ts file at the root of my monorepo, with multiple packages under ./packages/{a,b,c} and a custom plugin at ./codegen.plugin.cjs. I'm able to run gql-gen -p a from within ./packages/{a} without any issue.
After patching vite-plugin-graphql-codegen with the above change, I'm receiving the following error:

Error: 
        Unable to find template plugin matching './codegen.plugin.cjs'
        Install one of the following packages:

        
        - @graphql-codegen/./codegen.plugin.cjs
        - @graphql-codegen/./codegen.plugin.cjs-template
        - @graphql-codegen/./codegen.plugin.cjs-plugin
        - graphql-codegen-./codegen.plugin.cjs
        - graphql-codegen-./codegen.plugin.cjs-template
        - graphql-codegen-./codegen.plugin.cjs-plugin
        - codegen-./codegen.plugin.cjs
        - codegen-./codegen.plugin.cjs-template
        - ./codegen.plugin.cjs
      
    at getPluginByName (file:///path/to/workspace/root/node_modules/@graphql-codegen/cli/esm/plugins.js:34:11)

It looks like this is due to some uses of process.cwd() that aren't configurable.
I'm not entirely sure why this works from the CLI as it doesn't seem to matter which working directory I'm running from.


const possibleModules = possibleNames.concat(resolve(process.cwd(), name));

These two functions could be modified to accept a cwd argument that defaults to process.cwd().

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

3 participants