-
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): Improvements to GraphQL TypeScript Generation (#35581)
- Loading branch information
Showing
23 changed files
with
298 additions
and
138 deletions.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
e2e-tests/development-runtime/cypress/integration/graphql-typegen/files-existence.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,76 @@ | ||
// https://docs.cypress.io/api/commands/readfile#Existence | ||
// By default, cy.readFile() asserts that the file exists and will fail if it does not exist. | ||
|
||
const FRAGMENT = `fragment TestingFragment on Site { | ||
siteMetadata { | ||
author | ||
} | ||
}` | ||
|
||
const GRAPHQL_TYPE = `type CheckMePlease { | ||
hello: String! | ||
}` | ||
|
||
const TS_TYPE = `type CheckMePlease = { | ||
readonly hello: Scalars['String']; | ||
};` | ||
|
||
describe(`typecheck`, () => { | ||
it(`passes without an error`, () => { | ||
cy.exec(`npm run typecheck`) | ||
}) | ||
}) | ||
|
||
describe('fragments.graphql', () => { | ||
it('exists in .cache folder', () => { | ||
cy.readFile('.cache/typegen/fragments.graphql') | ||
}) | ||
it('contains test fragment', () => { | ||
cy.readFile('.cache/typegen/fragments.graphql').then((file) => { | ||
expect(file).to.include(FRAGMENT) | ||
}) | ||
}) | ||
}) | ||
|
||
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" | ||
} | ||
} | ||
} | ||
}) | ||
}) | ||
}) | ||
}) | ||
|
||
describe('schema.graphql', () => { | ||
it('exists in .cache folder', () => { | ||
cy.readFile('.cache/typegen/schema.graphql') | ||
}) | ||
it('contains test type', () => { | ||
cy.readFile('.cache/typegen/schema.graphql').then((file) => { | ||
expect(file).to.include(GRAPHQL_TYPE) | ||
}) | ||
}) | ||
}) | ||
|
||
describe('gatsby-types.d.ts', () => { | ||
it('exists in src folder', () => { | ||
cy.readFile('src/gatsby-types.d.ts') | ||
}) | ||
it('contains test type', () => { | ||
cy.readFile('src/gatsby-types.d.ts').then((file) => { | ||
expect(file).to.include(TS_TYPE) | ||
}) | ||
}) | ||
}) |
5 changes: 0 additions & 5 deletions
5
e2e-tests/development-runtime/cypress/integration/graphql-typegen/fragments-file.js
This file was deleted.
Oops, something went wrong.
20 changes: 0 additions & 20 deletions
20
e2e-tests/development-runtime/cypress/integration/graphql-typegen/graphql-config.js
This file was deleted.
Oops, something went wrong.
5 changes: 0 additions & 5 deletions
5
e2e-tests/development-runtime/cypress/integration/graphql-typegen/schema-file.js
This file was deleted.
Oops, something went wrong.
5 changes: 0 additions & 5 deletions
5
e2e-tests/development-runtime/cypress/integration/graphql-typegen/ts-types.js
This file was deleted.
Oops, something went wrong.
56 changes: 56 additions & 0 deletions
56
e2e-tests/development-runtime/cypress/integration/graphql-typegen/updates.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,56 @@ | ||
const QUERY_BEFORE = `type GraphQLTypegenQuery = { readonly site: { readonly siteMetadata: { readonly title: string | null } | null } | null };` | ||
const QUERY_AFTER = `type GraphQLTypegenQuery = { readonly site: { readonly siteMetadata: { readonly author: string | null, readonly title: string | null } | null } | null };` | ||
const FRAGMENT_BEFORE = `fragment SiteInformation on Site { | ||
buildTime | ||
}` | ||
const FRAGMENT_AFTER = `fragment SiteInformation on Site { | ||
buildTime | ||
trailingSlash | ||
}` | ||
|
||
beforeEach(() => { | ||
cy.visit(`/graphql-typegen/`).waitForRouteChange() | ||
}) | ||
|
||
after(() => { | ||
cy.exec(`npm run reset`) | ||
}) | ||
|
||
describe(`hot-reloading changes on GraphQL Typegen files`, () => { | ||
it(`contains initial contents in files`, () => { | ||
cy.readFile('src/gatsby-types.d.ts').then((file) => { | ||
expect(file).to.include(QUERY_BEFORE) | ||
}) | ||
cy.readFile('.cache/typegen/fragments.graphql').then((file) => { | ||
expect(file).to.include(FRAGMENT_BEFORE) | ||
}) | ||
}) | ||
|
||
it(`can edit a page query`, () => { | ||
cy.exec( | ||
`npm run update -- --file src/pages/graphql-typegen.tsx --replacements "# %AUTHOR%:author" --exact` | ||
) | ||
|
||
cy.waitForHmr() | ||
|
||
cy.readFile('src/gatsby-types.d.ts').then((file) => { | ||
expect(file).to.include(QUERY_AFTER) | ||
}) | ||
}) | ||
|
||
it(`can edit a fragment`, () => { | ||
cy.exec( | ||
`npm run update -- --file src/pages/graphql-typegen.tsx --replacements "# %TRAILING_SLASH%:trailingSlash" --exact` | ||
) | ||
|
||
cy.waitForHmr() | ||
|
||
cy.readFile('.cache/typegen/fragments.graphql').then((file) => { | ||
expect(file).to.include(FRAGMENT_AFTER) | ||
}) | ||
}) | ||
|
||
it(`successfully runs typecheck`, () => { | ||
cy.exec(`npm run typecheck`) | ||
}) | ||
}) |
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
23 changes: 23 additions & 0 deletions
23
e2e-tests/development-runtime/plugins/gatsby-node-typegen/gatsby-node.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { GatsbyNode } from "gatsby" | ||
|
||
export const createSchemaCustomization: GatsbyNode["createSchemaCustomization"] = ({ actions }) => { | ||
const { createTypes } = actions | ||
|
||
createTypes(` | ||
type CheckMePlease { | ||
hello: String! | ||
} | ||
`) | ||
} | ||
|
||
export const createPages: GatsbyNode["createPages"] = async ({ graphql }) => { | ||
await graphql<Queries.GatsbyNodeQuery>(`#graphql | ||
query GatsbyNode { | ||
site { | ||
siteMetadata { | ||
title | ||
} | ||
} | ||
} | ||
`) | ||
} |
1 change: 1 addition & 0 deletions
1
e2e-tests/development-runtime/plugins/gatsby-node-typegen/package.json
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 @@ | ||
{} |
27 changes: 27 additions & 0 deletions
27
e2e-tests/development-runtime/src/pages/graphql-typegen.tsx
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,27 @@ | ||
import React from "react" | ||
import { graphql, PageProps } from "gatsby" | ||
|
||
function GraphQLTypegen({ data }: PageProps<Queries.GraphQLTypegenQuery>) { | ||
return ( | ||
<p> | ||
{data?.site?.siteMetadata?.title} | ||
</p> | ||
) | ||
} | ||
|
||
export const query = graphql` | ||
query GraphQLTypegen{ | ||
site { | ||
siteMetadata { | ||
# %AUTHOR% | ||
title | ||
} | ||
} | ||
} | ||
fragment SiteInformation on Site { | ||
buildTime | ||
# %TRAILING_SLASH% | ||
} | ||
` | ||
|
||
export default GraphQLTypegen |
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,14 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "esnext", | ||
"lib": ["dom", "esnext"], | ||
"jsx": "react", | ||
"module": "esnext", | ||
"moduleResolution": "node", | ||
"esModuleInterop": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"strict": true, | ||
"skipLibCheck": true | ||
}, | ||
"include": ["./src/**/*", "./plugins/**/*"] | ||
} |
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.