如何将 2 个 buildService(上传和 header)添加到 apollo federation?

How can I add 2 buildService (upload and header) to apollo federation?

是否有任何选项可以添加 header 并上传到 apollo federation 中的 buildService? 我想要我的 headers 用于身份验证和上传用于上传文件。 buildService 不支持 return object.

const gateway = new ApolloGateway({
  buildService({ name, url }) {
    return new RemoteGraphQLDataSource({
      url,
      willSendRequest({ request, context }: { request: any; context: any }) {
        request?.http?.headers?.set(
          "authorization",
          context.auth ? context.auth : ""
        );
        request?.http?.session?.set(
          "session",
          context.session ? context.session : ""
        );
        request?.http?.sessionStore?.set(
          "sessionStore",
          context.sessionStore ? context.sessionStore : ""
        );
      },
    });
  },
});

如何将 upload file 添加到我的 buildService?

找到解决方案,如果您以某种方式扩展 FileUploadDataSource:

import FileUploadDataSource from "@profusion/apollo-federation-upload";

class AuthenticationAndUpload extends FileUploadDataSource {
  willSendRequest({
    request,
    context,
  }: {
    request: GraphQLRequest;
    context: any;
  }) {
    request?.http?.headers?.set(
      "authorization",
      context.auth ? context.auth : ""
    );
  }
}

const gateway = new ApolloGateway({
  buildService({ url }) {
    return new AuthenticationAndUpload({ url });
  },
});