-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
8 changed files
with
212 additions
and
104 deletions.
There are no files selected for viewing
This file contains 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,42 @@ | ||
import { OperationVariablesToObject, ScalarsMap, ConvertNameFn } from 'graphql-codegen-visitor-plugin-common'; | ||
import { TypeNode, Kind } from 'graphql'; | ||
|
||
export class FlowOperationVariablesToObject extends OperationVariablesToObject { | ||
private clearOptional(str: string): string { | ||
if (str.startsWith('?')) { | ||
return str.replace(/^\?(.*?)$/i, '$1'); | ||
} | ||
|
||
return str; | ||
} | ||
|
||
protected wrapAstTypeWithModifiers(baseType: string, typeNode: TypeNode): string { | ||
if (typeNode.kind === Kind.NON_NULL_TYPE) { | ||
const type = this.wrapAstTypeWithModifiers(baseType, typeNode.type); | ||
|
||
return this.clearOptional(type); | ||
} else if (typeNode.kind === Kind.LIST_TYPE) { | ||
const innerType = this.wrapAstTypeWithModifiers(baseType, typeNode.type); | ||
|
||
return `?Array<${innerType}>`; | ||
} else { | ||
return `?${baseType}`; | ||
} | ||
} | ||
|
||
protected formatFieldString(fieldName: string, isNonNullType: boolean, hasDefaultValue: boolean): string { | ||
if (hasDefaultValue || isNonNullType) { | ||
return fieldName; | ||
} else { | ||
return `${fieldName}?`; | ||
} | ||
} | ||
|
||
protected formatTypeString(fieldType: string, isNonNullType: boolean, hasDefaultValue: boolean): string { | ||
if (hasDefaultValue && !isNonNullType) { | ||
return this.clearOptional(fieldType); | ||
} | ||
|
||
return fieldType; | ||
} | ||
} |
This file contains 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
46 changes: 46 additions & 0 deletions
46
packages/plugins/typescript/src/typescript-variables-to-object.ts
This file contains 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,46 @@ | ||
import { OperationVariablesToObject, ScalarsMap, ConvertNameFn } from 'graphql-codegen-visitor-plugin-common'; | ||
import { TypeNode, Kind } from 'graphql'; | ||
|
||
export class TypeScriptOperationVariablesToObject extends OperationVariablesToObject { | ||
constructor(_scalars: ScalarsMap, _convertName: ConvertNameFn, private _avoidOptionals: boolean) { | ||
super(_scalars, _convertName); | ||
} | ||
|
||
private clearOptional(str: string): string { | ||
if (str.startsWith('Maybe')) { | ||
return str.replace(/^Maybe<(.*?)>$/i, '$1'); | ||
} | ||
|
||
return str; | ||
} | ||
|
||
protected wrapAstTypeWithModifiers(baseType: string, typeNode: TypeNode): string { | ||
if (typeNode.kind === Kind.NON_NULL_TYPE) { | ||
const type = this.wrapAstTypeWithModifiers(baseType, typeNode.type); | ||
|
||
return this.clearOptional(type); | ||
} else if (typeNode.kind === Kind.LIST_TYPE) { | ||
const innerType = this.wrapAstTypeWithModifiers(baseType, typeNode.type); | ||
|
||
return `Maybe<Array<${innerType}>>`; | ||
} else { | ||
return `Maybe<${baseType}>`; | ||
} | ||
} | ||
|
||
protected formatFieldString(fieldName: string, isNonNullType: boolean, hasDefaultValue: boolean): string { | ||
if (hasDefaultValue || isNonNullType || this._avoidOptionals) { | ||
return fieldName; | ||
} else { | ||
return `${fieldName}?`; | ||
} | ||
} | ||
|
||
protected formatTypeString(fieldType: string, isNonNullType: boolean, hasDefaultValue: boolean): string { | ||
if (hasDefaultValue) { | ||
return this.clearOptional(fieldType); | ||
} | ||
|
||
return fieldType; | ||
} | ||
} |
This file contains 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 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 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.