【问题标题】:How can I add 2 buildService (upload and header) to apollo federation?如何将 2 buildService(上传和标头)添加到 apollo federation?
【发布时间】:2022-01-22 08:30:57
【问题描述】:

在 apollo federation 中是否有任何选项可以同时添加标头和上传到 buildService? 我想要我的标头进行身份验证和上传以上传文件。 buildService 不支持返回对象。

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?

【问题讨论】:

    标签: apollo-server apollo-federation


    【解决方案1】:

    找到解决方案,如果您扩展 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 });
      },
    });
    

    【讨论】:

      猜你喜欢
      • 2019-12-08
      • 2021-05-02
      • 2022-11-10
      • 2020-04-12
      • 2012-12-23
      • 2019-08-08
      • 2017-07-10
      • 2016-09-25
      • 1970-01-01
      相关资源
      最近更新 更多