【发布时间】:2018-04-17 23:31:51
【问题描述】:
更新:添加了一些说明
我正在使用 Apollo graphql 包装器来包装组件。我想将onPaymentLoaded 中的onPaymentLoaded 属性发送到包装函数中。我的尝试如下所示。但是,如果我不将 onPaymentLoaded 也包含在 Result 接口的一部分中,则代码不会通过 TypeScript 编译器。这非常令人困惑。我的理解是Result 指定了从查询返回的内容——即Payment。那么如果我不添加onPaymentLoaded,为什么编译器会抱怨呢?
const PAYMENT_QUERY = gql`
query payment($id: ID!) {
payment(id: $id) {
id
amount
}
}
`;
interface Result {
payment: Payment;
// ----- If I don't include onPaymentLoaded in the Result,
// I get the error shown below. I don't understand why.
// onPaymentLoaded is not part of the query result!!!
onPaymentLoaded: (payment: Payment) => void;
}
type WrappedProps = Result & QueryProps;
interface OwnProps {
paymentid: string;
onPaymentLoaded: (payment: Payment) => void;
}
const withPayment = graphql<
Result,
OwnProps,
WrappedProps
>(PAYMENT_QUERY, {
options: ({ paymentid }) => ({
variables: { id: paymentid }
}),
props: ({ data, ownProps }) => ({ ...data, ownProps })
});
export const PaymentContainer = withPayment(
// ----- Error if interface Result above does not include onPaymentLoaded:
// Type 'Response & QueryProps<OperationVariables> &
// { children?: ReactNode; }' has no property 'onPaymentLoaded'
// and no string index signature."
({ loading, error, payment, onPaymentLoaded }) => {
return (
<PaymentView
loading={loading}
error={error}
payment={payment}
onPaymentLoaded={onPaymentLoaded}
/>
);
}
);
【问题讨论】:
标签: typescript graphql-js apollo react-apollo