【发布时间】:2020-10-03 20:37:42
【问题描述】:
概述
应用最初查询 GraphQL API 并使用 TagsList 呈现“标签”(字符串)。
然后用户可以点击一个标签,然后这个标签被传递给ShortList并被渲染。
我想做什么?
我有第二个名为 GQLSIMILARTAGS 的 GraphQL 查询,它接受一个字符串变量,并在 GQLFuncSecond 中使用 useQuery 调用。我想在用户点击其中一个标签时触发它。
我知道查询可以正常工作,如下所示,硬编码searchLabel 作为变量传入。
<GQLFuncSecond searchLabel={"cloud"} />
问题
当用户单击来自TagsList 的标签时,如何将字符串 (data.tag.label) 传递到此 useQuery 变量中。
我不知道如何将用户选择的标签(当它添加到 ShortList 时)传递到我的 GQLFuncSecond 函数中。
例如,我应该在ShortList 中调用GQLFuncSecond 吗?我该怎么做?
主应用
function WrappedApp(props) {
const [favourites, setFavourites] = useState([]);
function GQLFuncSecond(props) {
const { loading, error, data } = useQuery(GQLSIMILARTAGS, {
variables: { search_label: props.searchLabel },
});
if (loading) return <p>Loading...</p>;
if (error) return <p>Error :(</p>;
if (data) return <RelatedTagData data={data.tag} />;
}
// add clicked name ID to the favourites array
const addFavourite = (id) => {
const newSet = favourites.concat([id]);
setFavourites(newSet);
};
// remove ID from the favourites array
const deleteFavourite = (id) => {
const newList = [...favourites.slice(0, id), ...favourites.slice(id + 1)];
setFavourites(newList);
};
return (
<div>
<main>
<GQLFuncSecond searchLabel={"cloud"} />
<ShortList
data={props.data}
favourites={favourites}
deleteFavourite={deleteFavourite}
/>
<TagsList
data={props.data}
favourites={favourites}
addFavourite={addFavourite}
/>
</main>
</div>
);
}
export default WrappedApp;
组件
import React, { useState } from "react";
import { useQuery } from "@apollo/react-hooks";
import { GQLSIMILARTAGS } from "./graphclient";
/* ##### Single tag ##### */
const Tag = ({ id, info, handleFavourite }) => (
<li className={info.count} onClick={() => handleFavourite(id)}>
{info.label} ({info.tag_related_counts_aggregate.aggregate.count})
</li>
);
const RelatedTagData = ({ data }) => (
<div>
<ul>{data[0].tag_related_counts[0].other_label}</ul>
<ul>{data[0].tag_related_counts[1].other_label}</ul>
<ul>{data[0].tag_related_counts[2].other_label}</ul>
<ul>{data[0].tag_related_counts[3].other_label}</ul>text
</div>
);
/* ##### Shortlist ##### */
const ShortList = ({ favourites, data, deleteFavourite, GQLFuncSecond }) => {
const hasFavourites = favourites.length > 0;
const favList = favourites.map((fav, i) => {
return (
<Tag
id={i}
key={i}
info={data.find((tag) => tag.id === fav)}
handleFavourite={(id) => deleteFavourite(id)}
/>
);
});
return (
<div className="favourites">
<h4>
{hasFavourites
? "Shortlist. Click to remove.."
: "Click on a tag to shortlist it.."}
</h4>
<ul>{favList}</ul>
{hasFavourites && <hr />}
</div>
);
};
/* ##### Tag list ##### */
const TagsList = ({ data, addFavourite }) => {
// Gather list of tags
const tags = data
// filtering out the tags that...
.map((tag, i) => {
return (
<Tag
id={tag.id}
key={i}
info={tag}
handleFavourite={(id) => addFavourite(id)}
/>
);
});
/* ##### the component's output ##### */
return <ul>{tags}</ul>;
};
GraphQL 代码
const client = new ApolloClient({
uri: "https://xxxx.herokuapp.com/v1/graphql",
});
const GQLTAGS = gql`
{
tag(
order_by: { tag_related_counts_aggregate: { count: desc } }
where: { label: { _nin: ["None", "null"] } }
) {
id
label
tag_related_counts_aggregate {
aggregate {
count
}
}
}
}
`;
const GQLSIMILARTAGS = gql`
query($search_label: String!) {
tag(
where: { tag_related_counts: { search_label: { _eq: $search_label } } }
distinct_on: id
) {
label
tag_related_counts(order_by: { count: desc }) {
count
other_label
search_label
}
}
}
`;
function GQLFunc(props) {
const { loading, error, data } = useQuery(GQLTAGS);
if (loading) return <p>Loading...</p>;
if (error) return <p>Error :(</p>;
let CallingApp = props.callingApp;
if (data) return <CallingApp data={data.tag} />;
}
export { client, GQLTAGS, GQLFunc, GQLFuncSecond, GQLSIMILARTAGS };
【问题讨论】:
-
查询
tag签名? -
tag(where.????.. f.e.来自 graphiql 文档 -
@xadm 不幸的是,我没有你的理解水平,所以不理解这个建议。澄清。我想用从这个标签传递给
ShortList(data.tag.label)的标签替换当前的硬编码字符串(在这个例子中是“云”)searchLabel={"cloud"}。因此,如果我单击“安全”标签,它当前会传递给ShortList,但我还想通过GQLFuncSecond函数对这个字符串运行GQLSIMILARTAGS查询。
标签: reactjs graphql react-apollo apollo-client