架构:
type Link {
items: [itemapi]
}
type itemapi {
id: ID!
name: String!
custom_attributes: [CUSTOM_ATTRIBUTES]
}
union CUSTOM_ATTRIBUTES = CustomString | CustomArray
type CustomString {
attribute_code: String
value: String
}
type CustomArray {
attribute_code: String
value: [String]
}
type Query {
allLinks: Link!
}
`;
功能:
const itemList= ({ data: {loading, error, allLinks }}) => {
if (loading) {
return <p>Loading ...</p>;
}
if (error) {
return <p>{error.message}</p>;
}
return (
<div className="itemList">
{ allLinks.items.map( aL => <div key={aL.id} className="items">{aL.???}</div> ) }
</div>
);
};
const channelsListQuery = gql`
query ChannelsListQuery {
allLinks {
items {
id
custom_attributes {
... on CustomString{
value
}
... on CustomArray{
aliasVar: value
}
}
}
}
}
`;
问题是,我不知道如何正确映射这个。我在模式中使用union和查询中的片段来获取我需要的内容然后在页面上呈现它。根据我现在所知,查询数据是通过道具传递的,那么如何才能访问union / fragments中的props,如果可能的话,查询中的道具在value
中有CustomArray
这样的别名?因为value
中的一个是String,第二个是字符串数组,但是具有相同的名称(value
),我需要映射它并将其中一个别名用于从api获取数据。
我需要在地图功能中使用地图吗?
{"items":[{"id":"1","custom_attributes":[{"__typename":"CustomString","value":"<p>The sporty Joust Duffle Bag can't be beat - not in the gym, not on the luggage carousel, not anywhere. Big enough to haul a basketball or soccer ball and some sneakers with plenty of room to spare, it's ideal for athletes with places to go.<p>\r\n<ul>\r\n<li>Dual top handles.</li>\r\n<li>Adjustable shoulder strap.</li>\r\n<li>Full-length zipper.</li>\r\n<li>L 29\" x W 13\" x H 11\".</li>\r\n</ul>"},{"__typename":"CustomString","value":"/m/b/mb01-blue-0.jpg"},{"__typename":"CustomString","value":"/m/b/mb01-blue-0.jpg"},{"__typename":"CustomString","value":"/m/b/mb01-blue-0.jpg"},{"__typename":"CustomString","value":"49"},{"__typename":"CustomArray"},{"__typename":"CustomString","value":"container1"},{"__typename":"CustomString","value":"0"},{"__typename":"CustomString","value":"0"},{"__typename":"CustomString","value":"0"},{"__typename":"CustomString","value":"joust-duffle-bag"},{"__typename":"CustomString","value":"0"},{"__typename":"CustomString","value":"0"},{"__typename":"CustomString","value":"0"},{"__typename":"CustomString","value":"0"},{"__typename":"CustomString","value":"0"},{"__typename":"CustomString","value":"0"},{"__typename":"CustomString","value":"0"},{"__typename":"CustomString","value":"no_selection"}],"__typename":"itemapi"},{"id":"2","custom_attributes":[{"__typename":"CustomString","value":"<p>The sporty Joust blablablablablablablablablbalbalblalbalbla not on the luggage carousel, not anywhere. Big enough to haul a basketball or soccer ball and some sneakers with plenty of room to spare, it's ideal for athletes with places to go.<p>\r\n<ul>\r\n<li>Dual top handles.</li>\r\n<li>Adjustable shoulder strap.</li>\r\n<li>Full-length zipper.</li>\r\n<li>L 29\" x W 13\" x H 11\".</li>\r\n</ul>"},{"__typename":"CustomString","value":"/m/b/mb01-blue-0.jpg"},{"__typename":"CustomString","value":"/m/b/mb01-blue-0.jpg"},{"__typename":"CustomString","value":"/m/b/mb01-blue-0.jpg"},{"__typename":"CustomString","value":"49"},{"__typename":"CustomArray"},{"__typename":"CustomString","value":"container1"},{"__typename":"CustomString","value":"0"},{"__typename":"CustomString","value":"0"},{"__typename":"CustomString","value":"0"},{"__typename":"CustomString","value":"joust-duffle-bag"},{"__typename":"CustomString","value":"0"},{"__typename":"CustomString","value":"0"},{"__typename":"CustomString","value":"0"},{"__typename":"CustomString","value":"0"},{"__typename":"CustomString","value":"0"},{"__typename":"CustomString","value":"0"},{"__typename":"CustomString","value":"0"},{"__typename":"CustomString","value":"no_selection"}],"__typename":"itemapi"}],"__typename":"Link"}
答案 0 :(得分:1)
您可以修改查询以包含__typename
。假设custom_attributes
是一个对象:
query ChannelsListQuery {
allLinks {
items {
id
custom_attributes {
__typename
... on CustomString{
value
}
... on CustomArray{
aliasVar: value
}
}
}
}
}
Then inside your render function you can do something like this:
return (
<div className="itemList">
{
allLinks.items.map(item =>
<div key={item.id} className="items">
{
item.__typename === 'CustomString'
? <div>{item.custom_attributes.value}</div>
: item.custom_attributes.aliasVar.map(v => <div key={v}>{v}</div>)
}
</div>
)
}
</div>
);
或者,您可以只检查是否存在__typename
属性并相应地进行渲染,而不是aliasVar
,但是类型名称是了解您正在使用的数据形状的可预测方式