|
1 | 1 | import chalk from 'chalk'
|
2 | 2 | import * as os from 'os'
|
3 | 3 | import { graphQLToTypecriptFlowType, GraphQLTypeObject } from './source-helper'
|
4 |
| -import { ValidatedDefinition } from './validation' |
| 4 | +import { maybeReplaceDefaultName, ValidatedDefinition } from './validation' |
5 | 5 | import { Language } from 'graphqlgen-json-schema'
|
6 | 6 | import { getExtNameFromLanguage } from './path-helpers'
|
7 | 7 |
|
@@ -94,14 +94,15 @@ ${chalk.bold('Step 2')}: Re-run ${chalk.bold('`graphqlgen`')}`)
|
94 | 94 | export function outputMissingModels(
|
95 | 95 | missingModels: GraphQLTypeObject[],
|
96 | 96 | language: Language,
|
| 97 | + defaultName: string | null, |
97 | 98 | ) {
|
98 | 99 | console.log(`❌ Some types from your application schema have model definitions that are not defined yet
|
99 | 100 |
|
100 | 101 | ${chalk.bold(
|
101 | 102 | 'Step 1',
|
102 | 103 | )}: Copy/paste the model definitions below to your application
|
103 | 104 |
|
104 |
| -${missingModels.map(type => renderModelFromType(type, language)).join(os.EOL)} |
| 105 | +${missingModels.map(type => renderModelFromType(type, language, defaultName)).join(os.EOL)} |
105 | 106 |
|
106 | 107 |
|
107 | 108 | ${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`')}`)
|
130 | 131 | function renderModelFromType(
|
131 | 132 | type: GraphQLTypeObject,
|
132 | 133 | language: Language,
|
| 134 | + defaultName: string | null, |
133 | 135 | ): string {
|
134 | 136 | switch (language) {
|
135 | 137 | case 'typescript':
|
136 |
| - return renderTypescriptModelFromType(type) |
| 138 | + return renderTypescriptModelFromType(type, defaultName) |
137 | 139 | case 'flow':
|
138 |
| - return renderFlowModelFromType(type) |
| 140 | + return renderFlowModelFromType(type, defaultName) |
139 | 141 | }
|
140 | 142 | }
|
141 | 143 |
|
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) |
143 | 149 | return `\
|
144 |
| -export interface ${chalk.bold(type.name)} { |
| 150 | +export interface ${chalk.bold(name)} { |
145 | 151 | ${type.fields
|
146 | 152 | .map(field => ` ${field.name}: ${graphQLToTypecriptFlowType(field.type)}`)
|
147 | 153 | .join(os.EOL)}
|
148 | 154 | }`
|
149 | 155 | }
|
150 | 156 |
|
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) |
152 | 162 | return `\
|
153 |
| -export interface ${chalk.bold(type.name)} { |
| 163 | +export interface ${chalk.bold(name)} { |
154 | 164 | ${type.fields
|
155 | 165 | .map(field => ` ${field.name}: ${graphQLToTypecriptFlowType(field.type)}`)
|
156 | 166 | .join(',' + os.EOL)}
|
|
0 commit comments