Reputation: 21
I need help trying to typescript a mutation and Query. The documentation and examples are limited and i'm struggling to manage to understand how to do it. I don't really understand the last property from the documentation. It seems to be input, response, variables, ?. https://www.apollographql.com/docs/react/recipes/static-typing
query and mutation below where I had to use any:
interface Data {
loading: DataValue<boolean> | undefined;
isLinkValid: DataValue<boolean> | undefined;
}
interface InputProps {
location: {
search: string;
};
showToast: Function;
}
interface Variables {
id: string | string[] | null | undefined;
}
const validateLinkQuery = graphql<InputProps, Data, Variables, Data>(VALIDATE_LINK_MUTATION, {
options: ({ location }) => {
const params = QueryString.parse(location.search);
const id = params.id;
return {
variables: { id },
};
},
props: ({
ownProps,
data,
}: {
ownProps: {
showToast: Function;
};
data?: any;
}) => {
const { validateLink, loading, error } = data;
if (error) {
ownProps.showToast(
Type.ERROR,
get(error, 'graphQLErrors[0].message', 'An error occured on the validate link query')
);
}
return {
isLinkValid: validateLink,
loading,
};
},
});
const validateUserMutation = graphql(
VALIDATE_CARD_MUTATION,
{
props: ({ ownProps, mutate }) => ({
validateCard: (access: SubmitAccessInput) =>
mutate({
variables: {
access,
},
})
.then((response: any) => response)
.catch((error: any) => {
ownProps.showToast(
Type.ERROR,
get(error, 'graphQLErrors[0].message', 'An error occurred while signing up for an account')
);
}),
}),
}
);```
Upvotes: 2
Views: 3242
Reputation: 709
I would use https://github.com/dotansimha/graphql-code-generator library which generator type and React Apollo HOC component with type based on your graphql schema.
After you can do something like this.
import * as React from "react";
import { Mutation } from "react-apollo";
import { gql } from "apollo-boost";
import { RouteComponentProps } from "react-router-dom";
import { LoginMutationVariables, LoginMutation } from "../../schemaTypes";
import { meQuery } from "../../graphql/queries/me";
import { userFragment } from "../../graphql/fragments/userFragment";
import { Form } from "./Form";
const loginMutation = gql`
mutation LoginMutation($email: String!, $password: String!) {
login(email: $email, password: $password) {
...UserInfo
}
}
${userFragment}
`;
export class LoginView extends React.PureComponent<RouteComponentProps<{}>> {
render() {
return (
<Mutation<LoginMutation, LoginMutationVariables>
update={(cache, { data }) => {
if (!data || !data.login) {
return;
}
cache.writeQuery({
query: meQuery,
data: { me: data.login }
});
}}
mutation={loginMutation}
>
{(mutate, { client }) => (
<Form
buttonText="login"
onSubmit={async data => {
// optional reset cache
await client.resetStore();
const response = await mutate({
variables: data
});
console.log(response);
this.props.history.push("/account");
}}
/>
)}
</Mutation>
);
}
}
Here LoginMutationVariables and LoginMutation type are being generated through graphql-code-generator.
graphql-code-generator
also generate `React Apollo Mutation/Query Hoc component with type for all mutation and queries. So you don't even need to pass these type. After generating HOC component, you can write same component like this
<LoginMutationComponent>
... rest of the code
</LoginMutationComponent>
rather than doing like this
<Mutation<LoginMutation, LoginMutationVariables>
But you need to configure graphql-code-generator. if you want to generate HOC component for queries and mutation
Upvotes: 2