Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

gatsby - GraphQL defining fragments for components

I've created a base of components that are reused across a few different page templates. So let's say I have component CTA and a few page templates that have cta element with text and title. I would want to find a way to define a query next to CTA component that can be reused for each template - in case if I would change something in the component then I have to remember to go and update all template's queries. I tried defining objects, but graphql doesn't support string interpolation and I tried defining fragments, but then I would have to define a fragment for each template, which misses the point... I tried to have fragment data on cta but then it says there's no type cta. My cta is an object in the schema.

SanityFirstTemplate {
  cta {
    title
    text
  }
}
SanitySecondTemplate {
  cta {
    title
    text
  }
}

Is there a way of defining "cta" and it's contents so I could use it like that or in any similar way?

SanityFirstTemplate {
  ...cta
}
SanitySecondTemplate {
  ...cta
}

?

Thanks!

question from:https://stackoverflow.com/questions/65829910/graphql-defining-fragments-for-components

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...