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

Move __resolveReference resolvers on to extensions (version-0.x) #1747

Merged
merged 2 commits into from
Apr 20, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 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,20 @@ 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: {
// extensions is nullable in graphql@15 so the conditional chain is
// necessary for back compat
...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 +78,20 @@ 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: {
// extensions is nullable in graphql@15 so the conditional chain is
// necessary for back compat
...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>;
}
}
}
30 changes: 28 additions & 2 deletions subgraph-js/src/schema-helper/buildSchemaFromSDL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,20 @@ 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: {
// extensions is nullable in graphql@15 so the conditional chain is
// necessary for back compat
...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 @@ -153,7 +166,20 @@ 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: {
// extensions is nullable in graphql@15 so the conditional chain is
// necessary for back compat
...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>;
};
}
}
10 changes: 5 additions & 5 deletions subgraph-js/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ export const entitiesField: GraphQLFieldConfig<any, any> = {
}
}

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

// FIXME somehow get this to show up special in Studio traces?
const result = resolveReference(reference, context, info);
Expand Down