Skip to content

Commit

Permalink
feat(gatsby): PQR workers can access inference metadata (#31858)
Browse files Browse the repository at this point in the history
* test(gatsby): check if worker can access node created in different process

* make timeout longer for workerpool tests

* test fixes from other pr

* wip: saveStateForWorkers / loadStateInWorker

* basic functionality + type fixes

* wip (failing tests)

* revert child

* add redux state func

* revert early return changes

* initial

* update tests and add env var

* updates

* send inference metadata

* ignore all node_modules

* rename GatsbyStateSlices type to GatsbyStateKeys to more accurately describe it

* actually fix @babel/register

* Restore pool.ts

Co-authored-by: Michal Piechowiak <misiek.piechowiak@gmail.com>
Co-authored-by: gatsbybot <mathews.kyle+gatsbybot@gmail.com>
  • Loading branch information
3 people committed Jun 14, 2021
1 parent 222a5ed commit 315b694
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 2 deletions.
4 changes: 4 additions & 0 deletions packages/gatsby/src/bootstrap/index.ts
Expand Up @@ -61,6 +61,10 @@ export async function bootstrap(

await rebuildSchemaWithSitePage(context)

if (process.env.GATSBY_EXPERIMENTAL_PARALLEL_QUERY_RUNNING) {
saveStateForWorkers([`inferenceMetadata`])
}

await extractQueries(context)

if (process.env.GATSBY_EXPERIMENTAL_PARALLEL_QUERY_RUNNING) {
Expand Down
45 changes: 44 additions & 1 deletion packages/gatsby/src/utils/worker/__tests__/share-state.ts
Expand Up @@ -168,7 +168,7 @@ describe(`worker (share-state)`, () => {
`)
})

it(`can set slices results into state and access it`, async () => {
it(`can set slices results into state and access page & static queries`, async () => {
worker = createTestWorker()
const staticQueryID = `1`

Expand Down Expand Up @@ -234,4 +234,47 @@ describe(`worker (share-state)`, () => {
}
`)
})

it(`can set slices results into state and access inference metadata`, async () => {
worker = createTestWorker()

store.dispatch({
type: `BUILD_TYPE_METADATA`,
payload: {
typeName: `Test`,
nodes: [
{
id: `1`,
parent: null,
children: [],
foo: `bar`,
internal: { type: `Test` },
},
],
},
})

saveStateForWorkers([`inferenceMetadata`])

await worker.setInferenceMetadata()

const inf = await worker.getInferenceMetadata(`Test`)

expect(inf).toMatchInlineSnapshot(`
Object {
"dirty": true,
"fieldMap": Object {
"foo": Object {
"string": Object {
"example": "bar",
"first": "1",
"total": 1,
},
},
},
"ignoredFields": Object {},
"total": 1,
}
`)
})
})
Expand Up @@ -5,6 +5,7 @@ import {
IGatsbyPageComponent,
IGatsbyStaticQueryComponents,
} from "../../../../redux/types"
import { ITypeMetadata } from "../../../../schema/infer/inference-metadata"
import reporter from "gatsby-cli/lib/reporter"
import apiRunner from "../../../api-runner-node"

Expand Down Expand Up @@ -33,6 +34,9 @@ export function getStaticQueryComponent(
): IGatsbyStaticQueryComponents | undefined {
return store.getState().staticQueryComponents.get(id)
}
export function getInferenceMetadata(typeName: string): ITypeMetadata {
return store.getState().inferenceMetadata.typeMap[typeName]
}

// test: reporter
export function log(message: string): boolean {
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/src/utils/worker/child/index.ts
@@ -1,4 +1,4 @@
// Note: this doesn't check for conflicts between module exports
export { renderHTMLProd, renderHTMLDev } from "./render-html"
export { setQueries } from "./schema"
export { setQueries, setInferenceMetadata } from "./schema"
export { loadConfigAndPlugins } from "./load-config-and-plugins"
3 changes: 3 additions & 0 deletions packages/gatsby/src/utils/worker/child/schema.ts
@@ -1,5 +1,8 @@
import { setState } from "./state"

export function setInferenceMetadata(): void {
setState([`inferenceMetadata`])
}
export function setQueries(): void {
setState([`components`, `staticQueryComponents`])
}

0 comments on commit 315b694

Please sign in to comment.