Hello everyone! Completely new to DatoCMS after working with Strapi for a while, and Iām having major issues getting the typescript codegen things to work.
Iāve followed this guide but Iām not having any success.
For one, the graphql.config.yml file has a spelling error in the custom scalars:
āJsonField: unkownā should be unknown, right?
But when I fix that I get a type error on the StructuredText component:
"Type '{ __typename?: "StartpageModelContentField" | undefined; value: unknown; } | null | undefined' is not assignable to type 'Document | Node | StructuredText<Record, Record> | null | undefined'."
...
"Type 'unknown' is not assignable to type 'Document'"
In other similar issues on here, a suggested solution is to make the JsonField scalar have the type " StructuredText<Record, Record>", but StructuredText doesnāt get imported in to the generated.ts file, and if I import it manually Iām getting the error āGeneric type āRecordā requires 2 type argument(s).ts(2314)ā
Any help would be much appreciated, itās definitely possible Iām doing something else completely wrong.
Thank you for the swift response! I get errors in the generated.ts file itself now, but it did fix the error in the components. What I also had to do was restructure the .yml file to this:
Iāve sort of kinda made it work by changing my .yml file to this:
generates:
src/graphql/generated.ts:
plugins:
- typescript
- add:
content: "import { type Record as StructuredTextGraphQlResponseRecord, type StructuredText as StructuredTextGraphQlResponse } from 'datocms-structured-text-utils';"
- typescript-operations
- typed-document-node
config:
strictScalars: true
scalars:
BooleanType: boolean
CustomData: Record<string, string>
Date: string
DateTime: string
FloatType: number
IntType: number
ItemId: string
JsonField: "StructuredTextGraphQlResponse<StructuredTextGraphQlResponseRecord,StructuredTextGraphQlResponseRecord>"
MetaTagAttributes: Record<string, string>
UploadId: string
namingConvention:
enumValues: "./src/lib/pascalCaseWithUnderscores.js"
and then adding the full āpage.content.valueā prop to the StructuredText data field.
But I imagine that would break cases where you need to use blocks or links instead of the value.
Iām facing the same issue with this one. Any chance of an update with a proper answer for this @m.finamor? @koskarbengtsson said so himself, that using blocks or links would break and I need to use links in my case. Iām getting errors in my generated.ts file: Generic type 'Record' requires 2 type argument(s).
Hi @john.hult, welcome! Could you please provide a bit more detail?
I tried following the guide and using the example repo along with a structured text field with a linked record, and it seems to work without any errors.
I didnāt modify the graphql.config.yml, but I did edit graphql/home.graphql in that example to fetch the structured context field and its links, like:
query Home {
allArticles {
id
title
_createdAt
_publishedAt
image {
...responsiveImage
}
content {
value
links {
id
title
content {
value
}
}
}
}
}
(The content is what I added over the example)
Then I as able to run graphql-codegen without any errors.
What specifically are you trying to do that is failing with an error? Any error messages, screenshots, sample code, etc. would be very helpful
function isStructuredText(
text: unknown
): text is StructuredTextGraphQlResponse {
const t = text as StructuredTextGraphQlResponse;
return (
t &&
typeof t === "object" &&
"value" in t &&
validate(t.value as Document).valid
);
}
/**
* if t contains value and is validated as a Document ()
* @param t your structured text
* @returns Structured Text
*/
export function parseStructuredText(
t: unknown
): StructuredTextGraphQlResponse | undefined {
return isStructuredText(t)
? (t as unknown as StructuredTextGraphQlResponse)
: undefined;
}
which solves it for now. But it just feels like structured text should have a proper type.