I have a react app with a keystone.js backend and a graphql api
I have a list of products in keystones.js and a simple graphql query
import gql from "graphql-tag";
export const ALL_PRODUCTS_QUERY = gql`
query ProductData{
allProducts{
id
price
description
name
}
}
`
I'm using apollo codegen to generate the types for the graphql so I get
export interface ProductData_allProducts {
__typename: "Product";
id: string;
price: number | null;
description: string | null;
name: string | null;
}
export interface ProductData {
/**
* Search for all Product items which match the where clause.
*/
allProducts: (ProductData_allProducts | null)[] | null;
}
In React I can list the products and use the types in the code, here I'm using <ProductData>
import { useQuery } from "@apollo/client";
import {ALL_PRODUCTS_QUERY} from '../queries/index'
import { ProductData } from "../generated/ProductData";
const Products = () => {
const {data, error, loading} = useQuery<ProductData>(ALL_PRODUCTS_QUERY)
if(loading) return <p>Loading</p>
if(error) return <p>Error: {error.message}</p>
return (
<div>
<div>
{data?.allProducts?.map(product => (
<div>{product?.name}</div>
))}
</div>
</div>
);
};
export default Products;
Instead of using <div>{product?.name}</div> I would like to create a Product component
import React from 'react';
import { ProductData, ProductData_allProducts } from '../generated/ProductData';
const Product = ({product}:ProductData_allProducts) => {
return (
<p>{product.name}</p>
);
};
export default Product;
but what should the type be for product here I get an error saying
Property 'product' does not exist on type 'ProductData_allProducts'.
and on the Products page
import { useQuery } from "@apollo/client";
import {ALL_PRODUCTS_QUERY} from '../queries/index'
import { ProductData } from "../generated/ProductData";
import Product from "./Product";
const Products = () => {
const {data, error, loading} = useQuery<ProductData>(ALL_PRODUCTS_QUERY)
if(loading) return <p>Loading</p>
if(error) return <p>Error: {error.message}</p>
return (
<div>
<div>
{data?.allProducts?.map(product => (
<Product product={product} />
))}
</div>
</div>
);
};
export default Products;
I now get an error on the product prop
Type '{ product: ProductData_allProducts | null; }' is not assignable to type 'IntrinsicAttributes & ProductData_allProducts'.
Property 'product' does not exist on type 'IntrinsicAttributes & ProductData_allProducts'.
So what should the types be on the Product page when passing in the product