-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolver.ts
110 lines (101 loc) · 3.57 KB
/
resolver.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { AbstractCommand } from '../abstract-command'
import * as transform_ from 'graphql-json-schema'
const transform = transform_
import { Operation, Schema } from './resolver-types'
import {
functionOperationPrefix,
gqlMethodSignature,
gqlMethodSuffix,
keysToGenerate,
operationFuntionsSuffix,
operationPrefix,
resolverPrefix,
resolverSuffix,
subscriptionSubscribeDefinition
} from './resolver-constants'
import { fileExists, readFileContent, writeToFile } from '../../utils/file-operations'
import logger from '../../utils/logger'
export class Resolver extends AbstractCommand {
public getName(): string {
return 'resolver <path-to-type> <path-to-resolver>'
}
public getAction(): (...args: any[]) => void {
return async (pathToType: string, pathToResolver: string) => {
try {
const resolverExists = await fileExists(pathToResolver)
if (resolverExists) {
logger.warn(
'Resolver file already exists. please select a different path or remove the current file...'
)
return
}
const schema = await this.getSchemaByFilePath(pathToType)
if (!schema || schema === '') {
logger.error('Failed to read schema, exiting...')
return
}
const schemaJsonRepresentation = transform(schema)
const resolverContent = this.resolverContentByJsonSchema(schemaJsonRepresentation)
logger.info('Writing resolvers to ' + pathToResolver + '...')
await writeToFile(pathToResolver, resolverContent)
logger.info('Resolver file was created successfully!')
} catch (err) {
logger.error(`Error in generating resolver- ${err}`)
}
}
}
public getAlias(): string {
return 'r'
}
public getDescription(): string {
return 'Generate new resolver by path to type file'
}
private resolverContentByJsonSchema(jsonSchema: Schema): string {
const schemaDefinitions = jsonSchema.definitions
let resolverContent = resolverPrefix
Object.entries(schemaDefinitions).forEach(([key, operation]: [string, Operation]) => {
if (keysToGenerate.includes(key)) {
resolverContent += this.getTypeOperationContent(operation)
}
})
resolverContent += resolverSuffix
return resolverContent
}
private getTypeOperationContent(operation: Operation): string {
let opStrRepresentation = '\t' + operation.title + operationPrefix
const operationProperties = operation.properties
Object.keys(operationProperties).forEach((key: string) => {
opStrRepresentation += this.getTypeOperationParamContent(key, operation)
})
opStrRepresentation += operationFuntionsSuffix
return opStrRepresentation
}
private getTypeOperationParamContent(key: string, operation: Operation): string {
let operationParamStr = '\t\t' + key
if (operation.title === 'Subscription') {
operationParamStr +=
operationPrefix +
subscriptionSubscribeDefinition +
gqlMethodSignature +
functionOperationPrefix +
'\t' +
operationFuntionsSuffix
} else {
operationParamStr += gqlMethodSignature + gqlMethodSuffix
}
return operationParamStr
}
private async getSchemaByFilePath(pathToType: string): Promise<string> {
try {
const typeFileContent = await readFileContent(pathToType)
const splittedBySchema = typeFileContent.split('`')
if (splittedBySchema.length !== 3) {
return ''
}
return splittedBySchema[1]
} catch (err) {
logger.error(`Got error in reading type file- ${err}`)
return ''
}
}
}