Skip to content

Commit

Permalink
feat: allow union and ref field updates for reverse fields (#22)
Browse files Browse the repository at this point in the history
Co-authored-by: Jonas Faber <[email protected]>
  • Loading branch information
joelpierre and flexzuu authored Apr 1, 2022
1 parent 2d704e9 commit 8660f94
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 16 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"size-limit": "4.10.1",
"tsdx": "0.14.1",
"tslib": "2.1.0",
"typescript": "4.2.3"
"typescript": "4.5.5"
},
"dependencies": {
"@types/nanoid": "2.1.0",
Expand Down
2 changes: 1 addition & 1 deletion src/field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
GraphQLBatchMigrationUpdateUnionFieldInput,
} from "./generated/schema";

type FieldArgs =
export type FieldArgs =
| GraphQLBatchMigrationCreateSimpleFieldInput
| GraphQLBatchMigrationUpdateSimpleFieldInput
| GraphQLBatchMigrationCreateRelationalFieldInput
Expand Down
34 changes: 24 additions & 10 deletions src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
GraphQLFieldValidationFloatRangeInput,
GraphQLFieldValidationIntRangeInput,
GraphQLFieldValidationRegExInput,
GraphQLRelationalFieldType,
GraphQLSimpleFieldType,
GraphQLSimpleFieldValidationsInput,
} from "./generated/schema";
Expand Down Expand Up @@ -156,6 +157,7 @@ interface UpdateEnumerableFieldArgs

interface CreateRemoteFieldArgs
extends Omit<GraphQLBatchMigrationCreateRemoteFieldInput, "parentApiId"> {}

interface UpdateRemoteFieldArgs
extends Omit<GraphQLBatchMigrationUpdateRemoteFieldInput, "parentApiId"> {}

Expand Down Expand Up @@ -279,13 +281,17 @@ class ModelClass implements Model, ChangeItem {
addRelationalField(passedFieldArgs: any): Model {
const fieldArgs = { ...passedFieldArgs };
fieldArgs.modelApiId = this.args.apiId;

const fieldTypeUpper = fieldArgs.type?.toUpperCase();
const fieldModelUpper = fieldArgs.model?.toUpperCase();

if (
(fieldArgs.type && fieldArgs.type === "ASSET") ||
(fieldArgs.model && fieldArgs.model === "Asset")
fieldTypeUpper === GraphQLRelationalFieldType.Asset ||
fieldModelUpper === GraphQLRelationalFieldType.Asset
) {
fieldArgs.type = "ASSET";
fieldArgs.type = GraphQLRelationalFieldType.Asset;
} else {
fieldArgs.type = "RELATION";
fieldArgs.type = GraphQLRelationalFieldType.Relation;
}

if (!fieldArgs.reverseField) {
Expand All @@ -294,6 +300,7 @@ class ModelClass implements Model, ChangeItem {
displayName: `Related ${fieldArgs.modelApiId}`,
};
}

fieldArgs.reverseField.modelApiId = fieldArgs.model;

fieldArgs.isList =
Expand All @@ -303,8 +310,8 @@ class ModelClass implements Model, ChangeItem {
fieldArgs.relationType === RelationType.ManyToOne ||
fieldArgs.relationType === RelationType.ManyToMany;

if (fieldArgs.type === "ASSET") {
// assets needs the isRequired field
if (fieldArgs.type === GraphQLRelationalFieldType.Asset) {
// Asset needs the isRequired field
if (fieldArgs.isRequired === undefined) {
fieldArgs.isRequired = false;
}
Expand Down Expand Up @@ -368,6 +375,15 @@ class ModelClass implements Model, ChangeItem {
updateRelationalField(passedFieldArgs: any): Model {
const fieldArgs = { ...passedFieldArgs };
fieldArgs.modelApiId = this.args.apiId;
fieldArgs.reverseField = passedFieldArgs?.reverseField;

if (
fieldArgs.modelApiId?.toUpperCase() ===
GraphQLRelationalFieldType.Asset &&
fieldArgs.isRequired !== undefined
) {
fieldArgs.isRequired = Boolean(fieldArgs.isRequired);
}

const field = new Field(
fieldArgs,
Expand All @@ -382,6 +398,7 @@ class ModelClass implements Model, ChangeItem {
const fieldArgs = { ...passedFieldArgs };
fieldArgs.modelApiId = this.args.apiId;
fieldArgs.reverseField = {
...passedFieldArgs?.reverseField,
modelApiIds: fieldArgs.models,
};

Expand Down Expand Up @@ -467,10 +484,7 @@ class ModelClass implements Model, ChangeItem {
return true;
}
// apiId is always a requirement, length of 1 means its apiId only.
if (Object.keys(this.args).length > 1) {
return true;
}
return false;
return Object.keys(this.args).length > 1;
}

generateChange(): MigrationChange {
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10000,10 +10000,10 @@ typedarray@^0.0.6:
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=

typescript@4.2.3:
version "4.2.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.2.3.tgz#39062d8019912d43726298f09493d598048c1ce3"
integrity sha512-qOcYwxaByStAWrBf4x0fibwZvMRG+r4cQoTjbPtUlrWjBHbmCAww1i448U0GJ+3cNNEtebDteo/cHOR3xJ4wEw==
typescript@4.5.5:
version "4.5.5"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.5.5.tgz#d8c953832d28924a9e3d37c73d729c846c5896f3"
integrity sha512-TCTIul70LyWe6IJWT8QSYeA54WQe8EjQFU4wY52Fasj5UKx88LNYKCgBEHcOMOrFF1rKGbD8v/xcNWVUq9SymA==

typescript@^3.7.3:
version "3.9.9"
Expand Down

0 comments on commit 8660f94

Please sign in to comment.