Skip to content

Commit b03b692

Browse files
committed
Revert unncessary changes to FieldDefinitionPrintFn
1 parent 133b348 commit b03b692

File tree

2 files changed

+112
-117
lines changed

2 files changed

+112
-117
lines changed

packages/plugins/other/visitor-plugin-common/src/base-resolvers-visitor.ts

Lines changed: 111 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,6 @@ type FieldDefinitionPrintFn = (
8686
parentName: string,
8787
avoidResolverOptionals: boolean
8888
) => { value: string | null; meta: { federation?: { isResolveReference: boolean } } };
89-
export type FieldDefinitionResult = [{ node: FieldDefinitionNode }, FieldDefinitionPrintFn];
90-
9189
export interface RootResolver {
9290
content: string;
9391
generatedResolverTypes: {
@@ -1521,132 +1519,128 @@ export class BaseResolversVisitor<
15211519
return `ParentType extends ${parentType} = ${parentType}`;
15221520
}
15231521

1524-
FieldDefinition(node: FieldDefinitionNode, key: string | number, parent: any): FieldDefinitionResult {
1522+
FieldDefinition(node: FieldDefinitionNode, key: string | number, parent: any): FieldDefinitionPrintFn {
15251523
const hasArguments = node.arguments && node.arguments.length > 0;
15261524
const declarationKind = 'type';
15271525

1528-
return [
1529-
{ node },
1530-
(parentName, avoidResolverOptionals) => {
1531-
const original: FieldDefinitionNode = parent[key];
1532-
const parentType = this.schema.getType(parentName);
1533-
const meta: ReturnType<FieldDefinitionPrintFn>['meta'] = {};
1534-
const typeName = node.name as unknown as string;
1535-
1536-
const fieldsToGenerate = this._federation.findFieldNodesToGenerate({ type: parentType }); // FIXME: for every field in a object, we are looping through every field of said object. This could be a bottleneck
1537-
const shouldGenerateField =
1538-
fieldsToGenerate.some(field => field.name.value === typeName) ||
1539-
this._federation.isResolveReferenceField(node);
1540-
1541-
if (!shouldGenerateField) {
1542-
return { value: null, meta };
1543-
}
1526+
return (parentName, avoidResolverOptionals) => {
1527+
const original: FieldDefinitionNode = parent[key];
1528+
const parentType = this.schema.getType(parentName);
1529+
const meta: ReturnType<FieldDefinitionPrintFn>['meta'] = {};
1530+
const typeName = node.name as unknown as string;
15441531

1545-
const contextType = this.getContextType(parentName, node);
1546-
1547-
let argsType = hasArguments
1548-
? this.convertName(
1549-
parentName +
1550-
(this.config.addUnderscoreToArgsType ? '_' : '') +
1551-
this.convertName(typeName, {
1552-
useTypesPrefix: false,
1553-
useTypesSuffix: false,
1554-
}) +
1555-
'Args',
1556-
{
1557-
useTypesPrefix: true,
1558-
},
1559-
true
1560-
)
1561-
: null;
1562-
1563-
const avoidInputsOptionals = this.config.avoidOptionals.inputValue;
1564-
1565-
if (argsType !== null) {
1566-
const argsToForceRequire = original.arguments.filter(
1567-
arg => !!arg.defaultValue || arg.type.kind === 'NonNullType'
1568-
);
1569-
1570-
if (argsToForceRequire.length > 0) {
1571-
argsType = this.applyRequireFields(argsType, argsToForceRequire);
1572-
} else if (original.arguments.length > 0 && avoidInputsOptionals !== true) {
1573-
argsType = this.applyOptionalFields(argsType, original.arguments);
1574-
}
1575-
}
1532+
const fieldsToGenerate = this._federation.findFieldNodesToGenerate({ type: parentType }); // FIXME: for every field in a object, we are looping through every field of said object. This could be a bottleneck
1533+
const shouldGenerateField =
1534+
fieldsToGenerate.some(field => field.name.value === typeName) || this._federation.isResolveReferenceField(node);
15761535

1577-
const parentTypeSignature = this._federation.transformFieldParentType({
1578-
fieldNode: original,
1579-
parentType,
1580-
parentTypeSignature: this.getParentTypeForSignature(node),
1581-
federationTypeSignature: 'FederationType',
1582-
});
1536+
if (!shouldGenerateField) {
1537+
return { value: null, meta };
1538+
}
15831539

1584-
const { mappedTypeKey, resolverType } = ((): { mappedTypeKey: string; resolverType: string } => {
1585-
const baseType = getBaseTypeNode(original.type);
1586-
const realType = baseType.name.value;
1587-
const typeToUse = this.getTypeToUse(realType);
1588-
/**
1589-
* Turns GraphQL type to TypeScript types (`mappedType`) e.g.
1590-
* - String! -> ResolversTypes['String']>
1591-
* - String -> Maybe<ResolversTypes['String']>
1592-
* - [String] -> Maybe<Array<Maybe<ResolversTypes['String']>>>
1593-
* - [String!]! -> Array<ResolversTypes['String']>
1594-
*/
1595-
const mappedType = this._variablesTransformer.wrapAstTypeWithModifiers(typeToUse, original.type);
1596-
1597-
const subscriptionType = this._schema.getSubscriptionType();
1598-
const isSubscriptionType = subscriptionType && subscriptionType.name === parentName;
1599-
1600-
if (isSubscriptionType) {
1601-
return {
1602-
mappedTypeKey: `${mappedType}, "${typeName}"`,
1603-
resolverType: 'SubscriptionResolver',
1604-
};
1605-
}
1540+
const contextType = this.getContextType(parentName, node);
1541+
1542+
let argsType = hasArguments
1543+
? this.convertName(
1544+
parentName +
1545+
(this.config.addUnderscoreToArgsType ? '_' : '') +
1546+
this.convertName(typeName, {
1547+
useTypesPrefix: false,
1548+
useTypesSuffix: false,
1549+
}) +
1550+
'Args',
1551+
{
1552+
useTypesPrefix: true,
1553+
},
1554+
true
1555+
)
1556+
: null;
1557+
1558+
const avoidInputsOptionals = this.config.avoidOptionals.inputValue;
1559+
1560+
if (argsType !== null) {
1561+
const argsToForceRequire = original.arguments.filter(
1562+
arg => !!arg.defaultValue || arg.type.kind === 'NonNullType'
1563+
);
16061564

1607-
const directiveMappings =
1608-
node.directives
1609-
?.map(directive => this._directiveResolverMappings[directive.name as any])
1610-
.filter(Boolean)
1611-
.reverse() ?? [];
1565+
if (argsToForceRequire.length > 0) {
1566+
argsType = this.applyRequireFields(argsType, argsToForceRequire);
1567+
} else if (original.arguments.length > 0 && avoidInputsOptionals !== true) {
1568+
argsType = this.applyOptionalFields(argsType, original.arguments);
1569+
}
1570+
}
16121571

1572+
const parentTypeSignature = this._federation.transformFieldParentType({
1573+
fieldNode: original,
1574+
parentType,
1575+
parentTypeSignature: this.getParentTypeForSignature(node),
1576+
federationTypeSignature: 'FederationType',
1577+
});
1578+
1579+
const { mappedTypeKey, resolverType } = ((): { mappedTypeKey: string; resolverType: string } => {
1580+
const baseType = getBaseTypeNode(original.type);
1581+
const realType = baseType.name.value;
1582+
const typeToUse = this.getTypeToUse(realType);
1583+
/**
1584+
* Turns GraphQL type to TypeScript types (`mappedType`) e.g.
1585+
* - String! -> ResolversTypes['String']>
1586+
* - String -> Maybe<ResolversTypes['String']>
1587+
* - [String] -> Maybe<Array<Maybe<ResolversTypes['String']>>>
1588+
* - [String!]! -> Array<ResolversTypes['String']>
1589+
*/
1590+
const mappedType = this._variablesTransformer.wrapAstTypeWithModifiers(typeToUse, original.type);
1591+
1592+
const subscriptionType = this._schema.getSubscriptionType();
1593+
const isSubscriptionType = subscriptionType && subscriptionType.name === parentName;
1594+
1595+
if (isSubscriptionType) {
16131596
return {
1614-
mappedTypeKey: mappedType,
1615-
resolverType: directiveMappings[0] ?? 'Resolver',
1597+
mappedTypeKey: `${mappedType}, "${typeName}"`,
1598+
resolverType: 'SubscriptionResolver',
16161599
};
1617-
})();
1618-
1619-
const signature: {
1620-
name: string;
1621-
modifier: string;
1622-
type: string;
1623-
genericTypes: string[];
1624-
} = {
1625-
name: typeName,
1626-
modifier: avoidResolverOptionals ? '' : '?',
1627-
type: resolverType,
1628-
genericTypes: [mappedTypeKey, parentTypeSignature, contextType, argsType].filter(f => f),
1629-
};
1630-
1631-
if (this._federation.isResolveReferenceField(node)) {
1632-
if (!this._federation.getMeta()[parentType.name].hasResolveReference) {
1633-
return { value: '', meta };
1634-
}
1635-
signature.type = 'ReferenceResolver';
1636-
signature.genericTypes = [mappedTypeKey, parentTypeSignature, contextType];
1637-
meta.federation = { isResolveReference: true };
16381600
}
16391601

1602+
const directiveMappings =
1603+
node.directives
1604+
?.map(directive => this._directiveResolverMappings[directive.name as any])
1605+
.filter(Boolean)
1606+
.reverse() ?? [];
1607+
16401608
return {
1641-
value: indent(
1642-
`${signature.name}${signature.modifier}: ${signature.type}<${signature.genericTypes.join(
1643-
', '
1644-
)}>${this.getPunctuation(declarationKind)}`
1645-
),
1646-
meta,
1609+
mappedTypeKey: mappedType,
1610+
resolverType: directiveMappings[0] ?? 'Resolver',
16471611
};
1648-
},
1649-
];
1612+
})();
1613+
1614+
const signature: {
1615+
name: string;
1616+
modifier: string;
1617+
type: string;
1618+
genericTypes: string[];
1619+
} = {
1620+
name: typeName,
1621+
modifier: avoidResolverOptionals ? '' : '?',
1622+
type: resolverType,
1623+
genericTypes: [mappedTypeKey, parentTypeSignature, contextType, argsType].filter(f => f),
1624+
};
1625+
1626+
if (this._federation.isResolveReferenceField(node)) {
1627+
if (!this._federation.getMeta()[parentType.name].hasResolveReference) {
1628+
return { value: '', meta };
1629+
}
1630+
signature.type = 'ReferenceResolver';
1631+
signature.genericTypes = [mappedTypeKey, parentTypeSignature, contextType];
1632+
meta.federation = { isResolveReference: true };
1633+
}
1634+
1635+
return {
1636+
value: indent(
1637+
`${signature.name}${signature.modifier}: ${signature.type}<${signature.genericTypes.join(
1638+
', '
1639+
)}>${this.getPunctuation(declarationKind)}`
1640+
),
1641+
meta,
1642+
};
1643+
};
16501644
}
16511645

16521646
private getFieldContextType(parentName: string, node: FieldDefinitionNode): string {
@@ -1746,8 +1740,8 @@ export class BaseResolversVisitor<
17461740
return false;
17471741
})();
17481742

1749-
const fieldsContent = (node.fields as unknown as FieldDefinitionResult[])
1750-
.map(([_, f]) => {
1743+
const fieldsContent = (node.fields as unknown as FieldDefinitionPrintFn[])
1744+
.map(f => {
17511745
return f(
17521746
typeName,
17531747
(rootType === 'query' && this.config.avoidOptionals.query) ||
@@ -1982,7 +1976,7 @@ export class BaseResolversVisitor<
19821976

19831977
// An Interface in Federation may have the additional __resolveReference resolver, if resolvable.
19841978
// So, we filter out the normal fields declared on the Interface and add the __resolveReference resolver.
1985-
const fields = (node.fields as unknown as FieldDefinitionResult[]).map(([_, f]) =>
1979+
const fields = (node.fields as unknown as FieldDefinitionPrintFn[]).map(f =>
19861980
f(typeName, this.config.avoidOptionals.resolvers)
19871981
);
19881982
for (const field of fields) {

packages/utils/plugins-helpers/src/federation.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ export class ApolloFederation {
291291
const fieldNodesWithProvides = fieldNodes.reduce<FieldDefinitionNode[]>((acc, fieldNode) => {
292292
if (this.hasProvides(type, fieldNode.name as unknown as string)) {
293293
acc.push(fieldNode);
294+
return acc;
294295
}
295296
return acc;
296297
}, []);

0 commit comments

Comments
 (0)