prismicio / prismic-helpers

Set of helpers to manage Prismic data
https://prismic.io/docs/technical-reference/prismicio-helpers
Apache License 2.0
15 stars 9 forks source link

Include `data` field in `documentToLinkField` #19

Closed angeloashmore closed 3 years ago

angeloashmore commented 3 years ago

If a document is provided to documentToLinkField for use with asLink, the document's data field should be retained.

Related issue: https://github.com/prismicio/prismic-types/issues/1

documentToLinkField currently copies all standard Link fields except data, which may be populated in normal Fields using fetchLinks or GraphQuery.

https://github.com/prismicio/prismic-helpers/blob/6dec3ab22bb5f29b26846aa52b9e6ce0a40034f5/src/documentToLinkField.ts#L18-L27

lihbr commented 3 years ago

Can you give more context over why this change? Looks like to me this create inconsistencies regarding what document object the link resolver will receive, maybe I missed something.

lihbr commented 3 years ago

Okay, after some thoughts I got it, that's quite farfetched as assuming your link resolver might receive links with a data object can be dangerous but I'm fine with it ☺️

angeloashmore commented 3 years ago

Ah sorry, forgot to come back to your question. Yes, this is to cover the case where your Link Resolver expects data to be populated with content using fetchLinks or graphQuery. For example, if you query for a Page document with a parent field, you may have your client setup to fetch data for the parent document.

The change described above allows documentToLinkField to be inline with that functionality.

angeloashmore commented 3 years ago

If a use case is needed for reference, documentToLinkField is used in gatsby-source-prismic where the Link Resolver must act exactly like it does elsewhere, such as in Rich Text fields.

https://github.com/angeloashmore/gatsby-source-prismic/blob/8b90c862b104136f8e761801ab192ecc06632638/packages/gatsby-source-prismic/src/lib/createCustomType.ts#L166-L170