@@ -22,28 +22,106 @@ import { Generator } from '../types'
2222
2323export const generator : Generator = {
2424 Main : renderMainMethod ,
25- Header : ( ) => '' ,
25+ Header : renderHeader ,
2626}
2727
28+ function renderHeader ( schema : string ) : string {
29+ return `const { FragmentReplacements } = require('graphcool-binding/dist/src/extractFragmentReplacements');
30+ const { GraphcoolLink } = require('graphcool-binding/dist/src/GraphcoolLink');
31+ const { buildFragmentInfo, buildTypeLevelInfo } = require('graphcool-binding/dist/src/prepareInfo');
32+ const { GraphQLResolveInfo, GraphQLSchema } = require('graphql');
33+ const { GraphQLClient } = require('graphql-request');
34+ const { SchemaCache } = require('graphql-schema-cache');
35+ const { delegateToSchema } = require('graphql-tools');
36+ const { sign } = require('jsonwebtoken');
37+
38+ // -------------------
39+ // This should be in graphcool-binding
40+ const schemaCache = new SchemaCache()
41+
42+ class BaseBinding {
43+ remoteSchema
44+ fragmentReplacements
45+ graphqlClient
46+
47+ constructor({
48+ typeDefs,
49+ endpoint,
50+ secret,
51+ fragmentReplacements}) {
52+
53+ fragmentReplacements = fragmentReplacements || {}
54+
55+ const token = sign({}, secret)
56+ const link = new GraphcoolLink(endpoint, token)
57+
58+ this.remoteSchema = schemaCache.makeExecutableSchema({
59+ link,
60+ typeDefs,
61+ key: endpoint,
62+ })
63+
64+ this.fragmentReplacements = fragmentReplacements
65+
66+ this.graphqlClient = new GraphQLClient(endpoint, {
67+ headers: { Authorization: \`Bearer \${token}\` },
68+ })
69+ }
70+
71+ delegate(operation, prop, args, info) {
72+ if (!info) {
73+ info = buildTypeLevelInfo(prop, this.remoteSchema, operation)
74+ } else if (typeof info === 'string') {
75+ info = buildFragmentInfo(prop, this.remoteSchema, operation, info)
76+ }
77+
78+ return delegateToSchema(
79+ this.remoteSchema,
80+ this.fragmentReplacements,
81+ operation,
82+ prop,
83+ args || {},
84+ {},
85+ info,
86+ )
87+ }
88+
89+ async request(
90+ query,
91+ variables
92+ ) {
93+ return this.graphqlClient.request(query, variables)
94+ }
95+ }
96+ // -------------------
97+
98+ const typeDefs = \`
99+ ${ schema } \``
100+ }
101+
102+
28103function renderMainMethod ( queryType : GraphQLObjectType , mutationType ?: GraphQLObjectType | null , subscriptionType ?: GraphQLObjectType | null ) {
29- return `exports.binding = {
30- query: {
31- ${ renderMainMethodFields ( queryType . getFields ( ) ) }
32- }${ mutationType ? `,
33- mutation: {
34- ${ renderMainMethodFields ( mutationType . getFields ( ) ) }
35- }` : '' } ${ subscriptionType ? `,
36- subscription: {
37- ${ renderMainMethodFields ( subscriptionType . getFields ( ) ) }
104+ return `export class Binding extends BaseBinding {
105+
106+ constructor({ endpoint, secret, fragmentReplacements}) {
107+ super({ typeDefs, endpoint, secret, fragmentReplacements});
108+ }
109+
110+ query = {
111+ ${ renderMainMethodFields ( 'query' , queryType . getFields ( ) ) }
112+ }${ mutationType ? `
113+
114+ mutation = {
115+ ${ renderMainMethodFields ( 'mutation' , mutationType . getFields ( ) ) }
38116 }` : '' }
39117}`
40118}
41119
42- function renderMainMethodFields ( fields : GraphQLFieldMap < any , any > ) : string {
120+ function renderMainMethodFields ( operation : string , fields : GraphQLFieldMap < any , any > ) : string {
43121 return Object . keys ( fields ) . map ( f => {
44122 const field = fields [ f ]
45123 return ` ${ field . name } (args, info) {
46- return /* TODO: Get actual implementation here from graphql-binding */
124+ return super.delegate(' ${ operation } ', ' ${ field . name } ', args, info)
47125 }`
48126 } ) . join ( ',\n' )
49127}
0 commit comments