-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Generate input types and output enums into target file #10527
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
base: master-next
Are you sure you want to change the base?
Generate input types and output enums into target file #10527
Conversation
|
| ); | ||
| this._declarationBlockConfig = { | ||
| ignoreExport: this.config.noExport, | ||
| enumNameValueSeparator: ' =', |
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.
I experimented with different enumType, this value appears to be required for other enumTypes to work.
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.
Yes you are right! I've got a similar fix in my follow-up Enum PR here: https://github.com/dotansimha/graphql-code-generator/pull/10525/files#diff-d0b5088bd638fefba8ba49f14b488769f115ef1daff777afbe718a4c5ecdf111R172
| const operationsResult = oldVisit(allAst, { leave: visitor }); | ||
|
|
||
| let operationsContent = operationsResult.definitions.join('\n'); | ||
| const operationsDefinitions = operationsResult.definitions; |
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.
I rearranged/simplified the code here - please check.
| operationsContent = operationsResult.definitions.concat(exportConsts).join('\n'); | ||
| } | ||
|
|
||
| if (config.globalNamespace) { |
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.
I moved this block to the end - to include all the generated code.
| `); | ||
| }); | ||
|
|
||
| it('try different ways to generate enums', async () => { |
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.
Oops sorry! I should have mentioned I'm working on the tests here: #10525
Since similar tests have been moved to standalone.enum.spec.ts, shall we remove the duplicates in this PR?
|
Sorry! I caused some conflicts in this PR after merging #10525 🙏 |
| private getInputObjectDeclarationBlock(node: InputObjectTypeDefinitionNode): DeclarationBlock { | ||
| return new DeclarationBlock(this._declarationBlockConfig) | ||
| .export() | ||
| .asKind('type') | ||
| .withName(this.convertName(node)) | ||
| .withComment(node.description?.value) | ||
| .withBlock((node.fields || []).join('\n')); | ||
| } | ||
|
|
||
| private getInputObjectOneOfDeclarationBlock(node: InputObjectTypeDefinitionNode): DeclarationBlock { | ||
| const declarationKind = (node.fields?.length || 0) === 1 ? 'type' : 'type'; | ||
| return new DeclarationBlock(this._declarationBlockConfig) | ||
| .export() | ||
| .asKind(declarationKind) | ||
| .withName(this.convertName(node)) | ||
| .withComment(node.description?.value) | ||
| .withContent(`\n` + (node.fields || []).join('\n |')); | ||
| } | ||
|
|
||
| private isValidVisit(ancestors: any): boolean { | ||
| const currentVisitContext = this.getVisitorKindContextFromAncestors(ancestors); | ||
| const isVisitingInputType = currentVisitContext.includes(Kind.INPUT_OBJECT_TYPE_DEFINITION); | ||
| const isVisitingEnumType = currentVisitContext.includes(Kind.ENUM_TYPE_DEFINITION); | ||
|
|
||
| return isVisitingInputType || isVisitingEnumType; | ||
| } |
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.
With utility functions that can be shared between typescript-operations and base-types-visitor like these, I wonder if we should abstract them into visitor-plugin-common now or in a following PR? 🤔
Having them in visitor-plugin-common helps ensure they'd behave the same all the time. I've done the same for Enum utilities here : https://github.com/dotansimha/graphql-code-generator/pull/10525/files#r2585220345
| const typeInfo = new TypeInfo(schema); | ||
| visit( | ||
| documentNode, | ||
| visitWithTypeInfo(typeInfo, { |
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.
Huh, this is my first time learning about this. Could you help me with the TLDR; of TypeInfo and how it helps us here?
Description
Generates input types into target files.
Generates output enums into target files.
Related # #10496
Type of change
Please delete options that are not relevant.
How Has This Been Tested?