Update: Hinzugefügt einige PräzisierungenPassing ownProps in die Komponente von Apollo gewickelt graphql
ich das Apollo graphql
Wrapper bin mit einer Komponente zu wickeln. Ich möchte die onPaymentLoaded
Eigenschaft von OwnProps
in die verpackte Funktion senden. Mein Versuch, dies zu tun, ist unten gezeigt. Der Code geht jedoch nicht über den TypeScript-Compiler hinaus, wenn ich auch onPaymentLoaded
nicht als Teil der Result
-Schnittstelle einschließe. Das ist sehr verwirrend. Mein Verständnis ist, dass Result
angibt, was aus der Abfrage zurückkommt - das ist nur Payment
. Warum also beschwert sich der Compiler, wenn ich onPaymentLoaded
nicht hinzufüge?
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}
/>
);
}
);
Hallo Daniel, vielen Dank für Ihre ausführliche Antwort. Ich bin zu einer ähnlichen Schlussfolgerung gekommen, seit ich meine Frage gestellt habe - also markiere ich deine Antwort als richtig. Ich habe jedoch eine unbeantwortete Frage. Bitte beachten Sie mein Update zu der Frage und dem Code. Es wäre toll, wenn Sie Ihre Gedanken zur Verfügung stellen könnten. P.S. (1) Ich spring noch nicht zum Komponieren, weil ich zuerst versuche, die grundlegende Verwendung des 'graphql'-HOCs zu verstehen. (2) Du magst Recht haben, dass ich Requisiten überhaupt nicht brauchen darf - habe das noch nicht probiert. – Naresh
Hallo @Naresh, Ich habe einen Punkt hochgeladen, wo ich Ihren Code auf die Art und Weise umstrukturiert habe, wie ich es normalerweise mache. Hoffe es wird helfen! https://gist.github.com/rasdaniil/67e12967a42571519bfbc59a8e870739 –
Das ist sehr hilfreich, @Daniel. Nochmals vielen Dank dafür, dass Sie sich die Zeit dafür genommen haben. – Naresh