Skip to content

Commit 56b764e

Browse files
Merge pull request #2245 from trevor-scheer/trevor/subgraph-resolve-reference
fix(apollo): Update `resolveReference` location
2 parents 01b14bf + da0eaa4 commit 56b764e

File tree

2 files changed

+62
-13
lines changed

2 files changed

+62
-13
lines changed

packages/graphql/lib/utils/transform-schema.util.ts

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
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
44

55
import {
66
GraphQLDirective,
@@ -15,6 +15,7 @@ import {
1515
GraphQLNonNull,
1616
GraphQLObjectType,
1717
GraphQLOutputType,
18+
GraphQLResolveInfo,
1819
GraphQLSchema,
1920
GraphQLType,
2021
GraphQLUnionType,
@@ -27,13 +28,33 @@ import {
2728
isUnionType,
2829
} from 'graphql';
2930

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+
3041
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+
};
3352
}
3453

35-
interface GraphQLObjectTypeConfig<TSource, TContext> {
36-
resolveReference?: any;
54+
interface GraphQLUnionTypeExtensions<_TSource = any, _TContext = any> {
55+
apollo?: {
56+
subgraph?: ApolloSubgraphExtensions<_TContext>;
57+
};
3758
}
3859
}
3960

@@ -81,7 +102,27 @@ export function transformSchema(
81102
fields: () => replaceFields(config.fields),
82103
});
83104

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)
85126
objectType.resolveReference = type.resolveReference;
86127
}
87128

packages/mercurius/lib/utils/transform-schema.util.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,19 @@ export function transformFederatedSchema(schema: GraphQLSchema) {
145145
throw new MER_ERR_GQL_GATEWAY_INVALID_SCHEMA(__typename);
146146
}
147147

148-
const resolveReference = (type as any).resolveReference
149-
? (type as any).resolveReference
150-
: function defaultResolveReference() {
151-
return reference;
152-
};
148+
const resolveReference =
149+
type.extensions?.apollo?.subgraph?.resolveReference ??
150+
/**
151+
* Backcompat for old versions of @apollo/subgraph which didn't use
152+
* `extensions` This can be removed when support for
153+
* @apollo/subgraph < 0.4.2 is dropped Reference:
154+
* https://github.com/apollographql/federation/pull/1747
155+
*/
156+
// @ts-expect-error (explanation above)
157+
type.resolveReference ??
158+
function defaultResolveReference() {
159+
return reference;
160+
};
153161

154162
const result = resolveReference(reference, {}, context, info);
155163

0 commit comments

Comments
 (0)