“错误错误:参数 2 `isExtractable` 必须是一个函数。” 使用 Apollo Angular 上传文件时

Wol*_*eur 5 file-upload apollo graphql apollo-client angular

根据https://apollo-angular.com/docs/data/network#file-upload,为了使用 Apollo Angular 上传文件,您必须添加context: {useMultipart: true}到 graphQL 查询中,并将该extractFiles函数添加到 httpLink 创建中。

但是,我不断收到此错误。似乎isExtractableFile没有使用默认功能,我不知道为什么会这样。

这是我的graphql.module.ts

const uri = environment.graphQLUrl; // <-- add the URL of the GraphQL server here
export function createApollo(httpLink: HttpLink): ApolloClientOptions<any> {
  return {
    link: httpLink.create({uri, useMultipart: true, extractFiles}),
    cache: new InMemoryCache(),
  };
}

@NgModule({
  exports: [ApolloModule],
  providers: [
    {
      provide: APOLLO_OPTIONS,
      useFactory: createApollo,
      deps: [HttpLink],
    },
  ],
})
export class GraphQLModule {}
Run Code Online (Sandbox Code Playgroud)

小智 2

从Angular-Apollo存储库上的一个未解决问题中找到了解决方案。

import extractFiles from 'extract-files/extractFiles.mjs';
import isExtractableFile from 'extract-files/isExtractableFile.mjs';

httpLink.create({
  ...
  extractFiles: (body) => extractFiles(body, isExtractableFile),
});
Run Code Online (Sandbox Code Playgroud)