小编Eli*_*eal的帖子

在 redux Action Creators 中访问 Apollo GraphQL 客户端的最佳方式?

在下面的(未经测试的)示例代码中,如果我想访问内部 Apollo GraphQL 客户端的实例actions/math.js,我必须将其从Calculator组件传递到事件处理程序,然后从WrappedCalculator事件处理程序传递到操作创建者。

这会导致大量代码膨胀。

actions/math.js对于操作创建者来说,访问 GraphQL 客户端实例的更好方式是什么?

示例代码:

常量/Queries.js

const MUTATION_APPEND_TO_AUDIT_TRAIL = gql`
    mutation MutationAppendToAuditTrail($mathOperation: String!, $operand1: Float!, $operand2: Float!) {
        appendToAuditTrail(operation: $mathOperation, operand1: $operand1, operand2: $operand2) {
            id
            operation
            operand1
            operand2
        }
    }
`;
Run Code Online (Sandbox Code Playgroud)

动作/math.js

import { INCREMENT_TOTAL_BY, MULTIPLY_TOTAL_BY } from '../constants/ActionTypes';
import { getTotal } from '../reducers';

incrementResultBy = (operand, graphQlClient) => (dispatch, getState) {
    // Use selector to get the total prior to the operation.
    const total …
Run Code Online (Sandbox Code Playgroud)

javascript apollo graphql react-redux

1
推荐指数
1
解决办法
3264
查看次数

标签 统计

apollo ×1

graphql ×1

javascript ×1

react-redux ×1