Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(apollo): Update resolveReference location #2245

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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