Skip to content
This repository was archived by the owner on May 10, 2018. It is now read-only.

Commit a2e3ff9

Browse files
committed
style(graphcool-ts): formatting
1 parent 2591469 commit a2e3ff9

File tree

1 file changed

+136
-69
lines changed

1 file changed

+136
-69
lines changed

src/generators/graphcool-ts.ts

+136-69
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1-
import {
2-
GraphQLUnionType,
3-
GraphQLWrappingType,
4-
GraphQLObjectType,
5-
GraphQLInputObjectType,
6-
GraphQLInputField,
7-
GraphQLField,
8-
GraphQLInputType,
9-
GraphQLOutputType,
10-
GraphQLScalarType,
11-
GraphQLNamedType,
12-
13-
isNonNullType,
14-
isListType,
15-
GraphQLFieldMap,
16-
GraphQLEnumType,
17-
GraphQLType,
18-
GraphQLInterfaceType,
1+
import {
2+
GraphQLUnionType,
3+
GraphQLWrappingType,
4+
GraphQLObjectType,
5+
GraphQLInputObjectType,
6+
GraphQLInputField,
7+
GraphQLField,
8+
GraphQLInputType,
9+
GraphQLOutputType,
10+
GraphQLScalarType,
11+
GraphQLNamedType,
12+
isNonNullType,
13+
isListType,
14+
GraphQLFieldMap,
15+
GraphQLEnumType,
16+
GraphQLType,
17+
GraphQLInterfaceType
1918
} from 'graphql'
2019

2120
import { Generator } from '../types'
@@ -30,7 +29,7 @@ export const generator: Generator = {
3029
RootType: renderRootType,
3130
SchemaType: renderSchemaInterface,
3231
Main: renderMainMethod,
33-
Header: renderHeader,
32+
Header: renderHeader
3433
}
3534

3635
const scalarMapping = {
@@ -49,7 +48,11 @@ const typeDefs = \`
4948
${schema}\``
5049
}
5150

52-
function renderMainMethod(queryType: GraphQLObjectType, mutationType?: GraphQLObjectType | null, subscriptionType?: GraphQLObjectType | null) {
51+
function renderMainMethod(
52+
queryType: GraphQLObjectType,
53+
mutationType?: GraphQLObjectType | null,
54+
subscriptionType?: GraphQLObjectType | null
55+
) {
5356
return `export class Graphcool extends BaseGraphcool {
5457
5558
constructor({ endpoint, secret, fragmentReplacements, debug }: BaseGraphcoolOptions) {
@@ -58,61 +61,96 @@ function renderMainMethod(queryType: GraphQLObjectType, mutationType?: GraphQLOb
5861
5962
query: Query = {
6063
${renderMainMethodFields('query', queryType.getFields())}
61-
}${mutationType ? `
64+
}${
65+
mutationType
66+
? `
6267
6368
mutation: Mutation = {
6469
${renderMainMethodFields('mutation', mutationType.getFields())}
65-
}`: ''}
70+
}`
71+
: ''
72+
}
6673
}`
6774
}
6875

69-
7076
export function renderMainMethodFields(operation: string, fields: GraphQLFieldMap<any, any>): string {
71-
return Object.keys(fields).map(f => {
72-
const field = fields[f]
73-
return ` ${field.name}: (args, info): Promise<${renderFieldType(field.type)}${!isNonNullType(field.type) ? ' | null' : ''}> => super.delegate('${operation}', '${field.name}', args, {}, info)`
74-
}).join(',\n')
77+
return Object.keys(fields)
78+
.map(f => {
79+
const field = fields[f]
80+
return ` ${field.name}: (args, info): Promise<${renderFieldType(field.type)}${
81+
!isNonNullType(field.type) ? ' | null' : ''
82+
}> => super.delegate('${operation}', '${field.name}', args, {}, info)`
83+
})
84+
.join(',\n')
7585
}
7686

7787
function renderScalarType(type: GraphQLScalarType): string {
78-
if (type.name === 'ID') { return renderIDType(type)}
79-
return `${type.description ? `/*
88+
if (type.name === 'ID') {
89+
return renderIDType(type)
90+
}
91+
return `${
92+
type.description
93+
? `/*
8094
${type.description}
8195
*/
82-
`: ''}export type ${type.name} = ${scalarMapping[type.name] || 'string'}`
96+
`
97+
: ''
98+
}export type ${type.name} = ${scalarMapping[type.name] || 'string'}`
8399
}
84100

