Skip to content

Commit

Permalink
Merge pull request #2245 from trevor-scheer/trevor/subgraph-resolve-r…
Browse files Browse the repository at this point in the history
…eference

fix(apollo): Update `resolveReference` location
  • Loading branch information
kamilmysliwiec committed Jun 15, 2022
2 parents 01b14bf + da0eaa4 commit 56b764e
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
@@ -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
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 56b764e

Please sign in to comment.