-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gatsby): Initial GraphQL Typegen Implementation (#35487)
Co-authored-by: Michal Piechowiak <[email protected]>
- Loading branch information
Showing
36 changed files
with
1,343 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,3 +74,5 @@ cypress/fixtures | |
cypress/videos | ||
|
||
__history__.json | ||
|
||
src/gatsby-types.d.ts |
5 changes: 5 additions & 0 deletions
5
e2e-tests/development-runtime/cypress/integration/graphql-typegen/fragments-file.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
describe('fragments.graphql', () => { | ||
it('exists in .cache folder', () => { | ||
cy.readFile('.cache/typegen/fragments.graphql') | ||
}) | ||
}) |
20 changes: 20 additions & 0 deletions
20
e2e-tests/development-runtime/cypress/integration/graphql-typegen/graphql-config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
describe('graphql-config', () => { | ||
it('exists in .cache folder with correct data', () => { | ||
cy.readFile('.cache/typegen/graphql.config.json', 'utf-8').then((json) => { | ||
expect(json).to.deep.equal({ | ||
"schema": ".cache/typegen/schema.graphql", | ||
"documents": [ | ||
"src/**/**.{ts,js,tsx,jsx}", | ||
".cache/typegen/fragments.graphql" | ||
], | ||
"extensions": { | ||
"endpoints": { | ||
"default": { | ||
"url": "http://localhost:8000/___graphql" | ||
} | ||
} | ||
} | ||
}) | ||
}) | ||
}) | ||
}) |
5 changes: 5 additions & 0 deletions
5
e2e-tests/development-runtime/cypress/integration/graphql-typegen/schema-file.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
describe('schema.graphql', () => { | ||
it('exists in .cache folder', () => { | ||
cy.readFile('.cache/typegen/schema.graphql') | ||
}) | ||
}) |
5 changes: 5 additions & 0 deletions
5
e2e-tests/development-runtime/cypress/integration/graphql-typegen/ts-types.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
describe('gatsby-types.d.ts', () => { | ||
it('exists in src folder', () => { | ||
cy.readFile('src/gatsby-types.d.ts') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { EventObject } from "xstate" | ||
import { IBuildContext } from "../internal" | ||
import { IDataLayerContext, IQueryRunningContext } from "../state-machines" | ||
import { | ||
writeGraphQLFragments, | ||
writeGraphQLSchema, | ||
} from "../utils/graphql-typegen/file-writes" | ||
import { writeTypeScriptTypes } from "../utils/graphql-typegen/ts-codegen" | ||
|
||
export async function graphQLTypegen( | ||
{ | ||
program, | ||
store, | ||
parentSpan, | ||
reporter, | ||
}: IBuildContext | IQueryRunningContext | IDataLayerContext, | ||
_: EventObject, | ||
{ | ||
src: { compile }, | ||
}: { | ||
src: { | ||
type: string | ||
compile?: "all" | "schema" | "definitions" | ||
} | ||
} | ||
): Promise<void> { | ||
// TypeScript requires null/undefined checks for these | ||
// But this should never happen unless e.g. the state machine doesn't receive this information from a parent state machine | ||
if (!program || !store || !compile || !reporter) { | ||
throw new Error( | ||
`Missing required params in graphQLTypegen. program: ${!!program}. store: ${!!store}. compile: ${!!compile}` | ||
) | ||
} | ||
const directory = program.directory | ||
|
||
const activity = reporter.activityTimer( | ||
`Generating GraphQL and TypeScript types`, | ||
{ | ||
parentSpan, | ||
} | ||
) | ||
activity.start() | ||
|
||
if (compile === `all` || compile === `schema`) { | ||
await writeGraphQLSchema(directory, store) | ||
if (compile === `schema`) { | ||
reporter.verbose(`Re-Generate GraphQL Schema types`) | ||
} | ||
} | ||
if (compile === `all` || compile === `definitions`) { | ||
await writeGraphQLFragments(directory, store) | ||
await writeTypeScriptTypes(directory, store) | ||
if (compile === `definitions`) { | ||
reporter.verbose(`Re-Generate TypeScript types & GraphQL fragments`) | ||
} | ||
} | ||
|
||
activity.end() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.