6
6
import { Plugin } from "graphile-build" ;
7
7
import printFederatedSchema from "./printFederatedSchema" ;
8
8
import { ObjectTypeDefinition , Directive , StringValue } from "./AST" ;
9
- import { PgAttribute , PgClass } from "graphile-build-pg" ;
9
+ import { PgAttribute , QueryBuilder } from "graphile-build-pg" ;
10
10
11
11
/**
12
12
* This plugin installs the schema outlined in the Apollo Federation spec, and
@@ -27,6 +27,9 @@ const SchemaExtensionPlugin = makeExtendSchemaPlugin(build => {
27
27
inflection,
28
28
nodeIdFieldName,
29
29
pgSql : sql ,
30
+ parseResolveInfo,
31
+ pgQueryFromResolveData : queryFromResolveData ,
32
+ pgPrepareAndRun,
30
33
} = build ;
31
34
// Cache
32
35
let Query : any ;
@@ -84,6 +87,7 @@ const SchemaExtensionPlugin = makeExtendSchemaPlugin(build => {
84
87
resolvers : {
85
88
Query : {
86
89
_entities ( data , { representations } , context , resolveInfo ) {
90
+ const { pgClient } = context ;
87
91
const {
88
92
graphile : { fieldContext } ,
89
93
} = resolveInfo ;
@@ -104,16 +108,14 @@ const SchemaExtensionPlugin = makeExtendSchemaPlugin(build => {
104
108
"Failed to interpret representation, invalid nodeId"
105
109
) ;
106
110
}
107
- const x = resolveNode (
111
+ return resolveNode (
108
112
nodeId ,
109
113
build ,
110
114
fieldContext ,
111
115
data ,
112
116
context ,
113
117
resolveInfo
114
118
) ;
115
-
116
- return x ;
117
119
} else {
118
120
const type = getTypeByName ( __typename ) ;
119
121
const { pgIntrospection : table } = scopeByType . get ( type ) ;
@@ -135,18 +137,32 @@ const SchemaExtensionPlugin = makeExtendSchemaPlugin(build => {
135
137
") and ("
136
138
) } )`;
137
139
138
- const rows = await resolveInfo . graphile . selectGraphQLResultFromTable (
139
- sql . identifier ( table . namespace , table . name ) ,
140
- ( _alias , queryBuilder ) => {
140
+ const resolveData = fieldContext . getDataFromParsedResolveInfoFragment (
141
+ parseResolveInfo ( resolveInfo ) ,
142
+ type
143
+ ) ;
144
+
145
+ const query = queryFromResolveData (
146
+ sql . identifier ( table . namespace . name , table . name ) ,
147
+ undefined ,
148
+ resolveData ,
149
+ {
150
+ useAsterisk : false , // Because it's only a single relation, no need
151
+ } ,
152
+ ( queryBuilder : QueryBuilder ) => {
141
153
queryBuilder . where ( whereClause ) ;
142
- }
154
+ } ,
155
+ context ,
156
+ resolveInfo . rootValue
143
157
) ;
144
158
145
- if ( rows . count !== 1 ) {
146
- throw new Error ( "Failed to interpret representation" ) ;
147
- }
159
+ const { text, values } = sql . compile ( query ) ;
160
+
161
+ const {
162
+ rows : [ row ] ,
163
+ } = await pgPrepareAndRun ( pgClient , text , values ) ;
148
164
149
- return rows [ 0 ] ;
165
+ return { [ $$nodeType ] : __typename , ... row } ;
150
166
}
151
167
} ) ;
152
168
} ,
@@ -283,8 +299,6 @@ const AddKeyPlugin: Plugin = builder => {
283
299
}
284
300
const { federationEntityTypes } = build ;
285
301
286
- console . log ( federationEntityTypes . map ( ( type : any ) => type . name ) ) ;
287
-
288
302
// Add our types to the entity types
289
303
return [ ...types , ...federationEntityTypes ] ;
290
304
} ) ;
0 commit comments