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

Properly handle nested resources when rendering the plan #25930

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/gatsby-recipes/src/graphql-server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ require(`dotenv`).config()
const express = require(`express`)
const chokidar = require(`chokidar`)
const graphqlHTTP = require(`express-graphql`)
const { v4: uuidv4 } = require("uuid")
const { v4: uuidv4 } = require(`uuid`)
const {
GraphQLSchema,
GraphQLObjectType,
Expand Down Expand Up @@ -164,7 +164,7 @@ const schema = new GraphQLSchema({
const app = express()
const server = createServer(app)

console.log(`listening on localhost:4000`)
console.log(`listening on localhost:${PORT}`)

app.use(cors())

Expand Down
8 changes: 5 additions & 3 deletions packages/gatsby-recipes/src/renderer/reconciler.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
const ReactReconciler = require(`react-reconciler`)
const flatted = require(`flatted`)

const debug = require(`debug`)(`recipes-reconciler`)
const debugInner = require(`debug`)(`recipes-reconciler`)
const debug = (title, data) => {
debugInner(title, JSON.stringify(data, null, 2))
}

const reconciler = ReactReconciler({
finalizeInitialChildren(element, type, props) {},
Expand Down Expand Up @@ -39,6 +42,7 @@ const reconciler = ReactReconciler({
return { text }
},
commitTextUpdate(textInstance, oldText, newText) {
debug(`updating text instance`, text)
textInstance.text = newText
return textInstance
},
Expand Down Expand Up @@ -103,8 +107,6 @@ const RecipesRenderer = {
let container = reconciler.createContainer(currState, false, false)
reconciler.updateContainer(whatToRender, container, null, null)

//debug(`render result`, require('circular-json').stringify(currState, null, 2))

return currState
},
}
Expand Down
24 changes: 12 additions & 12 deletions packages/gatsby-recipes/src/renderer/render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,50 +32,50 @@ test(`handles nested rendering`, async () => {
"_stepMetadata": Object {},
"currentState": "",
"describe": "Write red.js",
"diff": "- Original - 0
+ Modified + 1
"diff": "- Original - 0
+ Modified + 1

+ red!",
+ red!",
"newState": "red!",
"resourceChildren": Array [
Object {
"_stepMetadata": Object {},
"currentState": "",
"describe": "Write blue.js",
"diff": "- Original - 0
+ Modified + 1
"diff": "- Original - 0
+ Modified + 1

+ blue!",
+ blue!",
"newState": "blue!",
"resourceChildren": Array [
Object {
"_stepMetadata": Object {},
"currentState": "",
"describe": "Write yellow.js",
"diff": "- Original - 0
+ Modified + 1
"diff": "- Original - 0
+ Modified + 1

+ yellow!",
+ yellow!",
"newState": "yellow!",
"resourceDefinitions": Object {
"content": "yellow!",
"path": "yellow.js",
},
"resourceName": undefined,
"resourceName": "File",
},
],
"resourceDefinitions": Object {
"content": "blue!",
"path": "blue.js",
},
"resourceName": undefined,
"resourceName": "File",
},
],
"resourceDefinitions": Object {
"content": "red!",
"path": "red.js",
},
"resourceName": undefined,
"resourceName": "File",
},
]
`)
Expand Down
45 changes: 20 additions & 25 deletions packages/gatsby-recipes/src/renderer/transform-to-plan-structure.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
const providedResources = require(`../resources`)
const flatted = require(`flatted`)

const extractPlan = node => {
const isResource = type => type && (type === `Input` || providedResources[type])

const extractPlan = (node, type) => {
if (!isResource(type)) {
return null
}

let text = {}
if (node.text) {
try {
Expand All @@ -11,19 +17,7 @@ const extractPlan = node => {
}
}

const { _type: type, _props: props, ...plan } = text

if (type === `Input`) {
return {
resourceName: type,
resourceDefinitions: props,
...plan,
}
}

if (!type || !providedResources[type]) {
return null
}
const { _props: props, ...plan } = text

return {
resourceName: type,
Expand All @@ -32,14 +26,14 @@ const extractPlan = node => {
}
}

const transform = (props = {}) => {
const transform = (props = {}, type) => {
if (!props.children) {
const plan = extractPlan(props)
const plan = extractPlan(props, type)
return plan ? [plan] : []
}

const plan = props.children.filter(Boolean).reduce((acc, curr) => {
const childResourcePlans = transform(curr)
const childType = curr.type || type

let currText = {}
if (curr.text) {
Expand All @@ -48,27 +42,29 @@ const transform = (props = {}) => {
} catch {}
}

if (curr._type === `Input`) {
if (childType === `Input`) {
currText.resourceName = `Input`
return [...acc, currText]
}

if (!providedResources[curr.type]) {
return [...acc, ...childResourcePlans]
if (!providedResources[childType]) {
return [...acc, ...transform(curr, childType)]
}

const [rawResource, ...resourceChildren] = curr.children || []
const { _props, _type, ...plan } = JSON.parse(rawResource.text)
const { _props, ...plan } = JSON.parse(rawResource.text)

const resourcePlan = {
// TODO figure out why do we have to use the mdxType for child components?
resourceName: _type || _props?.mdxType,
resourceName: childType,
resourceDefinitions: _props,
...plan,
}

if (resourceChildren.length) {
resourcePlan.resourceChildren = transform({ children: resourceChildren })
resourcePlan.resourceChildren = transform(
{ children: resourceChildren },
childType
)
}

return [...acc, resourcePlan]
Expand All @@ -78,7 +74,6 @@ const transform = (props = {}) => {
}

module.exports = renderTree => {
// console.log(flatted.stringify(renderTree, null, 2))
const [doc] = renderTree.children

return transform(doc)
Expand Down