-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[v4] set GraphQL-Entity props required #11815
Comments
@derrickmehaffy Thank you, I should not have formulated it as a question. |
You are missing the system block of the template, if you could please add that and respond back and I can reopen. |
@derrickmehaffy Here we go |
This issue makes for a very bad experience when building a TS client application. Types generated from Strapi's GraphQL schema often contains null and undefined that requires mapping on the client side. eg. Dynamic Zone type Page {
title: String!
body: [PageBodyDynamicZone]!
} maps to export type Page = {
__typename: 'Page',
title: string,
body: Array<PageBodyDynamicZone | null | undefined> // ?
} eg. Query query Pages {
pages {
data {
id
attributes {
...PageAttributes
}
}
}
} maps to export type PagesQuery = {
__typename: 'Query',
pages?: null | undefined | { // ?
__typename: 'PageEntityResponseCollection',
data: Array<{
__typename: 'PageEntity',
id?: null | undefined | string, // ?
attributes?: null | undefined | PageAttributes // ?
}>
}
}; Related issue: 9411 These types require copious amounts of useless null/undefined checks on the client side, not to mention mapping returned arrays with: export function mapDefiniteArray<T>(source: Array<T | undefined | null>): Array<T> {
return source as Array<T>;
} Why are these GQL type definitions not required? |
Yes still definitely isn't right, the ID shouldn't ever be null or undefined. |
Hello, I just experienced the same problem, type generated for Typescript contains null even though the ID for example should never be nullable. Here is an example of a Collection type using the graphql api extension for autogenerated endpoints:
I would love to create an MR for this, is it something you would consider? Kind regards |
Hi, I'm experiencing the same. I really enjoy strapi, but these properties being marked as nullable makes my typescript experience quite messy. Would love to see a fix on this. |
Anyone have a workaround for this? Working with generated types from the graphql is such a mess. Have you made workarounds in your scripts (to not follow the schema...) or something else? |
It seems to be a headache working with TS client like this. Any updates or workarounds? |
How are you guys handing this in your applications? Huge headache for me |
Lots and lots of of |
I had to abandon Strapi because of this behavior 🥹 |
We use mapping functions to get rid of these crappy types. Here is a few examples: export function mapEntity<A>(data: { id?: string | null; attributes?: A | null }): {
id: string;
attributes: A;
} {
return data as { id: string; attributes: A };
}
export function mapDefiniteArray<T>(source: Array<T | undefined | null>): Array<T> {
return source as Array<T>;
} 😒 |
We are patching the documentation plugin to make ID and attributes required Patch file location (mind the lib version): diff --git a/node_modules/@strapi/plugin-documentation/server/services/helpers/build-component-schema.js b/node_modules/@strapi/plugin-documentation/server/services/helpers/build-component-schema.js
index 95fb824..54486c1 100644
--- a/node_modules/@strapi/plugin-documentation/server/services/helpers/build-component-schema.js
+++ b/node_modules/@strapi/plugin-documentation/server/services/helpers/build-component-schema.js
@@ -222,6 +222,7 @@ const getAllSchemasForContentType = ({ routeInfo, attributes, uniqueName }) => {
}),
},
[`${pascalCase(uniqueName)}ResponseDataObject`]: {
+ required: ['id', 'attributes'],
type: 'object',
properties: {
id: { type: 'number' }, and run (setup patch-package first) npx patch-package @strapi/plugin-documentation this will then generate those fields as required (tested for OpenAPI spec). Not a bulletproof solution so be cautious |
I'm amazed that the ticket hasn't budged in 2 years. Working with Strapi/GraphQL in TypeScript projects feels almost impossible. 🤔 |
I am also struggling with this. Any feedback from the Strapi developers? |
This is an immensely annoying bug. It's weird that it has been assigned a severity of 'low'. Lots of people use typescript these days and this bug makes it next to impossible to use typescript with the generated graphql types unless one litters the codebase with useless type assertions which are only there to make up for a bug that should've been addressed by the strapi team 2 years ago when the issue was first reported. Also none of the workarounds for graphql work anymore as the code has been refactored and shifted around beyond recognition since they were first described. |
Any chance one of the Strapi maintainers (@derrickmehaffy maybe) may look at the PR put forward by @msadegh76 and approve it or give suggestions on how to improve it? The diff looks simple enough that a review could probably be done without too much effort. |
Here's a patch pulled from the PR:
(btw it's crazy that this is somehow still open) |
Bug report
In your GraphQL-Example is this example:
ID
andattributes
shouldn't be optional in documentEntity, if there is a valid DocumentEntitydata
.Background: I create TypeScript-Classes for the Frontend based on the schema. In v3 I could just check if the data has arrived and then work with the data, in v4 I would need to check the data and then if id and attributes exists, that does not seem to be necessary.
Expected behavior
System
Solution
The text was updated successfully, but these errors were encountered: