feat(gatsby): Allow awaiting API run triggered by createNode action - #12748
Conversation
wardpeet
left a comment
There was a problem hiding this comment.
In my opinion, this looks great!
I just have a question if you could elaborate on
When the createNode action is called, we currently have no way of knowing when subsequent onCreateNode API calls have finished. This is e.g. a problem in custom field resolvers, where we cannot safely call createRemoteFileNode.
why can't we safely call createRemoteFileNode?
Currently the The problem is that when we call |
|
Anything missing from this? |
|
Thinking this looks good--let's get this merged today? |
|
@pieh want to take a look at this soon? |
Yes, on it. |
|
I just verified using this simple snippet: ---
title: Hello World
date: "2015-05-01T22:12:03.284Z"
remoteURL: "https://images.unsplash.com/photo-1556379092-dca659792591"
---exports.sourceNodes = ({ actions, schema, store, cache, createNodeId }) => {
actions.createTypes(`
type MarkdownRemark implements Node {
frontmatter: Frontmatter
}
`)
actions.createTypes(
schema.buildObjectType({
name: "Frontmatter",
fields: {
remoteURL: {
type: `File`,
resolve: async (source, args, context, info) => {
const fieldValue = source[info.fieldName]
console.log({ fieldValue })
const fileNode = await createRemoteFileNode({
url: fieldValue,
store,
cache,
createNode: actions.createNode,
createNodeId,
ext: `.jpg`,
})
return fileNode
},
},
},
})
)
}that this indeed works as expected. We will need to look at bumping/adjusting query running concurrency later on (because using above will limit downloading files to be just from 4 queries, and this can be bottleneck). Will run few more tests (against our .org and few other sites to verify there isn't any unintentional breakage somewhere, but code LGTM |
|
Also additional note - (not a blocker) - right now, fact that |
Co-Authored-By: stefanprobst <stefan.probst@univie.ac.at>
yes good point! |
Added short note about the returned Promise |
When the
createNodeaction is called, we currently have no way of knowing when subsequentonCreateNodeAPI calls have finished. This is e.g. a problem in custom field resolvers, where we cannot safely callcreateRemoteFileNode.This PR makes
createNodea thunk which still synchronously dispatches the actions, but returns a Promise of the triggered API run that can optionally be awaited.