datocms / gatsby-source-datocms

Official GatsbyJS source plugin to pull content from DatoCMS
MIT License
140 stars 50 forks source link

Schema says that everything is a Maybe<T> #195

Closed byronwilliams closed 1 year ago

byronwilliams commented 2 years ago

Both of the graphql + typescript plugins for Gatsby gatsby-plugin-graphql-config and gatsby-plugin-graphql-codegen declare that Required fields in DatoCMS are typed as Maybe or null | undefined.

This means that typescript files have to be peppered with ternary statements for "correctness". Using the DatoCMS explorer I can see that everything is typed correctly.

Can this plugin be updated so that it generates a schema inside Gatsby which marks the fields correct as required or optional?


image

type DatoCmsGuarantee = Node & {
  readonly _allFooterTextLocales: Maybe<ReadonlyArray<Maybe<DatoCmsAllLocalesForDatoCmsGuaranteeFooterText>>>;
  readonly _allImageLocales: Maybe<ReadonlyArray<Maybe<DatoCmsAllLocalesForDatoCmsGuaranteeImage>>>;
  readonly _allTextLocales: Maybe<ReadonlyArray<Maybe<DatoCmsAllLocalesForDatoCmsGuaranteeText>>>;
  readonly _allTitleLocales: Maybe<ReadonlyArray<Maybe<DatoCmsAllLocalesForDatoCmsGuaranteeTitle>>>;
  readonly children: ReadonlyArray<Node>;
  readonly footerText: Maybe<Scalars['String']>;
  readonly gatsbypreview: Maybe<Scalars['JSON']>;
  readonly id: Scalars['ID'];
  readonly image: Maybe<DatoCmsFileField>;
  readonly internal: Internal;
  readonly locale: Maybe<Scalars['String']>;
  readonly meta: Maybe<DatoCmsMetaField>;
  readonly model: Maybe<DatoCmsModel>;
  readonly originalId: Maybe<Scalars['String']>;
  readonly parent: Maybe<Node>;
  readonly seoMetaTags: Maybe<DatoCmsSeoMetaTags>;
  readonly text: Maybe<DatoCmsDatoCmsGuaranteeTextStructuredText>;
  readonly title: Maybe<Scalars['String']>;
};
stefanoverna commented 2 years ago

See this: https://community.datocms.com/t/make-required-fields-non-nullable-in-graphql-schema/1873/3?u=s.verna

(btw, the same situation is present on the CMS explorer as well)

In Gatsby-land, this sentence:

The only way to provide non-null GraphQL types would be to have different GraphQL queries that only return valid records, while the current allArticles queries return both valid and invalid records.

Would become:

The only way to provide non-null GraphQL types would be to have a global configuration option that only returns valid records, while the current allDatoCmsArticles queries return both valid and invalid records.