-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathPrismaTypescriptGenerator.ts
75 lines (64 loc) · 1.97 KB
/
PrismaTypescriptGenerator.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
import { TypescriptGenerator } from 'graphql-binding'
import { printSchema } from 'graphql'
import { getExistsTypes } from './utils'
export class PrismaTypescriptGenerator extends TypescriptGenerator {
constructor(options) {
super(options)
}
render() {
return this.compile`\
${this.renderImports()}
export interface Query ${this.renderQueries()}
export interface Mutation ${this.renderMutations()}
export interface Subscription ${this.renderSubscriptions()}
export interface Exists ${this.renderExists()}
export interface Prisma {
query: Query
mutation: Mutation
subscription: Subscription
exists: Exists
request: <T = any>(query: string, variables?: {[key: string]: any}) => Promise<T>
delegate(operation: 'query' | 'mutation', fieldName: string, args: {
[key: string]: any;
}, infoOrQuery?: GraphQLResolveInfo | string, options?: Options): Promise<any>;
delegateSubscription(fieldName: string, args?: {
[key: string]: any;
}, infoOrQuery?: GraphQLResolveInfo | string, options?: Options): Promise<AsyncIterator<any>>;
getAbstractResolvers(filterSchema?: GraphQLSchema | string): IResolvers;
}
export interface BindingConstructor<T> {
new(options: BasePrismaOptions): T
}
/**
* Type Defs
*/
${this.renderTypedefs()}
${this.renderExports()}
/**
* Types
*/
${this.renderTypes()}`
}
renderImports() {
return `\
import { GraphQLResolveInfo, GraphQLSchema } from 'graphql'
import { IResolvers } from 'graphql-tools/dist/Interfaces'
import { Options } from 'graphql-binding'
import { makePrismaBindingClass, BasePrismaOptions } from 'prisma-binding'`
}
renderExports() {
return `export const Prisma = makePrismaBindingClass<BindingConstructor<Prisma>>({typeDefs})`
}
renderTypedefs() {
return (
'const typeDefs = `' + printSchema(this.schema).replace(/`/g, '\\`') + '`'
)
}
renderExists() {
const queryType = this.schema.getQueryType()
if (queryType) {
return `{\n${getExistsTypes(queryType)}\n}`
}
return '{}'
}
}