typescripteslintapolloreact-apollo

Unsafe array destructuring of a tuple element with an `any` value using Apollo useQuery


I am using TypeScript and have a GraphQL query definition called using Apollo's useQuery. Documentation says the call should be typed, but I receive an ESLint error for data being an any value. How can I fix this?

export const listProducerOrgStewards = gql`
    query listProducerOrgStewards($user: String) {
        listProducerOrgStewards(user: $user) {
            ...
    }
`;

const { loading, data, error } = useQuery(listProducerOrgStewards, {
    fetchPolicy: 'cache-and-network',
});

Solution

  • You have a few options:

    1. Pass in auto-generated types to useQuery. This would look something like the following:
    import { ListProducerOrgStewardsQuery, ListProducerOrgStewardsQueryVariables
     } from 'src/gql/types.ts`
    
    
    const { loading, data, error } = useQuery<ListProducerOrgStewardsQuery, ListProducerOrgStewardsQueryVariables>(listProducerOrgStewards, {
        fetchPolicy: 'cache-and-network',
    });
    
    1. You could add @graphql-codegen/client-preset to your project to make your data variable typed. This would require some codegen config changes.