-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[typescript-resolvers][federation] Fix federation @requires type #10366
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
Open
eddeee888
wants to merge
10
commits into
federation-fixes
Choose a base branch
from
fix-federation-requires
base: federation-fixes
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+458
−232
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
80635c3
Update test setup
eddeee888 5c4ab1a
Implement @requires combination
eddeee888 b665686
Add changeset
eddeee888 1e7b946
Force release alpha
eddeee888 abd8276
Fix issue with empty array, set up tests
eddeee888 a4ba6e2
Generate FederationReferenceTypes once
eddeee888 44802c0
Update tests related to FederationReferenceTypes
eddeee888 3e749e4
Update dev-tests
eddeee888 39faa7e
Revert force release
eddeee888 61475de
Update test related to mapper
eddeee888 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
'@graphql-codegen/visitor-plugin-common': patch | ||
'@graphql-codegen/typescript-resolvers': patch | ||
'@graphql-codegen/plugin-helpers': patch | ||
--- | ||
|
||
Update @requires type |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -818,12 +818,8 @@ export class BaseResolversVisitor< | |
shouldInclude: namedType => !isEnumType(namedType), | ||
onNotMappedObjectType: ({ typeName, initialType }) => { | ||
let result = initialType; | ||
const federationReferenceTypes = this._federation.printReferenceSelectionSets({ | ||
typeName, | ||
baseFederationType: `${this.convertName('FederationTypes')}['${typeName}']`, | ||
}); | ||
if (federationReferenceTypes) { | ||
result += ` | ${federationReferenceTypes}`; | ||
if (this._federation.getMeta()[typeName]?.referenceSelectionSetsString) { | ||
result += ` | ${this.convertName('FederationReferenceTypes')}['${typeName}']`; | ||
} | ||
return result; | ||
}, | ||
|
@@ -1306,6 +1302,33 @@ export class BaseResolversVisitor< | |
).string; | ||
} | ||
|
||
public buildFederationReferenceTypes(): string { | ||
const federationMeta = this._federation.getMeta(); | ||
|
||
if (Object.keys(federationMeta).length === 0) { | ||
return ''; | ||
} | ||
|
||
const declarationKind = 'type'; | ||
return new DeclarationBlock(this._declarationBlockConfig) | ||
.export() | ||
.asKind(declarationKind) | ||
.withName(this.convertName('FederationReferenceTypes')) | ||
.withComment('Mapping of federation reference types') | ||
.withBlock( | ||
Object.entries(federationMeta) | ||
.map(([typeName, { referenceSelectionSetsString }]) => { | ||
if (!referenceSelectionSetsString) { | ||
return undefined; | ||
} | ||
|
||
return indent(`${typeName}: ${referenceSelectionSetsString}${this.getPunctuation(declarationKind)}`); | ||
}) | ||
.filter(v => v) | ||
.join('\n') | ||
).string; | ||
} | ||
|
||
public get schema(): GraphQLSchema { | ||
return this._schema; | ||
} | ||
|
@@ -1586,13 +1609,6 @@ export class BaseResolversVisitor< | |
} | ||
} | ||
|
||
const parentTypeSignature = this._federation.transformFieldParentType({ | ||
fieldNode: original, | ||
parentType, | ||
parentTypeSignature: this.getParentTypeForSignature(node), | ||
federationTypeSignature: 'FederationType', | ||
}); | ||
Comment on lines
-1589
to
-1594
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since federation reference type is extracted to FederationReferenceTypes, we don't need complex, inline reference types. |
||
|
||
const { mappedTypeKey, resolverType } = ((): { mappedTypeKey: string; resolverType: string } => { | ||
const baseType = getBaseTypeNode(original.type); | ||
const realType = baseType.name.value; | ||
|
@@ -1637,15 +1653,18 @@ export class BaseResolversVisitor< | |
name: typeName, | ||
modifier: avoidResolverOptionals ? '' : '?', | ||
type: resolverType, | ||
genericTypes: [mappedTypeKey, parentTypeSignature, contextType, argsType].filter(f => f), | ||
genericTypes: [mappedTypeKey, this.getParentTypeForSignature(node), contextType, argsType].filter(f => f), | ||
}; | ||
|
||
if (this._federation.isResolveReferenceField(node)) { | ||
if (!this._federation.getMeta()[parentType.name].hasResolveReference) { | ||
return { value: '', meta }; | ||
} | ||
const resultType = `${mappedTypeKey} | FederationReferenceType`; | ||
const referenceType = 'FederationReferenceType'; | ||
|
||
signature.type = 'ReferenceResolver'; | ||
signature.genericTypes = [mappedTypeKey, parentTypeSignature, contextType]; | ||
signature.genericTypes = [resultType, referenceType, contextType]; | ||
meta.federation = { isResolveReference: true }; | ||
} | ||
|
||
|
@@ -1788,7 +1807,7 @@ export class BaseResolversVisitor< | |
]; | ||
this._federation.addFederationTypeGenericIfApplicable({ | ||
genericTypes, | ||
federationTypesType: this.convertName('FederationTypes'), | ||
federationTypesType: this.convertName('FederationReferenceTypes'), | ||
typeName, | ||
}); | ||
|
||
|
@@ -1975,7 +1994,7 @@ export class BaseResolversVisitor< | |
]; | ||
this._federation.addFederationTypeGenericIfApplicable({ | ||
genericTypes, | ||
federationTypesType: this.convertName('FederationTypes'), | ||
federationTypesType: this.convertName('FederationReferenceTypes'), | ||
typeName, | ||
}); | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Having
buildFederationReferenceTypes
to build standaloneFederationReferenceTypes
allows us to easily refer to the complexreference
param types in different scenarios:This allows us to return reference as-is without type errors:
Similar to the mapper case, the parent of each normal resolver can receive a normal type or reference type
Again,
FederationReferenceTypes["User"]
is used:reference
type (2nd generic param)__resolveReference
must return: (1)User
value normally, or (2)FederationReferenceType