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 #1746

Merged
merged 3 commits into from
Apr 20, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 12 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,12 @@ export function addResolversToSchema(

if (isAbstractType(type)) {
for (const [fieldName, fieldConfig] of Object.entries(fieldConfigs)) {
if (fieldName.startsWith("__")) {
if (fieldName === "__resolveReference") {
type.extensions = {
...type.extensions,
resolveReference: fieldConfig,
};
} else if (fieldName.startsWith("__")) {
(type as any)[fieldName.substring(2)] = fieldConfig;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@trevor-scheer is this basically just for resolveType on abstract types and isTypeOf on object types? wonder if we should just special case them (which lets us drop the type as any).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also it looks like this entire directory (gateway-js/src/schema-helper, not to be confused with subgraph-js/src/schema-helper) is only used in tests? Can it be moved into gateway-js/src/__tests__, or maybe you can just use the version in subgraph-js directly from the tests?

}
}
Expand Down Expand Up @@ -65,7 +70,12 @@ 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,
resolveReference: fieldConfig,
};
} else if (fieldName.startsWith("__")) {
(type as any)[fieldName.substring(2)] = fieldConfig;
continue;
}
Expand Down
14 changes: 12 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,12 @@ export function addResolversToSchema(

if (isAbstractType(type)) {
for (const [fieldName, fieldConfig] of Object.entries(fieldConfigs)) {
if (fieldName.startsWith("__")) {
if (fieldName === "__resolveReference") {
type.extensions = {
...type.extensions,
resolveReference: fieldConfig,
};
} else if (fieldName.startsWith("__")) {
(type as any)[fieldName.substring(2)] = fieldConfig;
}
}
Expand Down Expand Up @@ -154,7 +159,12 @@ 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,
resolveReference: fieldConfig,
};
} else if (fieldName.startsWith("__")) {
(type as any)[fieldName.substring(2)] = fieldConfig;
continue;
}
Expand Down
8 changes: 2 additions & 6 deletions subgraph-js/src/schemaExtensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ type GraphQLReferenceResolver<TContext> = (
) => any;

declare module 'graphql/type/definition' {
interface GraphQLObjectType {
resolveReference?: GraphQLReferenceResolver<any>;
}

interface GraphQLObjectTypeConfig<TSource, TContext> {
resolveReference?: GraphQLReferenceResolver<TContext>;
interface GraphQLObjectTypeExtensions<_TSource = any, _TContext = any> {
resolveReference?: GraphQLReferenceResolver<_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?.resolveReference ??
function defaultResolveReference() {
return reference;
};

Expand Down