Skip to content
This repository was archived by the owner on Dec 8, 2021. It is now read-only.

Commit 9f37315

Browse files
committed
Use the defaultName pattern for suggested model type names.
Fixes #226.
1 parent 3d52e95 commit 9f37315

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

packages/graphqlgen/src/output.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import chalk from 'chalk'
22
import * as os from 'os'
33
import { graphQLToTypecriptType, GraphQLTypeObject } from './source-helper'
4-
import { ValidatedDefinition } from './validation'
4+
import { maybeReplaceDefaultName, ValidatedDefinition } from './validation'
55

66
export function outputDefinitionFilesNotFound(
77
validatedDefinitions: ValidatedDefinition[],
@@ -89,14 +89,14 @@ ${chalk.bold(
8989
${chalk.bold('Step 2')}: Re-run ${chalk.bold('`graphqlgen`')}`)
9090
}
9191

92-
export function outputMissingModels(missingModels: GraphQLTypeObject[]) {
92+
export function outputMissingModels(missingModels: GraphQLTypeObject[], defaultName: string | null) {
9393
console.log(`❌ Some types from your application schema have model definitions that are not defined yet
9494
9595
${chalk.bold(
9696
'Step 1',
9797
)}: Copy/paste the model definitions below to your application
9898
99-
${missingModels.map(renderModelFromType).join(os.EOL)}
99+
${missingModels.map(m => renderModelFromType(m, defaultName)).join(os.EOL)}
100100
101101
102102
${chalk.bold('Step 2')}: Reference the model definitions in your ${chalk.bold(
@@ -110,9 +110,10 @@ models:
110110
${chalk.bold('Step 3')}: Re-run ${chalk.bold('`graphqlgen`')}`)
111111
}
112112

113-
function renderModelFromType(type: GraphQLTypeObject): string {
113+
function renderModelFromType(type: GraphQLTypeObject, defaultName: string | null): string {
114+
const name = maybeReplaceDefaultName(type.name, defaultName)
114115
return `\
115-
export interface ${chalk.bold(type.name)} {
116+
export interface ${chalk.bold(name)} {
116117
${type.fields
117118
.map(field => ` ${field.name}: ${graphQLToTypecriptType(field.type)}`)
118119
.join(os.EOL)}

packages/graphqlgen/src/validation.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -188,22 +188,31 @@ function validateSchemaToModelMapping(
188188
interfaceName => {
189189
const file = interfaceNamesToPath[interfaceName]
190190
const defaultName = getDefaultName(file)
191-
return interfaceName === replaceDefaultName(type.name, defaultName)
191+
return interfaceName === maybeReplaceDefaultName(type.name, defaultName)
192192
},
193193
)
194194

195195
return !typeHasMappingWithAFile
196196
})
197197

198198
if (missingModels.length > 0) {
199-
outputMissingModels(missingModels)
199+
// Append the user's chosen defaultName pattern to the step 1 missing models,
200+
// but only if they have the same pattern for all of their files
201+
let defaultName: string | null = null
202+
if (files.length > 0) {
203+
const names = files.map(getDefaultName)
204+
if (names.every(name => name === names[0])) {
205+
defaultName = names[0]
206+
}
207+
}
208+
outputMissingModels(missingModels, defaultName)
200209
return false
201210
}
202211

203212
return true
204213
}
205214

206-
function replaceDefaultName(typeName: string, defaultName?: string | null) {
215+
export function maybeReplaceDefaultName(typeName: string, defaultName?: string | null) {
207216
return defaultName
208217
? replaceVariablesInString(defaultName, { typeName })
209218
: typeName

0 commit comments

Comments
 (0)