danielwaltz / vite-plugin-graphql-codegen

Zero-config vite plugin that uses the vite file watcher to run graphql codegen programmatically without needing to start a separate watcher
https://www.npmjs.com/package/vite-plugin-graphql-codegen
MIT License
83 stars 8 forks source link

bug: codegen with config via plugin causes ERR_INVALID_ARG_TYPE #13

Closed lrstanley closed 2 years ago

lrstanley commented 2 years ago

I have the following vite.config.ts file (converting from a codegen.yaml file):

// file: vite.config.ts

// [...]
import { defineConfig } from "vite"
import codegen from "vite-plugin-graphql-codegen"
// [...]

export default defineConfig({
  // [...]
  plugins: [
    codegen({
      enableWatcher: true,
      config: {
        errorsOnly: true,
        schema: "./../../../internal/database/graphql/schema/*.gql",
        documents: "./src/lib/api/*.gql",
        generates: {
          "./src/lib/api/graphql.ts": {
            plugins: ["typescript", "typescript-operations", "typescript-vue-urql"],
            config: {
              preResolveTypes: true,
              nonOptionalTypename: true,
              skipTypeNameForRoot: true,
              useTypeImports: true,
              inputMaybeValue: "T | Ref<T> | ComputedRef<T>",
            },
            hooks: {
              afterOneFileWrite: ["pnpm exec prettier --write"],
            },
          },
        },
      },
    }),
  ],
  // [...]
})

When starting vite, I run into the following error

# [...]
✔ Parse Configuration
❯ Generate outputs
  ❯ Generate to ./src/lib/api/graphql.ts
    ✔ Load GraphQL schemas
    ⠙ Load GraphQL documents
    ◼ Generate
node:internal/errors:484
    ErrorCaptureStackTrace(err);
    ^

TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined
    at new NodeError (node:internal/errors:393:5)
    at validateString (node:internal/validators:161:11)
    at Object.normalize (node:path:1128:5)
    at normalizePath (/home/myuser/myproject/public/node_modules/.pnpm/vite@3.1.6/node_modules/vite/dist/node-cjs/publicUtils.cjs:3428:32)
    at isCodegenConfig (/home/myuser/myproject/public/node_modules/.pnpm/vite-plugin-graphql-codegen@2.3.0_4h7b5qb2olf2rqq35afqw572d4/node_modules/vite-plugin-graphql-codegen/src/utils/isCodegenConfig.ts:8:38)
    at FSWatcher.listener (/home/myuser/myproject/public/node_modules/.pnpm/vite-plugin-graphql-codegen@2.3.0_4h7b5qb2olf2rqq35afqw572d4/node_modules/vite-plugin-graphql-codegen/src/index.ts:154:26)
    at FSWatcher.emit (node:events:525:35)
    at FSWatcher.emit (node:domain:489:12)
    at FSWatcher.emitWithAll (file:///home/myuser/myproject/public/node_modules/.pnpm/vite@3.1.6/node_modules/vite/dist/node/chunks/dep-db16f19c.js:51419:8)
    at FSWatcher._emit (file:///home/myuser/myproject/public/node_modules/.pnpm/vite@3.1.6/node_modules/vite/dist/node/chunks/dep-db16f19c.js:51511:8) {
  code: 'ERR_INVALID_ARG_TYPE'
}

Node.js v18.9.0

Disabling enableWatcher works without issue, so I suspect it's related to the file glob references for schema and documents with the file watcher specifically. I know the docs mention only file paths, but curious if globs aren't supported either (even though they are standard local files)?

Version Info

dependencies:
@fullhuman/postcss-purgecss 5.0.0             @tailwindcss/forms 0.5.3                      autoprefixer 10.4.12                          typescript 4.8.4                              
@graphql-codegen/cli 2.13.5                   @types/node 18.8.3                            core-js 3.25.5                                unplugin-auto-import 0.11.2                   
@graphql-codegen/typescript 2.7.3             @urql/exchange-retry 1.0.0                    daisyui 2.31.0                                unplugin-icons 0.14.11                        
@graphql-codegen/typescript-operations 2.5.3  @urql/vue 1.0.2                               graphql 16.6.0                                unplugin-vue-components 0.22.8                
@graphql-codegen/typescript-vue-urql 2.3.3    @vitejs/plugin-vue 3.1.2                      graphql-tag 2.12.6                            unplugin-vue-router 0.2.3                     
@headlessui/vue 1.7.3                         @vue/tsconfig 0.1.3                           nprogress 0.2.0                               vite 3.1.6                                    
@iconify-json/fa6-brands 1.1.6                @vueuse/core 9.3.0                            pinia 2.0.22                                  vite-plugin-graphql-codegen 2.3.0             
@iconify-json/fa6-regular 1.1.6               @vueuse/integrations 9.3.0                    postcss 8.4.17                                vite-plugin-vue-layouts 0.7.0                 
@iconify-json/fa6-solid 1.1.7                 @vueuse/motion 2.0.0-beta.12                  tailwindcss 3.1.8                             vue 3.2.40                                    
@iconify-json/twemoji 1.1.5                   @vueuse/router 9.3.0                          ts-node 10.9.1                                vue-router 4.1.5                              

devDependencies:
@rushstack/eslint-patch 1.2.0            @vue/eslint-config-prettier 7.0.0        eslint-config-prettier 8.5.0             eslint-plugin-vue 9.6.0                  
@typescript-eslint/eslint-plugin 5.39.0  @vue/eslint-config-typescript 11.0.2     eslint-plugin-prettier 4.2.1             prettier 2.7.1                           
@typescript-eslint/parser 5.39.0         eslint 8.25.0                            eslint-plugin-tailwindcss 3.6.2
alexvuka1 commented 2 years ago

I ran into the same bug and after a bit of digging, I found the cause. The problem is that when a new CodegenContext is created at src/index.ts:115, neither filepath nor graphqlConfig is passed to its constructor which results in filepath being set to undefined. Later on, the normalize function is called with CodegenContext's filepath whose value is undefined, but the function expects a string, therefore it throws an error. My temporary solution was to patch src/index.ts:115 locally:

-          codegenContext = new import_cli.CodegenContext({ config });
+          codegenContext = new import_cli.CodegenContext({ config, filepath: '' });

This is obviously not the most elegant way of "fixing" it, but it works for the time being. Hope this helps!

danielwaltz commented 2 years ago

Ah, good catch! This looks like a relatively simple fix. I will hopefully be able to get a release out for this soon!

danielwaltz commented 2 years ago

Alright, should be fixed now! Feel free to reopen this issue or create another one if the problem persists after updating.

Thanks for letting me know about this and helping to track down the issue!

lrstanley commented 2 years ago

Works great, thanks!