@@ -42,94 +42,18 @@ const scalarMapping = {
42
42
}
43
43
44
44
function renderHeader ( schema : string ) : string {
45
- return `import { FragmentReplacements } from 'graphcool-binding/dist/src/extractFragmentReplacements';
46
- import { GraphcoolLink } from 'graphcool-binding/dist/src/GraphcoolLink';
47
- import { buildFragmentInfo, buildTypeLevelInfo } from 'graphcool-binding/dist/src/prepareInfo';
48
- import { GraphQLResolveInfo, GraphQLSchema } from 'graphql';
49
- import { GraphQLClient } from 'graphql-request';
50
- import { SchemaCache } from 'graphql-schema-cache';
51
- import { delegateToSchema } from 'graphql-tools';
52
- import { sign } from 'jsonwebtoken';
53
-
54
- // -------------------
55
- // This should be in graphcool-binding
56
- interface BindingOptions {
57
- fragmentReplacements?: FragmentReplacements
58
- endpoint: string
59
- secret: string
60
- }
61
-
62
- interface BaseBindingOptions extends BindingOptions {
63
- typeDefs: string
64
- }
65
-
66
- const schemaCache = new SchemaCache()
67
-
68
- class BaseBinding {
69
- private remoteSchema: GraphQLSchema
70
- private fragmentReplacements: FragmentReplacements
71
- private graphqlClient: GraphQLClient
72
-
73
- constructor({
74
- typeDefs,
75
- endpoint,
76
- secret,
77
- fragmentReplacements} : BaseBindingOptions) {
78
-
79
- fragmentReplacements = fragmentReplacements || {}
80
-
81
- const token = sign({}, secret)
82
- const link = new GraphcoolLink(endpoint, token)
83
-
84
- this.remoteSchema = schemaCache.makeExecutableSchema({
85
- link,
86
- typeDefs,
87
- key: endpoint,
88
- })
89
-
90
- this.fragmentReplacements = fragmentReplacements
91
-
92
- this.graphqlClient = new GraphQLClient(endpoint, {
93
- headers: { Authorization: \`Bearer \${token}\` },
94
- })
95
- }
96
-
97
- delegate<T>(operation: 'query' | 'mutation', prop: string, args, info?: GraphQLResolveInfo | string): Promise<T> {
98
- if (!info) {
99
- info = buildTypeLevelInfo(prop, this.remoteSchema, operation)
100
- } else if (typeof info === 'string') {
101
- info = buildFragmentInfo(prop, this.remoteSchema, operation, info)
102
- }
103
-
104
- return delegateToSchema(
105
- this.remoteSchema,
106
- this.fragmentReplacements,
107
- operation,
108
- prop,
109
- args || {},
110
- {},
111
- info,
112
- )
113
- }
114
-
115
- async request<T = any>(
116
- query: string,
117
- variables?: { [key: string]: any },
118
- ): Promise<T> {
119
- return this.graphqlClient.request<T>(query, variables)
120
- }
121
- }
122
- // -------------------
45
+ return `import { Graphcool, BaseGraphcoolOptions } from 'graphcool-binding'
46
+ import { GraphQLResolveInfo } from 'graphql'
123
47
124
48
const typeDefs = \`
125
49
${ schema } \``
126
50
}
127
51
128
52
function renderMainMethod ( queryType : GraphQLObjectType , mutationType ?: GraphQLObjectType | null , subscriptionType ?: GraphQLObjectType | null ) {
129
- return `export class Binding extends BaseBinding {
53
+ return `export class Binding extends Graphcool {
130
54
131
- constructor({ endpoint, secret, fragmentReplacements} : BindingOptions ) {
132
- super({ typeDefs, endpoint, secret, fragmentReplacements});
55
+ constructor({ endpoint, secret, fragmentReplacements, debug }: BaseGraphcoolOptions ) {
56
+ super({ typeDefs, endpoint, secret, fragmentReplacements, debug });
133
57
}
134
58
135
59
query: Query = {
@@ -143,10 +67,10 @@ ${renderMainMethodFields('mutation', mutationType.getFields())}
143
67
}
144
68
145
69
146
- function renderMainMethodFields ( operation : string , fields : GraphQLFieldMap < any , any > ) : string {
70
+ export function renderMainMethodFields ( operation : string , fields : GraphQLFieldMap < any , any > ) : string {
147
71
return Object . keys ( fields ) . map ( f => {
148
72
const field = fields [ f ]
149
- return ` ${ field . name } : (args, info): Promise<${ renderFieldType ( field . type ) } ${ ! isNonNullType ( field . type ) ? ' | null' : '' } > => super.delegate('${ operation } ', '${ field . name } ', args, info)`
73
+ return ` ${ field . name } : (args, info): Promise<${ renderFieldType ( field . type ) } ${ ! isNonNullType ( field . type ) ? ' | null' : '' } > => super.delegate('${ operation } ', '${ field . name } ', args, {}, info)`
150
74
} ) . join ( ',\n' )
151
75
}
152
76
@@ -169,7 +93,7 @@ function renderRootType(type: GraphQLObjectType): string {
169
93
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' : '' } >`
170
94
} ) . join ( '\n' )
171
95
172
- return renderInterfaceWrapper ( type . name , type . description , type . getInterfaces ( ) , fieldDefinition )
96
+ return renderTypeWrapper ( type . name , type . description , fieldDefinition )
173
97
}
174
98
175
99
function renderUnionType ( type : GraphQLUnionType ) : string {
@@ -214,15 +138,21 @@ ${mutationType ? ` mutation: ${mutationType.name}
214
138
` : '' } }`
215
139
}
216
140
217
- function renderInterfaceWrapper ( typeName : string , typeDescription : string , interfaces : GraphQLInterfaceType [ ] , fieldDefinition : string ) : string {
141
+ function renderInterfaceWrapper ( typeName : string , typeDescription : string , interfaces : GraphQLInterfaceType [ ] , fieldDefinition : string ) : string {
218
142
return `${ renderDescription ( typeDescription ) } export interface ${ typeName } ${ interfaces . length > 0 ? ` extends ${ interfaces . map ( i => i . name ) . join ( ', ' ) } ` : '' } {
219
143
${ fieldDefinition }
220
144
}`
221
145
}
222
146
147
+ function renderTypeWrapper ( typeName : string , typeDescription : string , fieldDefinition : string ) : string {
148
+ return `${ renderDescription ( typeDescription ) } export type ${ typeName } = {
149
+ ${ fieldDefinition }
150
+ }`
151
+ }
152
+
223
153
function renderDescription ( description ?: string ) {
224
154
return `${ description ? `/*
225
- ${ description }
226
- */
155
+ ${ description . split ( '\n' ) . map ( l => ` * ${ l } \n` ) }
156
+ */
227
157
` : '' } `
228
158
}
0 commit comments