Skip to content

Commit 3abc2e8

Browse files
fix: implements graphql schema generation (#6254)
Co-authored-by: Elliot DeNolf <denolfe@gmail.com>
1 parent 40d3078 commit 3abc2e8

File tree

15 files changed

+497
-830
lines changed

15 files changed

+497
-830
lines changed

.github/workflows/main.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,6 @@ jobs:
423423
pnpm run build
424424
425425
tests-type-generation:
426-
if: false # This should be replaced with gen on a real Payload project
427426
runs-on: ubuntu-latest
428427
needs: build
429428

packages/graphql/bin.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env node
2+
3+
import { bin } from './dist/bin/index.js'
4+
5+
bin()

packages/graphql/package.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,18 @@
1313
"import": "./src/index.ts",
1414
"require": "./src/index.ts",
1515
"types": "./src/index.ts"
16+
},
17+
"./*": {
18+
"import": "./src/exports/*.ts",
19+
"require": "./src/exports/*.ts",
20+
"types": "./src/exports/*.ts"
1621
}
1722
},
1823
"main": "./src/index.ts",
1924
"types": "./src/index.d.ts",
25+
"bin": {
26+
"payload-graphql": "bin.js"
27+
},
2028
"files": [
2129
"dist"
2230
],
@@ -48,6 +56,11 @@
4856
"import": "./dist/index.js",
4957
"require": "./dist/index.js",
5058
"types": "./dist/index.d.ts"
59+
},
60+
"./*": {
61+
"import": "./dist/exports/*.js",
62+
"require": "./dist/exports/*.js",
63+
"types": "./dist/exports/*.d.ts"
5164
}
5265
},
5366
"main": "./dist/index.js",
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/* eslint-disable @typescript-eslint/no-floating-promises */
2+
import type { SanitizedConfig } from 'payload/types'
3+
4+
import fs from 'fs'
5+
import { printSchema } from 'graphql'
6+
7+
import { configToSchema } from '../index.js'
8+
export function generateSchema(config: SanitizedConfig): void {
9+
const outputFile = process.env.PAYLOAD_GRAPHQL_SCHEMA_PATH || config.graphQL.schemaOutputFile
10+
11+
const { schema } = configToSchema(config)
12+
13+
fs.writeFileSync(outputFile, printSchema(schema))
14+
}

packages/graphql/src/bin/index.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/* eslint-disable no-console */
2+
import minimist from 'minimist'
3+
import { findConfig, importConfig, loadEnv } from 'payload/node'
4+
5+
import { generateSchema } from './generateSchema.js'
6+
7+
export const bin = async () => {
8+
loadEnv()
9+
const configPath = findConfig()
10+
const config = await importConfig(configPath)
11+
12+
const args = minimist(process.argv.slice(2))
13+
const script = (typeof args._[0] === 'string' ? args._[0] : '').toLowerCase()
14+
15+
if (script === 'generate:schema') {
16+
return generateSchema(config)
17+
}
18+
19+
console.log(`Unknown script: "${script}".`)
20+
process.exit(1)
21+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { generateSchema } from '../bin/generateSchema.js'

packages/payload/src/bin/loader/read-default-tsconfig.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,29 @@
1+
/**
2+
*
3+
* MIT License
4+
*
5+
* Copyright (c) 2020-present LongYinan
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in all
15+
* copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
* SOFTWARE.
24+
*
25+
*/
26+
127
import type { Options } from '@swc-node/core'
228

329
import { resolve } from 'path'

packages/payload/src/config/defaults.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export const defaults: Omit<Config, 'db' | 'editor' | 'secret'> = {
2626
graphQL: {
2727
disablePlaygroundInProduction: true,
2828
maxComplexity: 1000,
29+
schemaOutputFile: `${typeof process?.cwd === 'function' ? process.cwd() : ''}/schema.graphql`,
2930
},
3031
hooks: {},
3132
i18n: {},

packages/payload/src/config/schema.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ export default joi.object({
110110
maxComplexity: joi.number(),
111111
mutations: joi.function(),
112112
queries: joi.function(),
113+
schemaOutputFile: joi.string(),
113114
}),
114115
hooks: joi.object().keys({
115116
afterError: joi.func(),

packages/payload/src/config/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,10 @@ export type Config = {
564564
* @see https://payloadcms.com/docs/graphql/extending
565565
*/
566566
queries?: GraphQLExtension
567+
/**
568+
* Filepath to write the generated schema to
569+
*/
570+
schemaOutputFile?: string
567571
}
568572
/**
569573
* Tap into Payload-wide hooks.

0 commit comments

Comments
 (0)