1
- // This file is copied from `apollo-tooling `. The only difference is that it has a hack to not remove federation specific properties.
2
- // The changed lines are 31-40 and 85-87 and the original file can be found here:
3
- // https://github.com/apollographql/apollo-tooling /blob/master/packages/apollo-graphql /src/schema/transformSchema.ts
1
+ // This file is copied from `apollographql/federation `. The only difference is
2
+ // that it has a hack to not remove federation specific properties.
3
+ // https://github.com/apollographql/federation /blob/main/subgraph-js /src/schema-helper /transformSchema.ts
4
4
5
5
import {
6
6
GraphQLDirective ,
@@ -15,6 +15,7 @@ import {
15
15
GraphQLNonNull ,
16
16
GraphQLObjectType ,
17
17
GraphQLOutputType ,
18
+ GraphQLResolveInfo ,
18
19
GraphQLSchema ,
19
20
GraphQLType ,
20
21
GraphQLUnionType ,
@@ -27,13 +28,33 @@ import {
27
28
isUnionType ,
28
29
} from 'graphql' ;
29
30
31
+ type GraphQLReferenceResolver < TContext > = (
32
+ reference : object ,
33
+ context : TContext ,
34
+ info : GraphQLResolveInfo ,
35
+ ) => any ;
36
+
37
+ interface ApolloSubgraphExtensions < TContext > {
38
+ resolveReference ?: GraphQLReferenceResolver < TContext > ;
39
+ }
40
+
30
41
declare module 'graphql/type/definition' {
31
- interface GraphQLObjectType {
32
- resolveReference ?: any ;
42
+ interface GraphQLObjectTypeExtensions < _TSource = any , _TContext = any > {
43
+ apollo ?: {
44
+ subgraph ?: ApolloSubgraphExtensions < _TContext > ;
45
+ } ;
46
+ }
47
+
48
+ interface GraphQLInterfaceTypeExtensions < _TSource = any , _TContext = any > {
49
+ apollo ?: {
50
+ subgraph ?: ApolloSubgraphExtensions < _TContext > ;
51
+ } ;
33
52
}
34
53
35
- interface GraphQLObjectTypeConfig < TSource , TContext > {
36
- resolveReference ?: any ;
54
+ interface GraphQLUnionTypeExtensions < _TSource = any , _TContext = any > {
55
+ apollo ?: {
56
+ subgraph ?: ApolloSubgraphExtensions < _TContext > ;
57
+ } ;
37
58
}
38
59
}
39
60
@@ -81,7 +102,27 @@ export function transformSchema(
81
102
fields : ( ) => replaceFields ( config . fields ) ,
82
103
} ) ;
83
104
84
- if ( type . resolveReference ) {
105
+ if ( type . extensions ?. apollo ?. subgraph ?. resolveReference ) {
106
+ objectType . extensions = {
107
+ ...objectType . extensions ,
108
+ apollo : {
109
+ ...objectType . extensions . apollo ,
110
+ subgraph : {
111
+ ...objectType . extensions . apollo . subgraph ,
112
+ resolveReference :
113
+ type . extensions . apollo . subgraph . resolveReference ,
114
+ } ,
115
+ } ,
116
+ } ;
117
+ /**
118
+ * Backcompat for old versions of @apollo/subgraph which didn't use
119
+ * `extensions` This can be removed when support for @apollo/subgraph <
120
+ * 0.4.2 is dropped Reference:
121
+ * https://github.com/apollographql/federation/pull/1747
122
+ */
123
+ // @ts -expect-error (explanation above)
124
+ } else if ( type . resolveReference ) {
125
+ // @ts -expect-error (explanation above)
85
126
objectType . resolveReference = type . resolveReference ;
86
127
}
87
128
0 commit comments