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

Commit 2415a44

Browse files
authored
Merge pull request #237 from stephenh/use-default-name-in-step-1-output
Use the defaultName pattern for suggested model type names.
2 parents f1dfff6 + da263aa commit 2415a44

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

packages/graphqlgen/src/output.ts

Lines changed: 18 additions & 8 deletions
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 { graphQLToTypecriptFlowType, GraphQLTypeObject } from './source-helper'
4-
import { ValidatedDefinition } from './validation'
4+
import { maybeReplaceDefaultName, ValidatedDefinition } from './validation'
55
import { Language } from 'graphqlgen-json-schema'
66
import { getExtNameFromLanguage } from './path-helpers'
77

@@ -94,14 +94,15 @@ ${chalk.bold('Step 2')}: Re-run ${chalk.bold('`graphqlgen`')}`)
9494
export function outputMissingModels(
9595
missingModels: GraphQLTypeObject[],
9696
language: Language,
97+
defaultName: string | null,
9798
) {
9899
console.log(`❌ Some types from your application schema have model definitions that are not defined yet
99100
100101
${chalk.bold(
101102
'Step 1',
102103
)}: Copy/paste the model definitions below to your application
103104
104-
${missingModels.map(type => renderModelFromType(type, language)).join(os.EOL)}
105+
${missingModels.map(type => renderModelFromType(type, language, defaultName)).join(os.EOL)}
105106
106107
107108
${chalk.bold('Step 2')}: Reference the model definitions in your ${chalk.bold(
@@ -130,27 +131,36 @@ ${chalk.bold('Step 2')}: Re-run ${chalk.bold('`graphqlgen`')}`)
130131
function renderModelFromType(
131132
type: GraphQLTypeObject,
132133
language: Language,
134+
defaultName: string | null,
133135
): string {
134136
switch (language) {
135137
case 'typescript':
136-
return renderTypescriptModelFromType(type)
138+
return renderTypescriptModelFromType(type, defaultName)
137139
case 'flow':
138-
return renderFlowModelFromType(type)
140+
return renderFlowModelFromType(type, defaultName)
139141
}
140142
}
141143

142-
function renderTypescriptModelFromType(type: GraphQLTypeObject): string {
144+
function renderTypescriptModelFromType(
145+
type: GraphQLTypeObject,
146+
defaultName: string | null,
147+
): string {
148+
const name = maybeReplaceDefaultName(type.name, defaultName)
143149
return `\
144-
export interface ${chalk.bold(type.name)} {
150+
export interface ${chalk.bold(name)} {
145151
${type.fields
146152
.map(field => ` ${field.name}: ${graphQLToTypecriptFlowType(field.type)}`)
147153
.join(os.EOL)}
148154
}`
149155
}
150156

151-
function renderFlowModelFromType(type: GraphQLTypeObject): string {
157+
function renderFlowModelFromType(
158+
type: GraphQLTypeObject,
159+
defaultName: string | null,
160+
): string {
161+
const name = maybeReplaceDefaultName(type.name, defaultName)
152162
return `\
153-
export interface ${chalk.bold(type.name)} {
163+
export interface ${chalk.bold(name)} {
154164
${type.fields
155165
.map(field => ` ${field.name}: ${graphQLToTypecriptFlowType(field.type)}`)
156166
.join(',' + os.EOL)}

packages/graphqlgen/src/validation.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,22 +194,31 @@ function validateSchemaToModelMapping(
194194
interfaceName => {
195195
const file = interfaceNamesToPath[interfaceName]
196196
const defaultName = getDefaultName(file)
197-
return interfaceName === replaceDefaultName(type.name, defaultName)
197+
return interfaceName === maybeReplaceDefaultName(type.name, defaultName)
198198
},
199199
)
200200

201201
return !typeHasMappingWithAFile
202202
})
203203

204204
if (missingModels.length > 0) {
205-
outputMissingModels(missingModels, language)
205+
// Append the user's chosen defaultName pattern to the step 1 missing models,
206+
// but only if they have the same pattern for all of their files
207+
let defaultName: string | null = null
208+
if (files.length > 0) {
209+
const names = files.map(getDefaultName)
210+
if (names.every(name => name === names[0])) {
211+
defaultName = names[0]
212+
}
213+
}
214+
outputMissingModels(missingModels, language, defaultName)
206215
return false
207216
}
208217

209218
return true
210219
}
211220

212-
function replaceDefaultName(typeName: string, defaultName?: string | null) {
221+
export function maybeReplaceDefaultName(typeName: string, defaultName?: string | null) {
213222
return defaultName
214223
? replaceVariablesInString(defaultName, { typeName })
215224
: typeName

0 commit comments

Comments
 (0)