Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

headless cms - Gatsby/graphql: How to reuse fragments on many different models

Using Gatsby & DatoCMS.

I have many (20) models that all contain links to other models.

For example, on the home page "hero", cms users may point to different models/pages:

fragment homeHeroFragment on DatoCmsCompanyHome {
   ctaLinkLabel
   ctaLinkObject {
        -- How to make this part reusable everywhere I have a "validated" Link: 
        ... on DatoCmsCaseOverview {
            type: __typename
            id
            locale
            company {
                slug
            }
        }
        ... on DatoCmsThemeOverview {
            type: __typename
            id
            locale
            company {
                slug
            }
        }
        ... -- x roughly 20
    }
}

Currently I have to explicitly reference each model that is allowed, as well as repeat this everywhere I have this kind of link which is ~ 20 places.

Does anyone know how to reuse this linkObject on multiple models so I have to write it only once? I can't simply interpolate, I can't write it as a reusable fragment as it needs to be mounted on many different files.

I'm considering writing a script for automatically generating these files. But I'm sure there's a better way.

Thanks!


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

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...