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

Commit 3616a8d

Browse files
authored
Refactored for new graphql-binding and graphcool-binding
1 parent 1c38e98 commit 3616a8d

File tree

8 files changed

+223
-220
lines changed

8 files changed

+223
-220
lines changed

src/generators/binding-js.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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,
19+
} from 'graphql'
20+
21+
import { Generator } from '../types'
22+
23+
export const generator: Generator = {
24+
Main: renderMainMethod,
25+
Header: renderHeader,
26+
}
27+
28+
function renderHeader(schema: string): string {
29+
return `const { Binding: BaseBinding } = require('graphql-binding')
30+
const { GraphQLResolveInfo } = require('graphql')`
31+
}
32+
33+
34+
function renderMainMethod(queryType: GraphQLObjectType, mutationType?: GraphQLObjectType | null, subscriptionType?: GraphQLObjectType | null) {
35+
return `module.exports.Binding = class Binding extends BaseBinding {
36+
37+
constructor({ schema, fragmentReplacements }) {
38+
super({ schema, fragmentReplacements });
39+
40+
var self = this
41+
this.query = {
42+
${renderMainMethodFields('query', queryType.getFields())}
43+
}${mutationType ? `
44+
45+
this.mutation = {
46+
${renderMainMethodFields('mutation', mutationType.getFields())}
47+
}`: ''}
48+
}
49+
50+
delegate(operation, field, args, context, info) {
51+
return super.delegate(operation, field, args, context, info)
52+
}
53+
}`
54+
}
55+
56+
function renderMainMethodFields(operation: string, fields: GraphQLFieldMap<any, any>): string {
57+
return Object.keys(fields).map(f => {
58+
const field = fields[f]
59+
return ` ${field.name}(args, info) {
60+
return self.delegate('${operation}', '${field.name}', args, {}, info)
61+
}`
62+
}).join(',\n')
63+
}

src/generators/binding-ts.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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,
19+
} from 'graphql'
20+
21+
import { Generator } from '../types'
22+
import { generator as gcgenerator, renderMainMethodFields } from './graphcool-ts'
23+
24+
export const generator: Generator = {
25+
...gcgenerator,
26+
Main: renderMainMethod,
27+
Header: renderHeader,
28+
}
29+
30+
function renderHeader(schema: string): string {
31+
return `import { Binding as BaseBinding, BindingOptions } from 'graphql-binding'
32+
import { GraphQLResolveInfo } from 'graphql'`
33+
}
34+
35+
function renderMainMethod(queryType: GraphQLObjectType, mutationType?: GraphQLObjectType | null, subscriptionType?: GraphQLObjectType | null) {
36+
return `export class Binding extends BaseBinding {
37+
38+
constructor({ schema, fragmentReplacements }: BindingOptions) {
39+
super({ schema, fragmentReplacements });
40+
}
41+
42+
query: Query = {
43+
${renderMainMethodFields('query', queryType.getFields())}
44+
}${mutationType ? `
45+
46+
mutation: Mutation = {
47+
${renderMainMethodFields('mutation', mutationType.getFields())}
48+
}`: ''}
49+
}`
50+
}

src/generators/graphcool-js.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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,
19+
} from 'graphql'
20+
21+
import { Generator } from '../types'
22+
23+
export const generator: Generator = {
24+
Main: renderMainMethod,
25+
Header: renderHeader,
26+
}
27+
28+
function renderHeader(schema: string): string {
29+
return `const { Graphcool } = require('graphcool-binding')
30+
const { GraphQLResolveInfo } = require('graphql')
31+
32+
const typeDefs = \`
33+
${schema}\``
34+
}
35+
36+
37+
function renderMainMethod(queryType: GraphQLObjectType, mutationType?: GraphQLObjectType | null, subscriptionType?: GraphQLObjectType | null) {
38+
return `module.exports.Binding = class Binding extends Graphcool {
39+
40+
constructor({ endpoint, secret, fragmentReplacements, debug }) {
41+
super({ typeDefs, endpoint, secret, fragmentReplacements, debug });
42+
43+
var self = this
44+
this.query = {
45+
${renderMainMethodFields('query', queryType.getFields())}
46+
}${mutationType ? `
47+
48+
this.mutation = {
49+
${renderMainMethodFields('mutation', mutationType.getFields())}
50+
}`: ''}
51+
}
52+
53+
delegate(operation, field, args, context, info) {
54+
return super.delegate(operation, field, args, context, info)
55+
}
56+
}`
57+
}
58+
59+
function renderMainMethodFields(operation: string, fields: GraphQLFieldMap<any, any>): string {
60+
return Object.keys(fields).map(f => {
61+
const field = fields[f]
62+
return ` ${field.name}(args, info) {
63+
return self.delegate('${operation}', '${field.name}', args, {}, info)
64+
}`
65+
}).join(',\n')
66+
}

src/generators/typescript.ts renamed to src/generators/graphcool-ts.ts

Lines changed: 17 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -42,94 +42,18 @@ const scalarMapping = {
4242
}
4343

4444
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'
12347
12448
const typeDefs = \`
12549
${schema}\``
12650
}
12751

12852
function 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

17599
function 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+
223153
function renderDescription(description?: string) {
224154
return `${description ? `/*
225-
${description}
226-
*/
155+
${description.split('\n').map(l => ` * ${l}\n`)}
156+
*/
227157
`: ''}`
228158
}

src/generators/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { generator as graphcooljsgenerator } from './graphcool-js'
2+
import { generator as graphcooltsgenerator } from './graphcool-ts'
3+
import { generator as bindingtsgenerator } from './binding-ts'
4+
import { generator as bindingjsgenerator } from './binding-js'
5+
import { Generator } from '../types';
6+
7+
export const generators: { [key: string]: Generator} = {
8+
'graphcool-js': graphcooljsgenerator,
9+
'graphcool-ts': graphcooltsgenerator,
10+
'binding-ts': bindingtsgenerator,
11+
'binding-js': bindingjsgenerator,
12+
}

0 commit comments

Comments
 (0)