【问题标题】:How to add hmac in grapqhl queries from client?如何在来自客户端的 graphql 查询中添加 hmac?
【发布时间】:2019-07-26 10:59:20
【问题描述】:

我们在 React Native 项目中使用 apolloclient,谁能指导我们如何在使用它进行的查询中添加 hmac?

【问题讨论】:

    标签: react-native graphql apollo-client


    【解决方案1】:

    希望对你有帮助

    import ApolloClient, { gql } from 'apollo-boost';
    import { InMemoryCache } from 'apollo-cache-inmemory';
    import { print } from 'graphql/language/printer';
    
    const baseURL = 'http://localhost:3000/';
    const cache = new InMemoryCache({
        addTypename: false
    });
    
    const apolloClient = new ApolloClient({
        uri: baseURL,
        cache,
        request: (operation) => {
            const { query, variables, operationName } = operation;
            const payload = {
                variables,
                operationName,
                query: print(query)
            }
            console.log(payload)
            // custom headers
            const headers = {
                'X-API-KEY': '332dace0-e629-48bf-b664-d550ebfc828c',
                'X-APP-ID': '52557818-b027-43a8-bf45-fb87f98cd96e',
                'content-type': 'application/xjson',
                'Authorization': token ? `Bearer ${token}` : '',
            }
            console.log(headers)
            const token = ''; // create HMAC here
            operation.setContext({
                headers: {
                    ...headers,
                }
            })
        }
    });

    【讨论】:

      猜你喜欢
      • 2019-07-26
      • 2020-06-14
      • 2021-05-04
      • 1970-01-01
      • 2020-01-23
      • 1970-01-01
      • 2018-02-18
      • 2017-11-28
      • 1970-01-01
      相关资源
      最近更新 更多