-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
feat(gatsby-source-contentful): provide gql type definitions using content model introspection and schema-customization #12816
Conversation
…ntent model introspection and schema-customization
|
Hi @pieh,
I think we can live with the Union type for now as it's the easiest approach for now. I don't think having the validation for single ref fields will happen anytime soon. For the complex example I can definetly help with testing on complex examples |
Hi @pieh
Own Contentful's GraphQL API exposes Other than that, I don't see reasons for keeping this field. |
Oh, interesting - I might revisit this to be more inline with how Contentful GraphqQL have RichText fields set up (once we deal with changed that we need to make in |
Hey @pieh Anything we can help with the unblock this? |
This is partly blocked by #13028 - after that we will be able to get rid of parts where I was importing gatsby internals (which is not safe):
I will also need to dial back unneeded back-references changes and address rich text types changes. Was there any update on validation field for single references in Contentful API (didn't check in quite a bit)? |
I don't think it will happen anytime soon so we can live with a union to all content types |
…tion for backreferences, use dontInfer to lock types
While we are kind of blocked here with merging this one, maybe you could try checking if creating temporarily placeholder context to create schema snapshot with https://www.gatsbyjs.org/packages/gatsby-plugin-schema-snapshot/, and then removing placeholders and relying on generated snapshot would work? |
I can confirm this works. I created a Contentful environment in which I ensured to have at least one entry will all fields for each content type as well as a reference to all possible types of components in every multi relationship field. Generated the schema using the https://www.gatsbyjs.org/packages/gatsby-plugin-schema-snapshot/ plugin, then unpublished all entries for some content types. Build passed ignoring the content types with no entries as they were still generated in the GraphQL schema (they simply return no results). Null checks are still necessary but this at least allows me to have queries in |
@pieh what about adding plugin option - |
Question: will this do anything to allow making Required-> Non_Null? I'm using graphql simply for a static build and would like to verify my Required fields are present. I know there's issue of Preview environments.. I'm fine one-off handling these on the frontend. I'm building for production and would like to be able to require things with typescript generated from graphql. |
Current implementation does make Contentful required field as NonNull Graphql fields and I do want to keep it that way for production builds / when using "production" data from Contentful. I'm not sure how exactly to make it to continue work with Contentful's Preview API yet (as queries would just fail if any of required field is not there). Maybe this is fine for them to not work/update results until all required fields are filled, but the DX when leaving it like that will be awful. One way we could make it work is if we use mock field values for required fields when using preview API (but not in production mode). So empty text field would be something like I guess question is - do we want types to change (probably not) or are we fine with mocked data for required fields to workaround the issue. |
@pieh What if it was configurable on a per-environment basis what Required meant? So for the server using preview environment, you could build with those fields being nullable based on a config param? Would totally be fine with using default fallbacks for preview environment. |
const digest = str => | ||
crypto | ||
.createHash(`md5`) | ||
.update(str) | ||
.digest(`hex`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
new code style suggestion from:
#8805 Issue: feat: update gatsby plugins to use new createContentDigest helper
const digest = str => | |
crypto | |
.createHash(`md5`) | |
.update(str) | |
.digest(`hex`) | |
const digest = str => createContentDigest(str) |
and add to imports:
import { createContentDigest } from "gatsby-core-utils"
or better replace all usages of digest
with createContentDigest
(I've converted this PR into a Draft PR. Although it's been a year since the start, is any progress expected at all?) |
it is expected 🙂 |
I want to follow up on this. The Here are my type overrides, which are a chore to maintain and somewhat eliminate the benefits of typescript https://gist.github.com/bsgreenb/05202d74c7ef079970b536fad0fc5c88 @pieh was concerned about preview environments, but why dont you just disallow previewing content that doesn't have required fields? The only fields I require on a post are a title/body/slug. I want the build to break if it doesn't have those, and an editor can easily enter them. This issue has been around over a year, and the entire time that I've been working on my project. I'm sure there's a workaround that let's us actually sync up TypeScript and Contentful! |
@axe312ger @pieh Just to make sure: Is this change also needed with the new source plugin for Contentful and/or will this be part of other changes already? |
@LekoArts still needed and one of the highest ranking issues for us after getting rich text run with complex content models! |
Rather than using a union, could we use the base type We used a similar approach for the Kentico Kontent plugin by using the This approach also works when patching the schema locally: function customiseHomeModel() {
const typeDefs = `
type ContentfulHome implements Node {
metadata: ContentfulReference @link(by: "id", from: "metadata___NODE")
sections: [ContentfulReference] @link(by: "id", from: "sections___NODE")
}
`;
return typeDefs;
} In the GraphQL query you can then use an inline fragment to pull any data you need: query MyQuery {
allContentfulHome {
nodes {
sections {
contentful_id
id
... on ContentfulArticle {
id
date
slug
title
}
}
}
}
}
|
Closing this PR in favour of #30855. Feel free to comment and test the upcoming major version. |
This is work in progress. It works, but there are few things that need to be cleaned up before merging this.
This works currently (at least with
using-contentful
example) - you can swap currently used Contentful space configuration inusing-contentful/gatsby-config.js
to one that is currently commented out. This space doesn't have any content but all queries still work and you can build a site (even if it does not have any content apart from things that are hardcoded in react components).Potential blockers (discussion points)
// Long string (markdown)
)using-contentful
example)I might have forgotten some things - I tried to add pretty detailed comments beside the code.
Breaking changes:
This also introduce number of breaking changes:
GraphQLJSON
type:json
field inside richText field is preserved (to maintain somewhat compatibility) but all other fields are gone (one that was already deprecated) and others that were inferred (they were pretty useless anyway, as it's really hard to query all the fields from structured content like that)TO-DO:
/cc @Khaledgarbaya @sbezludny @freiksenet @stefanprobst