gpt4 book ai didi

javascript - 如何使用 Apollo 在 React 中执行 GraphQL 查询 onClick?

转载 作者:行者123 更新时间:2023-12-02 23:13:45 29 4
gpt4 key购买 nike

我正在尝试根据foodType过滤特定的餐馆它作为字符串传递,例如(“Chicken”,“Pizza”等)。

我希望能够做到onClick在我的 React 组件中。

我尝试用 <ApolloPriver> 包裹起来包装器进行查询,但运气不佳。

FoodType.tsx

export const FoodTypes: React.SFC = () => {
return (
<div className="food-types">
<ul className="food-type-list">
<li className="food-type-item" onClick={async () => {
const { data } = await client.query({
query: FOODTYPE_QUERY,
variables: { foodType: "Pizza" }
})}}>Pizza</li>
<li className="food-type-item" onClick={}>Chicken</li>
<li className="food-type-item" onClick={}>Indian</li>
<li className="food-type-item" onClick={}>Chinese</li>
<li className="food-type-item" onClick={}>English</li>
<li className="food-type-item" onClick={}>Fish & Chips</li>
<li className="food-type-item" onClick={}>Kebab</li>
<li className="food-type-item" onClick={}>Curry</li>
<li className="food-type-item" onClick={}>Caribbean</li>
</ul>
)}

我想要传递的 GQL 查询

const FOODTYPE_QUERY = gql`
query foodTypeQuery($foodType: String!) {
getFoodType(foodType: $foodType) {
id
name
address
foodType
}
}
`;

我目前如何显示 FoodTypes.tsx 中的所有餐馆。这就是我想要结果的地方 onClick上面的按钮。

<div className={"EateryWrapper"}>
<ApolloProvider client={client}>
<Query query={EATERY_QUERY}>
{({ loading, data }: any) => {
if (loading) return "Loading...";
const { eateries } = data;
return eateries.map((eatery: any) => (
<EateryItem
key={eatery.id}
id={eatery.id}
name={eatery.name}
address={eatery.address}
foodType={eatery.foodType}
/>
));
}}
</Query>
</ApolloProvider>
</div>

整体FoodTypes.tsx

import React from "react";
import "./FoodTypes.scss";
import { ApolloProvider, Query } from "react-apollo";
import { gql } from "apollo-boost";

import { client } from "../..";
import { EateryItem } from "../Eatery/EateryItem";

const EATERY_QUERY = gql`
{
eateries {
id
name
address
foodType
}
}
`;

const FOODTYPE_QUERY = gql`
query foodTypeQuery($foodType: String!) {
getFoodType(foodType: $foodType) {
id
name
address
foodType
}
}
`;

export const FoodTypes: React.SFC = () => {
return (
<div className="food-types">
<ul className="food-type-list">
<li className="food-type-item" onClick={async () => {
const { data } = await client.query({
query: FOODTYPE_QUERY,
variables: { foodType: "Pizza" }
})
console.log(data)}
}>Pizza</li>
<li className="food-type-item">Chicken</li>
<li className="food-type-item">Indian</li>
<li className="food-type-item">Chinese</li>
<li className="food-type-item">English</li>
<li className="food-type-item">Fish & Chips</li>
<li className="food-type-item">Kebab</li>
<li className="food-type-item">Curry</li>
<li className="food-type-item">Caribbean</li>
</ul>
<div className={"EateryWrapper"}>
<ApolloProvider client={client}>
<Query query={EATERY_QUERY}>
{({ loading, data }: any) => {
if (loading) return "Loading...";
const { eateries } = data;
return eateries.map((eatery: any) => (
<EateryItem
key={eatery.id}
id={eatery.id}
name={eatery.name}
address={eatery.address}
foodType={eatery.foodType}
/>
));
}}
</Query>
</ApolloProvider>
</div>
</div>
);
};

export default FoodTypes;

已更新 FoodTypes.tsx

import React, { Component, useState } from "react";
import "./FoodTypes.scss";
import { ApolloProvider, Query } from "react-apollo";
import { gql } from "apollo-boost";

import { client } from "../..";
import { EateryItem } from "../Eatery/EateryItem";

const EATERY_QUERY = gql`
{
eateries {
id
name
address
foodType
}
}
`;

const FOODTYPE_QUERY = gql`
query foodTypeQuery($foodType: String!) {
getFoodType(foodType: $foodType) {
id
name
address
foodType
}
}
`;

type foodTypeFilterProps = {
foodTypeName: String
}

export const FoodTypes: React.SFC = () => {
return (
<div className="food-types">
<ul className="food-type-list">
<FoodTypeFilter foodTypeName={'Italian'}/>
<FoodTypeFilter foodTypeName={'Pizza'}/>
<FoodTypeFilter foodTypeName={'Chicken'}/>
<FoodTypeFilter foodTypeName={'Indian'}/>
<FoodTypeFilter foodTypeName={'Chinese'}/>
<FoodTypeFilter foodTypeName={'English'}/>
<FoodTypeFilter foodTypeName={'Fish & Chips'}/>
<FoodTypeFilter foodTypeName={'Kebab'}/>
<FoodTypeFilter foodTypeName={'Curry'}/>
<FoodTypeFilter foodTypeName={'Caribbean'}/>
</ul>
<div className={"EateryWrapper"}>
<ApolloProvider client={client}>
<Query query={EATERY_QUERY}>
{({ loading, data }: any) => {
if (loading) return "Loading...";
const { eateries } = data;
return eateries.map((eatery: any) => (
<EateryItem
key={eatery.id}
id={eatery.id}
name={eatery.name}
address={eatery.address}
foodType={eatery.foodType}
/>
));
}}
</Query>
</ApolloProvider>
</div>
</div>
);
};

const FoodTypeFilter: React.FunctionComponent<foodTypeFilterProps> = props => {
const [foodType, setFoodType] = useState(props.foodTypeName);
return(
<li className="food-type-item" onClick={async () => {
const { data } = await client.query({
query: FOODTYPE_QUERY,
variables: { foodType: props.foodTypeName }
})
setFoodType(foodType);
}}>{foodType}
</li>
)
}

export default FoodTypes;

让上述元素使用 onClick 之上的 GQL 查询的最佳方法是什么?

最佳答案

首先,我建议您将 FoodTypes 组件更改为 React 组件,以便能够将从 graphql 查询返回的数据存储在 state

export class classFoodTypes extends React.Component 

现在,在您的 classFoodTypes 组件中,您可以在获取数据后调用 setState:

<li className="food-type-item" onClick={async () => {
const { data } = await client.query({
query: FOODTYPE_QUERY,
variables: { foodType: "Pizza" }
})}
this.setState({eateries: data})}>Pizza</li>

从那里,您可以显示来自状态的数据:

            <EateryItem
key={this.state.eateries.id}
id={this.state.eateries.id}
name={this.state.eateries.name}
address={this.state.eateries.address}
foodType={this.state.eateries.foodType}
/>

我不会完成组件的其余部分的实现,以便您可以练习实现,但此模式应该有助于引导您走向正确的方向。如果您在此过程中遇到困难,请告诉我。

关于javascript - 如何使用 Apollo 在 React 中执行 GraphQL 查询 onClick?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57237677/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com