85101
function renderIDType(type: GraphQLScalarType): string {
86-
return `${type.description ? `/*
102+
return `${
103+
type.description
104+
? `/*
87105
${type.description}
88106
*/
89-
`: ''}export type ${type.name}_Input = ${scalarMapping[type.name] || 'string'}
107+
`
108+
: ''
109+
}export type ${type.name}_Input = ${scalarMapping[type.name] || 'string'}
90110
export type ${type.name}_Output = string`
91111
}
92112

93113
function renderEnumType(type: GraphQLEnumType): string {
94114
return `${renderDescription(type.description)}export type ${type.name} =
95-
${type.getValues().map(e => ` '${e.name}'`).join(' |\n')}`
115+
${type
116+
.getValues()
117+
.map(e => ` '${e.name}'`)
118+
.join(' |\n')}`
96119
}
97-
120+
98121
function renderRootType(type: GraphQLObjectType): string {
99-
const fieldDefinition = Object.keys(type.getFields()).map(f => {
100-
const field = type.getFields()[f]
101-
return ` ${field.name}: (args: {${field.args.length > 0 ? ' ': ''}${field.args.map(f => `${renderFieldName(f)}: ${renderFieldType(f.type)}`).join(', ')}${field.args.length > 0 ? ' ': ''}}, info?: GraphQLResolveInfo | string) => Promise<${renderFieldType(field.type)}${!isNonNullType(field.type) ? ' | null' : ''}>`
102-
}).join('\n')
122+
const fieldDefinition = Object.keys(type.getFields())
123+
.map(f => {
124+
const field = type.getFields()[f]
125+
return ` ${field.name}: (args: {${field.args.length > 0 ? ' ' : ''}${field.args
126+
.map(f => `${renderFieldName(f)}: ${renderFieldType(f.type)}`)
127+
.join(', ')}${
128+
field.args.length > 0 ? ' ' : ''
129+
}}, info?: GraphQLResolveInfo | string) => Promise<${renderFieldType(field.type)}${
130+
!isNonNullType(field.type) ? ' | null' : ''
131+
}>`
132+
})
133+
.join('\n')
103134

104135
return renderTypeWrapper(type.name, type.description, fieldDefinition)
105136
}
106137

