我在React和阿波罗中使用graphql :generate来从graphql模式创建类型。
我还使用react阿波罗-钩子useQuery输出数据.
一切似乎都正常,我可以看到输出的数据
我的问题是,我不知道如何声明生成的类型,然后在vc代码中获取intelliSense。
查询
import { gql } from 'apollo-boost';
export const GET_ALL_RECIPES = gql`
query RecipesData{
recipe{
_id
name
description
}
}
`生成类型
/* 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; 如何正确地将RecipesData或RecipesData_recipe在App.tsx中声明为使用intelliSense for recipe.name
发布于 2022-04-27 12:56:26
文档总是有帮助的。只需导入生成的...Data接口和/或...Vars接口(如果您正在查询),然后使用它来转换useQuery。
import { RecipesData } from '../generated/RecipesData';
const { data, loading } = useQuery<RecipesData>(GET_ALL_RECIPES, {
suspend: false
});然后简单地以更干净的方式返回组件:
return (
<div>
{loading ? (
<p>Loading ...</p>
) : (
<ul>
{data && data.recipe.map(recipe => (
<li key={recipe._id}>{recipe.name}</li>
))}
</ul>
)}
</div>
);https://stackoverflow.com/questions/59076988
复制相似问题