Fixed Maybe for Non-Nullable types (#394)
* Fixed Maybe for Non-Nullable types * Actual fix * Change files
This commit is contained in:
Родитель
065e5eaa04
Коммит
993c91f472
|
@ -0,0 +1,7 @@
|
|||
{
|
||||
"type": "patch",
|
||||
"comment": "Fixed Maybe for Non-Nullable types",
|
||||
"packageName": "@graphitation/graphql-codegen-typescript-operations",
|
||||
"email": "jakubvejr@microsoft.com",
|
||||
"dependentChangeType": "patch"
|
||||
}
|
|
@ -17,14 +17,14 @@
|
|||
},
|
||||
"dependencies": {
|
||||
"@graphql-codegen/plugin-helpers": "^1.18.2",
|
||||
"@graphql-codegen/typescript": "^1.18.1",
|
||||
"@graphql-codegen/typescript": "^1.19.0",
|
||||
"@graphql-codegen/visitor-plugin-common": "^1.17.22",
|
||||
"auto-bind": "~4.0.0",
|
||||
"tslib": "~2.0.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@graphql-codegen/testing": "1.17.7",
|
||||
"@graphql-codegen/typescript": "^1.18.1"
|
||||
"@graphql-codegen/typescript": "^1.19.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"graphql": "^15.0.0"
|
||||
|
|
|
@ -2778,9 +2778,9 @@ describe("TypeScript Operations Plugin", () => {
|
|||
password: string;
|
||||
input?: Maybe<InputType>;
|
||||
mandatoryInput: InputType;
|
||||
testArray?: Maybe<Array<Maybe<string>> | Maybe<string>>;
|
||||
requireString: Array<Maybe<string>> | Maybe<string>;
|
||||
innerRequired: Array<string> | string;
|
||||
testArray?: Maybe<Array<Maybe<string>>>;
|
||||
requireString: Array<Maybe<string>>;
|
||||
innerRequired: Array<string>;
|
||||
}>;`,
|
||||
);
|
||||
await validate(content, config);
|
||||
|
@ -5055,9 +5055,9 @@ function test(q: GetEntityBrandDataQuery): void {
|
|||
|
||||
expect(content).toBeSimilarStringTo(`
|
||||
export type UserQueryVariables = Exact<{
|
||||
testArray?: Maybe<Array<Maybe<string>> | Maybe<string>>;
|
||||
requireString: Array<Maybe<string>> | Maybe<string>;
|
||||
innerRequired: Array<string> | string;
|
||||
testArray?: Maybe<Array<Maybe<string>>>;
|
||||
requireString: Array<Maybe<string>>;
|
||||
innerRequired: Array<string>;
|
||||
}>;`);
|
||||
await validate(content, config);
|
||||
});
|
||||
|
|
|
@ -2764,9 +2764,9 @@ describe("TypeScript Operations Plugin", () => {
|
|||
password: Scalars['String'];
|
||||
input?: Maybe<InputType>;
|
||||
mandatoryInput: InputType;
|
||||
testArray?: Maybe<Array<Maybe<Scalars['String']>> | Maybe<Scalars['String']>>;
|
||||
requireString: Array<Maybe<Scalars['String']>> | Maybe<Scalars['String']>;
|
||||
innerRequired: Array<Scalars['String']> | Scalars['String'];
|
||||
testArray?: Maybe<Array<Maybe<Scalars['String']>>>;
|
||||
requireString: Array<Maybe<Scalars['String']>>;
|
||||
innerRequired: Array<Scalars['String']>;
|
||||
}>;`,
|
||||
);
|
||||
await validate(content, config);
|
||||
|
@ -5050,9 +5050,9 @@ function test(q: GetEntityBrandDataQuery): void {
|
|||
|
||||
expect(content).toBeSimilarStringTo(`
|
||||
export type UserQueryVariables = Exact<{
|
||||
testArray?: Maybe<Array<Maybe<Scalars['String']>> | Maybe<Scalars['String']>>;
|
||||
requireString: Array<Maybe<Scalars['String']>> | Maybe<Scalars['String']>;
|
||||
innerRequired: Array<Scalars['String']> | Scalars['String'];
|
||||
testArray?: Maybe<Array<Maybe<Scalars['String']>>>;
|
||||
requireString: Array<Maybe<Scalars['String']>>;
|
||||
innerRequired: Array<Scalars['String']>;
|
||||
}>;`);
|
||||
await validate(content, config);
|
||||
});
|
||||
|
|
|
@ -5,10 +5,12 @@ import {
|
|||
NormalizedScalarsMap,
|
||||
ParsedEnumValuesMap,
|
||||
} from "@graphql-codegen/visitor-plugin-common";
|
||||
import { Kind, TypeNode } from "graphql";
|
||||
|
||||
const BASIC_TYPES = ["string", "number", "boolean", "any"];
|
||||
export class TypeScriptOperationVariablesToObject extends TSOperationVariablesToObject {
|
||||
public isMaybeUsed = false;
|
||||
public immutableTypes: boolean;
|
||||
|
||||
constructor(
|
||||
_scalars: NormalizedScalarsMap,
|
||||
|
@ -34,6 +36,8 @@ export class TypeScriptOperationVariablesToObject extends TSOperationVariablesTo
|
|||
_enumValues,
|
||||
_applyCoercion,
|
||||
);
|
||||
|
||||
this.immutableTypes = _immutableTypes;
|
||||
}
|
||||
|
||||
protected formatTypeString(
|
||||
|
@ -60,4 +64,35 @@ export class TypeScriptOperationVariablesToObject extends TSOperationVariablesTo
|
|||
|
||||
return super.getScalar(name);
|
||||
}
|
||||
|
||||
private _clearOptional(str: string): string {
|
||||
const rgx = new RegExp(`^${this.wrapMaybe(`(.*?)`)}$`, "i");
|
||||
const maybeWithNamespace = `${
|
||||
this._namespacedImportName ? `${this._namespacedImportName}.` : ""
|
||||
}Maybe`;
|
||||
if (str.startsWith(maybeWithNamespace) || str.startsWith("Maybe")) {
|
||||
return str.replace(rgx, "$1");
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
public 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 this.wrapMaybe(
|
||||
`${this.immutableTypes ? "ReadonlyArray" : "Array"}<${innerType}>`,
|
||||
);
|
||||
} else {
|
||||
return this.wrapMaybe(baseType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2049,7 +2049,7 @@
|
|||
auto-bind "~4.0.0"
|
||||
tslib "~2.3.0"
|
||||
|
||||
"@graphql-codegen/typescript@^1.18.1", "@graphql-codegen/typescript@^1.22.0":
|
||||
"@graphql-codegen/typescript@^1.19.0", "@graphql-codegen/typescript@^1.22.0":
|
||||
version "1.23.0"
|
||||
resolved "https://registry.yarnpkg.com/@graphql-codegen/typescript/-/typescript-1.23.0.tgz#48a5372bcbe81a442c71c1bb032c312c6586a59a"
|
||||
integrity sha512-ZfFgk5mGfuOy4kEpy+dcuvJMphigMfJ4AkiP1qWmWFufDW3Sg2yayTSNmzeFdcXMrWGgfNW2dKtuuTmbmQhS5g==
|
||||
|
|
Загрузка…
Ссылка в новой задаче