prismicio / prismic-types

Type definitions for Prismic content, models, and APIs
https://prismic.io/docs/technologies/javascript
Apache License 2.0
11 stars 4 forks source link

Include `data` field in RelationField #1

Closed angeloashmore closed 3 years ago

angeloashmore commented 3 years ago

Is your feature request related to a problem? Please describe.

The data field does not exist on RelationField. Using the REST API, one can query for a document using fetchLinks or graphQuery. This populates the data field on the document.

Currently, we can only create a field type like the following:

type MyField = RelationField<"page", "en-us">;
type MyFieldData = MyField["data"];

data on MyField does not exist, causing the line with MyFieldData to throw a type error.

Describe the solution you'd like

The ability to provide the data field's type via a third generic could solve this.

type MyField = RelationField<
  "page",
  "en-us",
  {
    requestedDataField: KeyTextField,
  }
>;
type MyFieldData = MyField["data"];
lihbr commented 3 years ago

Great call! I'm currently investigating rich text output as current definition proved weak when working on helpers (https://github.com/prismicio/prismic-types/blob/master/src/fields.ts#L7), I also have integration fields to check on my plate ^^'

angeloashmore commented 3 years ago

I added this since I needed it in gatsby-source-prismic but the implementation isn't ideal.

https://github.com/prismicio/prismic-types/blob/cc62bf0dfe3f086668b2d42c3fc72642d0e9e168/src/fields.ts#L218-L233

DataInterface defaults to never rather than something like PrismicDocument['data'] since it would result in a type circular reference (links circularly referencing other links). TypeScript would not compile.

Basically, the current implementation requires a user to define the type of data if it is to be used. I think this is reasonable since users who use fetchLinks or graphQuery know exactly what kind of shape they requested. Typing it to be general by default (i.e. a record of any field type) may not be helpful.

This is totally open to being changed if there's a better way of handling it. 🙂