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

Use the defaultName pattern for suggested model type names. #237

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions packages/graphqlgen/src/output.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import chalk from 'chalk'
import * as os from 'os'
import { graphQLToTypecriptFlowType, GraphQLTypeObject } from './source-helper'
import { ValidatedDefinition } from './validation'
import { maybeReplaceDefaultName, ValidatedDefinition } from './validation'
import { Language } from 'graphqlgen-json-schema'
import { getExtNameFromLanguage } from './path-helpers'

Expand Down Expand Up @@ -94,14 +94,15 @@ ${chalk.bold('Step 2')}: Re-run ${chalk.bold('`graphqlgen`')}`)
export function outputMissingModels(
missingModels: GraphQLTypeObject[],
language: Language,
defaultName: string | null,
) {
console.log(`❌ Some types from your application schema have model definitions that are not defined yet

${chalk.bold(
'Step 1',
)}: Copy/paste the model definitions below to your application

${missingModels.map(type => renderModelFromType(type, language)).join(os.EOL)}
${missingModels.map(type => renderModelFromType(type, language, defaultName)).join(os.EOL)}


${chalk.bold('Step 2')}: Reference the model definitions in your ${chalk.bold(
Expand Down Expand Up @@ -130,27 +131,36 @@ ${chalk.bold('Step 2')}: Re-run ${chalk.bold('`graphqlgen`')}`)
function renderModelFromType(
type: GraphQLTypeObject,
language: Language,
defaultName: string | null,
): string {
switch (language) {
case 'typescript':
return renderTypescriptModelFromType(type)
return renderTypescriptModelFromType(type, defaultName)
case 'flow':
return renderFlowModelFromType(type)
return renderFlowModelFromType(type, defaultName)
}
}

function renderTypescriptModelFromType(type: GraphQLTypeObject): string {
function renderTypescriptModelFromType(
type: GraphQLTypeObject,
defaultName: string | null,
): string {
const name = maybeReplaceDefaultName(type.name, defaultName)
return `\
export interface ${chalk.bold(type.name)} {
export interface ${chalk.bold(name)} {
${type.fields
.map(field => ` ${field.name}: ${graphQLToTypecriptFlowType(field.type)}`)
.join(os.EOL)}
}`
}

function renderFlowModelFromType(type: GraphQLTypeObject): string {
function renderFlowModelFromType(
type: GraphQLTypeObject,
defaultName: string | null,
): string {
const name = maybeReplaceDefaultName(type.name, defaultName)
return `\
export interface ${chalk.bold(type.name)} {
export interface ${chalk.bold(name)} {
${type.fields
.map(field => ` ${field.name}: ${graphQLToTypecriptFlowType(field.type)}`)
.join(',' + os.EOL)}
Expand Down
15 changes: 12 additions & 3 deletions packages/graphqlgen/src/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,22 +194,31 @@ function validateSchemaToModelMapping(
interfaceName => {
const file = interfaceNamesToPath[interfaceName]
const defaultName = getDefaultName(file)
return interfaceName === replaceDefaultName(type.name, defaultName)
return interfaceName === maybeReplaceDefaultName(type.name, defaultName)
},
)

return !typeHasMappingWithAFile
})

if (missingModels.length > 0) {
outputMissingModels(missingModels, language)
// Append the user's chosen defaultName pattern to the step 1 missing models,
// but only if they have the same pattern for all of their files
let defaultName: string | null = null
if (files.length > 0) {
const names = files.map(getDefaultName)
if (names.every(name => name === names[0])) {
defaultName = names[0]
}
}
outputMissingModels(missingModels, language, defaultName)
return false
}

return true
}

function replaceDefaultName(typeName: string, defaultName?: string | null) {
export function maybeReplaceDefaultName(typeName: string, defaultName?: string | null) {
return defaultName
? replaceVariablesInString(defaultName, { typeName })
: typeName
Expand Down