업데이트를 graphql : 어떤 해명전달 ownProps는
내가 구성 요소를 포장 아폴로 graphql
래퍼를 사용하고 추가되었습니다. OwnProps
의 onPaymentLoaded
속성을 래핑 된 함수로 보내고 싶습니다. 이 작업을 수행하려는 시도가 아래에 나와 있습니다. 그러나 Result
인터페이스의 일부로 onPaymentLoaded
을 포함하지 않으면 코드가 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}
/>
);
}
);
안녕 다니엘, 상세한 응답을 주셔서 감사합니다. 나는 내 질문을 올린 이후 비슷한 결론에 도달했다 - 나는 당신의 대답을 정확하다고 표시했다. 그러나 나는 답이 하나도 없다. 질문과 코드에 대한 업데이트를 참조하십시오. 당신의 생각을 제공 할 수 있다면 좋을 것입니다. 추신 (1) 나는'graphql' HOC의 기본적인 사용법을 처음으로 이해하려고하기 때문에 아직 작성하지 않았습니다. (2) 나는 소품의 변형이 전혀 필요하지 않을지도 모르지만 아직 시도하지 않았다. – Naresh
안녕하세요 @Naresh, 나는 내가 일반적으로하는 방식으로 코드를 리팩토링 한 요점을 업로드했습니다. 희망이 도움이 될 것입니다! https://gist.github.com/rasdaniil/67e12967a42571519bfbc59a8e870739 –
@ 대니얼, 매우 도움이됩니다. 시간을내어 다시 해 주셔서 감사합니다. – Naresh