'React, Graphql schema type declaration

I'm using graphql in React and Apollo:generate to create types from the graphql schema.

I'm also using react-apollo-hooks useQuery to output the data.

Everything seems to be working and I can see the data outputted

My problem is I can't see how to declare the types generated to then get intelliSense in vc code.

Query

import { gql } from 'apollo-boost';

export const GET_ALL_RECIPES = gql`

  query RecipesData{
    recipe{
      _id
      name
      description
    }
  }
`

Generated Types

/* tslint:disable */
/* eslint-disable */
// This file was automatically generated and should not be edited.

// ====================================================
// GraphQL query operation: RecipesData
// ====================================================

export interface RecipesData_recipe {
  __typename: "Recipe";
  _id: string | null;
  name: string | null;
  description: string | null;
}

export interface RecipesData {
  recipe: (RecipesData_recipe | null)[] | null;
}   

App.tsx

import React from 'react';
import './App.css';

import { useQuery } from 'react-apollo-hooks';
import { GET_ALL_RECIPES } from '../queries';

import { RecipesData_recipe } from '../generated/RecipesData';

const App: React.FC<RecipesData> = () => {

  const { data, loading } = useQuery(GET_ALL_RECIPES, {
    suspend: false
  })

  if (loading || !data) return <div>Loading</div>

  return (
    <ul>
      {data.recipe.map((recipe) =>
        <li key={recipe._id}>{recipe.name}</li>
      )}
    </ul>
  );
}

export default App; 

How do I correctly declare the RecipesData or RecipesData_recipe in App.tsx to use the intelliSense for recipe.name



Solution 1:[1]

Docs are always helpful. Simply import the generated ...Data interface and/or ...Vars interface (if you're querying) and then use it to cast the useQuery.

import { RecipesData } from '../generated/RecipesData';

const { data, loading } = useQuery<RecipesData>(GET_ALL_RECIPES, {
  suspend: false
});

Then simply return the component in a more cleaner way:

return (
  <div>
    {loading ? (
      <p>Loading ...</p>
    ) : (
      <ul>
        {data && data.recipe.map(recipe => (
          <li key={recipe._id}>{recipe.name}</li>
        ))}
      </ul>
    )}
  </div>
);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mohamed Mohamed