reactjs Codegen配置无法加载自定义架构加载程序

bxjv4tth  于 2023-03-29  发布在  React
关注(0)|答案(1)|浏览(123)

我尝试为我的react设置codegen,我使用自定义模式加载从我的API端点加载模式,但每次都出错

Failed to load schema from pulse-api:
Failed to load custom loader: codegen_schema_loader.js
Error: Failed to load custom loader: codegen_schema_loader.js

下面是我的codegen配置文件(codegen.ts)

import type { CodegenConfig } from '@graphql-codegen/cli';

const config: CodegenConfig = {
  overwrite: true,
  schema: [{
    'pulse-api': {
      loader: './codegen_schema_loader.js',
      assumeValidSDL: true,
    }
  }],
  documents: 'src/adapters/worktango/queries/*.graphql',
  generates: {
    'src/gql/': {
      preset: 'client',
      plugins: ['typescript', 'typescript-operations', 'typescript-react-apollo'],
    },
    './graphql.schema.json': {
      plugins: ["introspection"]
    }
  },
  require: ['ts-node/register'],
};

export default config;

这里是自定义模式加载器(codegen_schema_loader.js),与配置文件位于同一文件夹

import { buildClientSchema, getIntrospectionQuery } from "graphql";
import { graphqlUrl } from "./src/config/settings/worktango.settings";
import fetch from 'cross-fetch'
import { tokens } from "./src/adapters";

export default async () => {
  const introspectionQuery = getIntrospectionQuery()
  const authToken = await tokens.getAccessToken();

  const response = await fetch(graphqlUrl, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Access-Token': authToken
    },
    body: JSON.stringify({ query: introspectionQuery })
  })

  const data = await response.json()

  return buildClientSchema(data.data)
}

我对codegen进程进行了调试分析,发现下面的函数加载器返回null,这是不应该的。看起来像是一个bug,但我不确定,有人知道如何修复这个问题吗?

async function useCustomLoader(loaderPointer, cwd) {
    let loader;
    if (typeof loaderPointer === 'string') {
        loader = await getCustomLoaderByPath(loaderPointer, cwd);
    }
    else if (typeof loaderPointer === 'function') {
        loader = loaderPointer;
    }
    if (typeof loader !== 'function') {
        throw new Error(`Failed to load custom loader: ${loaderPointer}`);
    }
    return loader;
}
r9f1avp5

r9f1avp51#

我也遇到了同样的问题。如果你看看useCustomLoader函数loaderPointer可以是一个函数。我改变了我的codegen.ts文件,直接使用我的加载器函数,它现在工作了。绝对看起来像一个bug。
将您的codegen.ts更改为如下所示

import loader from './codegen_schema_loader.js'

const config: CodegenConfig = {
  ...
  schema: [{
    'pulse-api': {
      loader,
      assumeValidSDL: true,
    }
  }],
  ...
};

相关问题