Skip to content

Commit 8724aaf

Browse files
fix: added support for graphql schema
1 parent 28104ec commit 8724aaf

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

src/commands/tsgen.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ export default class TypeScriptCodeGeneratorCommand extends Command {
4343
default: true,
4444
allowNo: true,
4545
}),
46+
47+
graphql: flags.boolean({
48+
description: 'support graphql schema',
49+
default: false,
50+
}),
4651
};
4752

4853
async run() {
@@ -51,6 +56,7 @@ export default class TypeScriptCodeGeneratorCommand extends Command {
5156

5257
const token = this.getToken(flags['token-alias'])
5358
const prefix = flags.prefix
59+
const graphql = flags.graphql
5460
const includeDocumentation = flags.doc
5561
const outputPath = flags.output
5662

@@ -65,14 +71,14 @@ export default class TypeScriptCodeGeneratorCommand extends Command {
6571
const config: StackConnectionConfig = {
6672
apiKey: token.apiKey,
6773
token: token.token,
68-
region: (this.region.name === 'eu') ? 'eu': undefined,
74+
region: (this.region.name === 'EU') ? 'EU' : null,
6975
environment: token.environment || '',
7076
}
7177

7278
const client = await stackConnect(this.deliveryAPIClient.Stack, config)
7379

7480
if (client.types) {
75-
const result = await tsgenRunner(outputPath, client.types, prefix, includeDocumentation)
81+
const result = await tsgenRunner(outputPath, client.types, prefix, includeDocumentation, graphql)
7682
this.log(`Wrote ${result.definitions} Content Types to '${result.outputPath}'.`)
7783
} else {
7884
this.log('No Content Types exist in the Stack.')

src/lib/tsgen/runner.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {DocumentationGenerator} from './docgen/doc'
77
import JSDocumentationGenerator from './docgen/jsdoc'
88
import NullDocumentationGenerator from './docgen/nulldoc'
99
import tsgenFactory from './factory'
10+
import {supportGraphql} from '../util'
1011

1112
async function format(definition: string) {
1213
const prettierConfig = await prettier.resolveConfig(process.cwd())
@@ -26,7 +27,7 @@ function createOutputPath(outputFile: string) {
2627
return outputPath
2728
}
2829

29-
export default async function tsgenRunner(outputFile: string, contentTypes: any[], prefix = '', includeDocumentation = true) {
30+
export default async function tsgenRunner(outputFile: string, contentTypes: any[], prefix = '', includeDocumentation = true, graphql: boolean) {
3031
const docgen: DocumentationGenerator = includeDocumentation ? new JSDocumentationGenerator() : new NullDocumentationGenerator()
3132

3233
const outputPath = createOutputPath(outputFile)
@@ -40,7 +41,10 @@ export default async function tsgenRunner(outputFile: string, contentTypes: any[
4041
},
4142
})
4243

43-
for (const contentType of contentTypes) {
44+
for (let contentType of contentTypes) {
45+
if (graphql) {
46+
contentType = supportGraphql(contentType)
47+
}
4448
const tsgenResult = tsgen(contentType)
4549

4650
definitions.push(tsgenResult.definition)
@@ -52,6 +56,7 @@ export default async function tsgenRunner(outputFile: string, contentTypes: any[
5256

5357
const output = await format(
5458
[
59+
5560
defaultInterfaces(prefix).join('\n\n'),
5661
[...globalFields].join('\n\n'),
5762
definitions.join('\n\n'),

src/lib/util/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export function supportGraphql(contentType: any) {
2+
contentType.schema = contentType.schema.map((element: any) => {
3+
if (element.data_type === 'reference') {
4+
element.uid += 'Connection'
5+
}
6+
return element
7+
})
8+
return contentType
9+
}

0 commit comments

Comments
 (0)