我在Gatsby.js中遇到GraphQL片段的问题。如果我将GraphQL查询的一部分逐字复制到新片段中,则返回的数据将不完整。
第一个查询:
export const PageQuery = graphql`
query PageQuery($slug: [String]) {
craftcms {
entries(type: "pages", slug: $slug) {
title
slug
id
... on CraftCMS_pages_pages_Entry {
id
sections {
... on CraftCMS_sections_fullWidthHero_BlockType {
cta
ctaTheme {
... on CraftCMS_buttonStyles_Category {
slug
}
}
heading
id
image {
width
url
height
mimeType
}
text
typeHandle
}
}
}
}
}
}
`;
第一个查询结果:
{
"__typename": "CraftCMS_sections_fullWidthHero_BlockType",
"cta": "https://app.helloalpha.com/membership-signup",
"ctaTheme": [
{
"__typename": "CraftCMS_buttonStyles_Category",
"slug": "sunset"
}
],
"heading": "Healthcare on your terms.",
"id": "5139",
"image": [
{
"__typename": "CraftCMS_siteContent_Asset",
"width": 1500,
"url": "https://assets-membership.helloalpha.com/content/RX-2.jpg?mtime=20190907143548",
"height": 1000,
"mimeType": "image/jpeg"
}
],
"text": "<p>Skip the waiting room - access a doctor online from the comfort of your own home, anytime.</p>",
"typeHandle": "fullWidthHero"
}
第二个查询:
export const fullWidthHero = graphql`
fragment fullWidthHero on CraftCMS_sections_fullWidthHero_BlockType {
cta
ctaTheme {
... on CraftCMS_buttonStyles_Category {
slug
}
}
heading
id
image {
width
url
height
mimeType
}
text
typeHandle
}
`;
export const PageQuery = graphql`
query PageQuery($slug: [String]) {
craftcms {
entries(type: "pages", slug: $slug) {
title
slug
id
... on CraftCMS_pages_pages_Entry {
id
sections {
...fullWidthHero
}
}
}
}
}
`;
第二个查询结果:
{
"__typename": "CraftCMS_sections_fullWidthHero_BlockType",
"cta": "https://app.helloalpha.com/membership-signup",
"ctaTheme": [],
"heading": "Healthcare on your terms.",
"id": "5139",
"image": [],
"text": "<p>Skip the waiting room - access a doctor online from the comfort of your own home, anytime.</p>",
"typeHandle": "fullWidthHero"
}
根据Using fragments docs,这两个查询在功能上应等效,但结果并不相同。有什么想法吗?