我是新手graphql-request
,想尝试使用片段,但注意到一些奇怪的行为。我不确定行为是来自graphql-request
还是 Dgraph在此处输入链接描述Cloud。
我的第一个问题是查询中数据的顺序真的很重要。
//query
const query = graphql`
query EntryQuery($id: Float) {
queryEntry(filter: { id: { eq: $id } }) {
...EntryBanner_entry
images {
source
}
}
}
${EntryBannerFragment}
`;
const fragment = graphql`
fragment EntryBanner_entry on Entry {
title
images {
source
type
}
}
`;
这将输出正确的对象:
{
title: string
images: { source: string, type: string }[]
}
但是,如果我更改数据和片段的顺序:
const query = graphql`
query EntryQuery($id: Float) {
queryEntry(filter: { id: { eq: $id } }) {
images {
source
}
...EntryBanner_entry
}
}
${EntryBannerFragment}
`;
那么对象是:
{
title: string
images: { source: string }[]
}
为什么会这样?我过去使用过 Relay,数据和片段的顺序无关紧要。
我遇到的另一个问题是当我有一个片段碰巧在组件树的某个地方的另一个片段中复制时。
例如,我有三种不同类型的横幅,每一种都使用描述。所以我将三种类型的横幅作为片段,每一种都包含一个描述片段。
const fragment = graphql`
fragment EntryBanner_entry on Entry {
...EntryBannerWithPoster_entry
...EntryBannerWithBanner_entry
...EntryBannerWithText_entry
}
${EntryBannerWithBannerFragment}
${EntryBannerWithPosterFragment}
${EntryBannerWithTextFragment}
`;
//here is an example of EntryBannerWithTextFragment (note each banner fragment has the description fragment
const fragment = graphql`
fragment EntryBannerWithText_entry on Entry {
title
originalTitle
releaseYear
movieLength
genres
...EntryDescription_entry
}
${EntryDescriptionFragment}
`;
但是,由于正在加入多个片段EntryBanner_entry
,因此会导致此错误:
There can be only one fragment named "EntryDescription_entry".: {"response":{"errors":[{"message":"There can be only one fragment named \"EntryDescription_entry\".",...}
为什么会这样?它似乎对组件不太友好。