@@ -42,94 +42,18 @@ const scalarMapping = {
4242}
4343
4444function 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'
12347
12448const typeDefs = \`
12549${ schema } \``
12650}
12751
12852function renderMainMethod ( queryType : GraphQLObjectType , mutationType ?: GraphQLObjectType | null , subscriptionType ?: GraphQLObjectType | null ) {
129- return `export class Binding extends BaseBinding {
53+ return `export class Binding extends Graphcool {
13054
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 });
13357 }
13458
13559 query: Query = {
@@ -143,10 +67,10 @@ ${renderMainMethodFields('mutation', mutationType.getFields())}
14367}
14468
14569
146- function renderMainMethodFields ( operation : string , fields : GraphQLFieldMap < any , any > ) : string {
70+ export function renderMainMethodFields ( operation : string , fields : GraphQLFieldMap < any , any > ) : string {
14771 return Object . keys ( fields ) . map ( f => {
14872 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)`
15074 } ) . join ( ',\n' )
15175}
15276
@@ -169,7 +93,7 @@ function renderRootType(type: GraphQLObjectType): string {
16993 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' : '' } >`
17094 } ) . join ( '\n' )
17195
172- return renderInterfaceWrapper ( type . name , type . description , type . getInterfaces ( ) , fieldDefinition )
96+ return renderTypeWrapper ( type . name , type . description , fieldDefinition )
17397}
17498
17599function renderUnionType ( type : GraphQLUnionType ) : string {
@@ -214,15 +138,21 @@ ${mutationType ? ` mutation: ${mutationType.name}
214138` : '' } }`
215139}
216140
217- function renderInterfaceWrapper ( typeName : string , typeDescription : string , interfaces : GraphQLInterfaceType [ ] , fieldDefinition : string ) : string {
141+ function renderInterfaceWrapper ( typeName : string , typeDescription : string , interfaces : GraphQLInterfaceType [ ] , fieldDefinition : string ) : string {
218142 return `${ renderDescription ( typeDescription ) } export interface ${ typeName } ${ interfaces . length > 0 ? ` extends ${ interfaces . map ( i => i . name ) . join ( ', ' ) } ` : '' } {
219143${ fieldDefinition }
220144}`
221145}
222146
147+ function renderTypeWrapper ( typeName : string , typeDescription : string , fieldDefinition : string ) : string {
148+ return `${ renderDescription ( typeDescription ) } export type ${ typeName } = {
149+ ${ fieldDefinition }
150+ }`
151+ }
152+
223153function renderDescription ( description ?: string ) {
224154 return `${ description ? `/*
225- ${ description }
226- */
155+ ${ description . split ( '\n' ) . map ( l => ` * ${ l } \n` ) }
156+ */
227157` : '' } `
228158}
0 commit comments