Skip to content

Commit

Permalink
fix(apollo): Update resolveReference location
Browse files Browse the repository at this point in the history
The location of `resolveReference` was updated in the
`@apollo/subgraph` library to live on a type's `extensions` object.

Reference:
apollographql/federation#1746
apollographql/federation#1747
  • Loading branch information
trevor-scheer committed Jun 14, 2022
1 parent 5a5efe5 commit 17d653f
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 13 deletions.
57 changes: 49 additions & 8 deletions packages/graphql/lib/utils/transform-schema.util.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// This file is copied from `apollo-tooling`. The only difference is that it has a hack to not remove federation specific properties.
// The changed lines are 31-40 and 85-87 and the original file can be found here:
// https://github.com/apollographql/apollo-tooling/blob/master/packages/apollo-graphql/src/schema/transformSchema.ts
// This file is copied from `apollographql/federation`. The only difference is
// that it has a hack to not remove federation specific properties.
// https://github.com/apollographql/federation/blob/main/subgraph-js/src/schema-helper/transformSchema.ts

import {
GraphQLDirective,
Expand All @@ -15,6 +15,7 @@ import {
GraphQLNonNull,
GraphQLObjectType,
GraphQLOutputType,
GraphQLResolveInfo,
GraphQLSchema,
GraphQLType,
GraphQLUnionType,
Expand All @@ -27,13 +28,33 @@ import {
isUnionType,
} from 'graphql';

type GraphQLReferenceResolver<TContext> = (
reference: object,
context: TContext,
info: GraphQLResolveInfo,
) => any;

interface ApolloSubgraphExtensions<TContext> {
resolveReference?: GraphQLReferenceResolver<TContext>;
}

declare module 'graphql/type/definition' {
interface GraphQLObjectType {
resolveReference?: any;
interface GraphQLObjectTypeExtensions<_TSource = any, _TContext = any> {
apollo?: {
subgraph?: ApolloSubgraphExtensions<_TContext>;
};
}

interface GraphQLInterfaceTypeExtensions<_TSource = any, _TContext = any> {
apollo?: {
subgraph?: ApolloSubgraphExtensions<_TContext>;
};
}

interface GraphQLObjectTypeConfig<TSource, TContext> {
resolveReference?: any;
interface GraphQLUnionTypeExtensions<_TSource = any, _TContext = any> {
apollo?: {
subgraph?: ApolloSubgraphExtensions<_TContext>;
};
}
}

Expand Down Expand Up @@ -81,7 +102,27 @@ export function transformSchema(
fields: () => replaceFields(config.fields),
});

if (type.resolveReference) {
if (type.extensions.apollo?.subgraph?.resolveReference) {
objectType.extensions = {
...objectType.extensions,
apollo: {
...objectType.extensions.apollo,
subgraph: {
...objectType.extensions.apollo.subgraph,
resolveReference:
type.extensions.apollo.subgraph.resolveReference,
},
},
};
/**
* Backcompat for old versions of @apollo/subgraph which didn't use
* `extensions` This can be removed when support for @apollo/subgraph <
* 0.4.2 is dropped Reference:
* https://github.com/apollographql/federation/pull/1747
*/
// @ts-expect-error (explanation above)
} else if (type.resolveReference) {
// @ts-expect-error (explanation above)
objectType.resolveReference = type.resolveReference;
}

Expand Down
18 changes: 13 additions & 5 deletions packages/mercurius/lib/utils/transform-schema.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,19 @@ export function transformFederatedSchema(schema: GraphQLSchema) {
throw new MER_ERR_GQL_GATEWAY_INVALID_SCHEMA(__typename);
}

const resolveReference = (type as any).resolveReference
? (type as any).resolveReference
: function defaultResolveReference() {
return reference;
};
const resolveReference =
type.extensions?.apollo?.subgraph?.resolveReference ??
/**
* Backcompat for old versions of @apollo/subgraph which didn't use
* `extensions` This can be removed when support for
* @apollo/subgraph < 0.4.2 is dropped Reference:
* https://github.com/apollographql/federation/pull/1747
*/
// @ts-expect-error (explanation above)
type.resolveReference ??
function defaultResolveReference() {
return reference;
};

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

Expand Down

0 comments on commit 17d653f

Please sign in to comment.