在下面的(未经测试的)示例代码中,如果我想访问内部 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)