datocms / structured-text

MIT License
22 stars 7 forks source link

Newer records are not assignable to dato-cms-structured-text-utils Records #31

Closed ptbrowne closed 11 months ago

ptbrowne commented 11 months ago

It seems like newly created Records are not assignable to the Record type in dato-cms-structured-text-utils, presumably because the id system has changed.

I am using generated types from the Graphql endpoint and generated types for newer records do not have an id.

It leads to this type of error since I am using the StructuredTextGraphqlResponse type:

        Type '{ __typename: "MyCustomRecord"; }' is not assignable to type 'Record'.
          Property 'id' is missing in type '{ __typename: "MyCustomRecord"; }' but required in type '{ __typename: string; id: string; }'.

It seems like the type here need to be updated https://github.com/datocms/structured-text/blob/3307cbdf9011af162b88ea8ff6cd9ad111b09270/packages/utils/src/types.ts#L454 to mark the id field as optional.

Am I correct ?

stefanoverna commented 11 months ago

I don't think that's the case. IDs have always been required, and they've always been strings. Take a look at https://www.datocms.com/docs/next-js/rendering-structured-text-fields#rendering-special-nodes

Are you writing your GraphQL queries like that (notice the id inside ImageBlockRecord and BlogPostRecord)?

const HOMEPAGE_QUERY = `query HomePage($limit: IntType) {
  allBlogPosts(first: $limit) {
    id
    title
    content {
      value
      blocks {
        __typename
        ... on ImageBlockRecord {
          id
          image { url alt }
        }
      }
      links {
        __typename
        ... on BlogPostRecord {
          id
          slug
          title
        }
      }
    }
  }
}`;
ptbrowne commented 11 months ago

You're right 😅 It seems like I had missed to add an "id" inside my query for a newer model. Thanks a lot 🙇