Skip to content
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

fix: use content type id by default to create reference gql types #26102

Conversation

axe312ger
Copy link
Collaborator

and add support for the useNameForId option

@axe312ger axe312ger added the status: needs core review Currently awaiting review from Core team member label Jul 29, 2020
@axe312ger axe312ger requested a review from a team as a code owner July 29, 2020 14:18
@gatsbot gatsbot bot added the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Jul 29, 2020
@ascorbic ascorbic added topic: source-contentful Related to Gatsby's integration with Contentful and removed status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer labels Jul 30, 2020
Copy link
Contributor

@pvdz pvdz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was intense to review. So many loops.

Generally speaking, I'm very concerned about the runtime performance implication of this PR. Please make sure to check this with some scaling builds. 15k, up to 100k if you can. The code loops over so many pieces and I can't tell how many each piece is expected to contain on average.

Additionally please check with a large site with many locales (10+?).

Additionally, I'm concerned about models that have circular references (Contentful can produce those) crashing the plugin. Please make sure to test with a model that somehow generates those circular references. Better yet, add a test for it. In particular, all the cases that exhaustively traverse or JSON.parse an object.

I've left many comments but ultimately you are the code owner. So I'm going to accept it and leave it up to you whether you want to address the comments or not. I hope you'll address at least some concerns.

@@ -7,16 +7,26 @@ jest.mock(`gatsby-core-utils`, () => {
}
})

const _ = require(`lodash`)
const gatsbyNode = require(`../gatsby-node`)
const fetch = require(`../fetch`)
const normalize = require(`../normalize`)

const startersBlogFixture = require(`../__fixtures__/starter-blog-data`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: if this file returned a function that returned the data structure, then you wouldn't need to bother to _.deepClone the structure every time. In fact, you wouldn't need to worry about cross test pollution at all.

Comment on lines 317 to 321
.mockReturnValueOnce(_.cloneDeep(startersBlogFixture.initialSync))
.mockReturnValueOnce(_.cloneDeep(startersBlogFixture.createBlogPost))

const createdBlogEntry =
startersBlogFixture.createBlogPost.currentSyncData.entries[0]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you deep clone, the startersBlogFixture reference may hold different values than what was passed on to fetch above. Not sure if that matters here.

@@ -134,6 +138,8 @@ ${formatPluginOptionsForCLI(pluginConfig.getOriginalPluginOptions(), errors)}`)
space,
}

console.timeEnd(`Fetch Contentful data`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if there isn't an easier way to group the start/stops of these. 🤷 But please don't remove them because I love seeing this.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same, I replaced all consone.timeEnd with GatsbyReporter.

@@ -39,7 +40,8 @@
],
"license": "MIT",
"peerDependencies": {
"gatsby": "^2.12.1"
"gatsby": "^2.12.1",
"gatsby-plugin-sharp": "^2.6.14"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious: what's mandating this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You only need it when using traced SVGs

if (type.name.match(/contentful.*RichTextNode/)) {
return {
nodeType: {
type: GraphQLString,
deprecationReason: `This field is deprecated, please use 'json' instead.`,
deprecationReason: `This field is deprecated, please use 'raw' instead. @todo add link to migration steps.`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the todo supposed to be part of the string? (same below)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I gonna replace this anyways pretty soon when updating the documentation. Thats when I was planning to update this @todo

Comment on lines 11 to 14
value &&
value.sys &&
value.sys.type === `Link` &&
value.sys.contentful_id
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
value &&
value.sys &&
value.sys.type === `Link` &&
value.sys.contentful_id
value?.sys?.type === `Link` &&
value.sys.contentful_id

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

value.sys.contentful_id
) {
value.sys.id = value.sys.contentful_id
delete value.sys.contentful_id
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete could have an adverse performance implication, especially at scale. Consider setting it to undefined or to use a different data structure if possible (like Map).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The delete is not needed at all, plus this part might be removed then the fixId's PR is merged.

const prepareIdsForResolving = obj => {
for (let k in obj) {
const value = obj[k]
if (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Similar as commented earlier, this if could be inside the typeof check of the else branch.

? getField(field)
: field[defaultLocale]
function renderRichText({ raw, references }, options = {}) {
const richText = JSON.parse(raw)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible for this raw data model to have circular references? If so, this call will throw an error. Please make sure to check for this.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope it can not by definition of a Contentful API response :)

asset: node.data.target,
getField,
}),
// Create dummy response so we can use official libraries for resolving the entries
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks odd to me 🤷

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added a better comment

@axe312ger axe312ger closed this Aug 5, 2020
@axe312ger axe312ger force-pushed the contentful-next-use-id-by-default branch from c29cb4a to e5487e1 Compare August 5, 2020 09:08
@axe312ger axe312ger reopened this Aug 5, 2020
@axe312ger axe312ger force-pushed the contentful-next-use-id-by-default branch from e5487e1 to bb4359a Compare August 5, 2020 12:05
@axe312ger axe312ger added bot: merge on green Gatsbot will merge these PRs automatically when all tests passes and removed status: needs core review Currently awaiting review from Core team member labels Aug 5, 2020
@gatsbybot gatsbybot merged commit cfd6dc2 into gatsbyjs:contentful-next Aug 5, 2020
@axe312ger axe312ger deleted the contentful-next-use-id-by-default branch August 5, 2020 13:37
pvdz pushed a commit that referenced this pull request Oct 5, 2020
…e performance (#27244)

* fix(gatsby-source-contentful): fixed id collision in contentful entries (#23514)

* fix(gatsby-source-contentful): fixed id collision in contentful entries

* fix(gatsby-source-contentful): properly resolve subsequent sync calls   (#15084)

* fix(gatsby-source-contentful): fixed id collision in contentful entries (#23514)

* fix(gatsby-source-contentful): fixed id collision in contentful entries

* improve logging and clean up code (#26238)

* improve comment about contentful-resolve-response usage

* test(unit): remove cross test data pollution

* further improve loggin by replacing console.time with GatsbyReporter

* improve code style

* missing file

* remove logging verbosity

* adjust mocked reporter for tests to pass

* remove unused deep-map dependency

* fix: use content type id by default to create reference gql types (#26102)

* temporary console.time calls for performance measurements

* perf(gatsby-source-contentful): use a map and less loops to merge old and latest sync API response

* log time Contentful API processing  and GraphQL node creation

* upgrade dependencies and use faster node 10 versions

* remove duplicate logging

* allow multiple Contentful sources

fixes #26875

* refactor(imports): fix dependency structure and make sharp optional

fixes #23904

* align tests to match new multi source cache pattern

Co-authored-by: Matthew Miller <mnmiller1@me.com>
Co-authored-by: Khaled Garbaya <khaled@contentful.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bot: merge on green Gatsbot will merge these PRs automatically when all tests passes topic: source-contentful Related to Gatsby's integration with Contentful
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants