如何在 Apollo GraphQL 中添加标题:iOS [英] How to add header in Apollo GraphQL : iOS

查看:28
本文介绍了如何在 Apollo GraphQL 中添加标题:iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在使用 Apollo GraphQL 方法在一个项目中工作,并且它工作正常.但是现在客户端需要使用 Apollo API 添加额外的标头.但是在添加标头后,API 的响应返回为 unAuthorized.

Hy I am working in a project with Apollo GraphQL method and its working fine. But now the client required for adding additional header with Apollo API's. But after adding the header the API's response return as unAuthorized.

我将标题添加为,

    let apolloAuth: ApolloClient = {
        let configuration = URLSessionConfiguration.default

        // Add additional headers as needed
        configuration.httpAdditionalHeaders = ["Authorization" : self.singleTonInstance.token]
        configuration.httpAdditionalHeaders = ["channel" : "mobile"]

        let url = URL(string: "http://xxx/graphql")!

        return ApolloClient(networkTransport: HTTPNetworkTransport(url: url, configuration: configuration))

    }()

请任何人帮助我了解如何使用 Apollo GraphQL 添加标头.

Anyone please help me to find out how to add headers with Apollo GraphQL.

推荐答案

我终于找到了答案.按如下方式添加标题,

Finally I found the answer. Add the header in the following way,

 let apolloAuth: ApolloClient = {
        let configuration = URLSessionConfiguration.default

        let token = UserDefaults.standard.value(forKey: "token")
        // Add additional headers as needed
        configuration.httpAdditionalHeaders = ["authorization":"\(token!)", "channel":"mobile"]
        let url = URL(string: "http://xxxx/graphql")!

        return ApolloClient(networkTransport: HTTPNetworkTransport(url: url, configuration: configuration))

    }()

希望对某人有所帮助.

这篇关于如何在 Apollo GraphQL 中添加标题:iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