107138
function renderUnionType(type: GraphQLUnionType): string {
108-
return `${renderDescription(type.description)}export type ${type.name} = ${type.getTypes().map(t => t.name).join(' | ')}`
109-
}
110-
111-
function renderObjectType(type: GraphQLObjectType | GraphQLInputObjectType | GraphQLInterfaceType): string {
112-
const fieldDefinition = Object.keys(type.getFields()).map(f => {
113-
const field = type.getFields()[f]
114-
return ` ${renderFieldName(field)}: ${renderFieldType(field.type)}`
115-
}).join('\n')
139+
return `${renderDescription(type.description)}export type ${type.name} = ${type
140+
.getTypes()
141+
.map(t => t.name)
142+
.join(' | ')}`
143+
}
144+
145+
function renderObjectType(
146+
type: GraphQLObjectType | GraphQLInputObjectType | GraphQLInterfaceType
147+
): string {
148+
const fieldDefinition = Object.keys(type.getFields())
149+
.map(f => {
150+
const field = type.getFields()[f]
151+
return ` ${renderFieldName(field)}: ${renderFieldType(field.type)}`
152+
})
153+
.join('\n')
116154

117155
let interfaces: GraphQLInterfaceType[] = []
118156
if (type instanceof GraphQLObjectType) {
@@ -122,11 +160,15 @@ function renderObjectType(type: GraphQLObjectType | GraphQLInputObjectType | Gra
122160
return renderInterfaceWrapper(type.name, type.description, interfaces, fieldDefinition)
123161
}
124162

125-
function renderInputObjectType(type: GraphQLObjectType | GraphQLInputObjectType | GraphQLInterfaceType): string {
126-
const fieldDefinition = Object.keys(type.getFields()).map(f => {
127-
const field = type.getFields()[f]
128-
return ` ${renderFieldName(field)}: ${renderInputFieldType(field.type)}`
129-
}).join('\n')
163+
function renderInputObjectType(
164+
type: GraphQLObjectType | GraphQLInputObjectType | GraphQLInterfaceType
165+
): string {
166+
const fieldDefinition = Object.keys(type.getFields())
167+
.map(f => {
168+
const field = type.getFields()[f]
169+
return ` ${renderFieldName(field)}: ${renderInputFieldType(field.type)}`
170+
})
171+
.join('\n')
130172

131173
let interfaces: GraphQLInterfaceType[] = []
132174
if (type instanceof GraphQLObjectType) {
@@ -139,37 +181,58 @@ function renderInputObjectType(type: GraphQLObjectType | GraphQLInputObjectType
139181
function renderFieldName(field: GraphQLInputField | GraphQLField<any, any>) {
140182
return `${field.name}${isNonNullType(field.type) ? '' : '?'}`
141183
}
142-
184+
143185
function renderFieldType(type: GraphQLInputType | GraphQLOutputType) {
144186
if (isNonNullType(type)) {
145187
return renderFieldType((type as GraphQLWrappingType).ofType)
146-
}
188+
}
147189
if (isListType(type)) {
148190
return `Array<${renderFieldType((type as GraphQLWrappingType).ofType)}>`
149191
}
150-
return `${(type as GraphQLNamedType).name}${(type as GraphQLNamedType).name == 'ID' ? '_Output' : ''}`
192+
return `${(type as GraphQLNamedType).name}${(type as GraphQLNamedType).name === 'ID' ? '_Output' : ''}`
151193
}
152194

153195
function renderInputFieldType(type: GraphQLInputType | GraphQLOutputType) {
154196
if (isNonNullType(type)) {
155197
return renderInputFieldType((type as GraphQLWrappingType).ofType)
156-
}
198+
}
157199
if (isListType(type)) {
158-
return `Array<${renderInputFieldType((type as GraphQLWrappingType).ofType)}> | ${renderInputFieldType((type as GraphQLWrappingType).ofType)}`
200+
return `Array<${renderInputFieldType(
201+
(type as GraphQLWrappingType).ofType
202+
)}> | ${renderInputFieldType((type as GraphQLWrappingType).ofType)}`
159203
}
160-
return `${(type as GraphQLNamedType).name}${(type as GraphQLNamedType).name == 'ID' ? '_Input' : ''}`
204+
return `${(type as GraphQLNamedType).name}${(type as GraphQLNamedType).name === 'ID' ? '_Input' : ''}`
161205
}
162-
163-
function renderSchemaInterface(queryType: GraphQLObjectType, mutationType?: GraphQLObjectType | null, subscriptionType?: GraphQLObjectType | null) {
206+
207+
function renderSchemaInterface(
208+
queryType: GraphQLObjectType,
209+
mutationType?: GraphQLObjectType | null,
210+
subscriptionType?: GraphQLObjectType | null
211+
) {
164212
return `export interface Schema {
165213
query: ${queryType.name}
166-
${mutationType ? ` mutation: ${mutationType.name}
167-
` : ''}${subscriptionType ? ` subscription: ${subscriptionType.name}
168-
` : ''}}`
169-
}
170-
171-
function renderInterfaceWrapper(typeName: string, typeDescription: string, interfaces: GraphQLInterfaceType[], fieldDefinition: string): string {
172-
return `${renderDescription(typeDescription)}export interface ${typeName}${interfaces.length > 0 ? ` extends ${interfaces.map(i => i.name).join(', ')}`: ''} {
214+
${
215+
mutationType
216+
? ` mutation: ${mutationType.name}
217+
`
218+
: ''
219+
}${
220+
subscriptionType
221+
? ` subscription: ${subscriptionType.name}
222+
`
223+
: ''
224+
}}`
225+
}
226+
227+
function renderInterfaceWrapper(
228+
typeName: string,
229+
typeDescription: string,
230+
interfaces: GraphQLInterfaceType[],
231+
fieldDefinition: string
232+
): string {
233+
return `${renderDescription(typeDescription)}export interface ${typeName}${
234+
interfaces.length > 0 ? ` extends ${interfaces.map(i => i.name).join(', ')}` : ''
235+
} {
173236
${fieldDefinition}
174237
}`
175238
}
@@ -181,8 +244,12 @@ ${fieldDefinition}
181244
}
182245

183246
function renderDescription(description?: string) {
184-
return `${description ? `/*
247+
return `${
248+
description
249+
? `/*
185250
${description.split('\n').map(l => ` * ${l}\n`)}
186251
*/
187-
`: ''}`
188-
}
252+
`
253+
: ''
254+
}`
255+
}

0 commit comments

Comments
 (0)