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

查看:62
本文介绍了如何在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))

    }()

希望它将对某人有所帮助.

Hope it will help someone.

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

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