Skip to content

Commit

Permalink
Move __resolveReference resolvers on to extensions (#1746)
Browse files Browse the repository at this point in the history
The entity __resolveReference resolver is currently placed directly
on to GraphQL*Type objects. This breaks the API, as the expectation
is to put things like this on to the `extensions` field of objects
contained within a GraphQLSchema.

This moves the resolver off of the object and into the extensions
where it belongs.

Fixes: #1131
Fixes: ardatan/graphql-tools#2687
Fixes: ardatan/graphql-tools#2857
  • Loading branch information
trevor-scheer authored Apr 20, 2022
1 parent 4fbf2ff commit 4e3c161
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 11 deletions.
26 changes: 24 additions & 2 deletions gateway-js/src/schema-helper/addResolversToSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,18 @@ export function addResolversToSchema(

if (isAbstractType(type)) {
for (const [fieldName, fieldConfig] of Object.entries(fieldConfigs)) {
if (fieldName.startsWith("__")) {
if (fieldName === "__resolveReference") {
type.extensions = {
...type.extensions,
apollo: {
...type.extensions.apollo,
subgraph: {
...type.extensions.apollo?.subgraph,
resolveReference: fieldConfig,
}
},
};
} else if (fieldName.startsWith("__")) {
(type as any)[fieldName.substring(2)] = fieldConfig;
}
}
Expand Down Expand Up @@ -65,7 +76,18 @@ export function addResolversToSchema(
const fieldMap = type.getFields();

for (const [fieldName, fieldConfig] of Object.entries(fieldConfigs)) {
if (fieldName.startsWith("__")) {
if (fieldName === "__resolveReference") {
type.extensions = {
...type.extensions,
apollo: {
...type.extensions.apollo,
subgraph: {
...type.extensions.apollo?.subgraph,
resolveReference: fieldConfig,
}
},
};
} else if (fieldName.startsWith("__")) {
(type as any)[fieldName.substring(2)] = fieldConfig;
continue;
}
Expand Down
31 changes: 31 additions & 0 deletions gateway-js/src/typings/graphql.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { GraphQLResolveInfo } from 'graphql';

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

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

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

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

interface GraphQLUnionTypeExtensions<_TSource = any, _TContext = any> {
apollo?: {
subgraph?: ApolloSubgraphExtensions<_TContext>;
}
}
}
26 changes: 24 additions & 2 deletions subgraph-js/src/schema-helper/buildSchemaFromSDL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,18 @@ export function addResolversToSchema(

if (isAbstractType(type)) {
for (const [fieldName, fieldConfig] of Object.entries(fieldConfigs)) {
if (fieldName.startsWith("__")) {
if (fieldName === "__resolveReference") {
type.extensions = {
...type.extensions,
apollo: {
...type.extensions.apollo,
subgraph: {
...type.extensions.apollo?.subgraph,
resolveReference: fieldConfig,
}
},
};
} else if (fieldName.startsWith("__")) {
(type as any)[fieldName.substring(2)] = fieldConfig;
}
}
Expand Down Expand Up @@ -154,7 +165,18 @@ export function addResolversToSchema(
const fieldMap = type.getFields();

for (const [fieldName, fieldConfig] of Object.entries(fieldConfigs)) {
if (fieldName.startsWith("__")) {
if (fieldName === "__resolveReference") {
type.extensions = {
...type.extensions,
apollo: {
...type.extensions.apollo,
subgraph: {
...type.extensions.apollo?.subgraph,
resolveReference: fieldConfig,
}
},
};
} else if (fieldName.startsWith("__")) {
(type as any)[fieldName.substring(2)] = fieldConfig;
continue;
}
Expand Down
22 changes: 18 additions & 4 deletions subgraph-js/src/schemaExtensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,26 @@ type GraphQLReferenceResolver<TContext> = (
info: GraphQLResolveInfo,
) => any;

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

declare module 'graphql/type/definition' {
interface GraphQLObjectType {
resolveReference?: GraphQLReferenceResolver<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?: GraphQLReferenceResolver<TContext>;
interface GraphQLUnionTypeExtensions<_TSource = any, _TContext = any> {
apollo?: {
subgraph?: ApolloSubgraphExtensions<_TContext>;
};
}
}
6 changes: 3 additions & 3 deletions subgraph-js/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ export function entitiesResolver({
}
}

const resolveReference = type.resolveReference
? type.resolveReference
: function defaultResolveReference() {
const resolveReference =
type.extensions.apollo?.subgraph?.resolveReference ??
function defaultResolveReference() {
return reference;
};

Expand Down

0 comments on commit 4e3c161

Please sign in to comment.