Skip to content

Commit

Permalink
fix createOrUpdate type
Browse files Browse the repository at this point in the history
  • Loading branch information
arvitaly committed Apr 27, 2017
1 parent 3a0fc12 commit afadcde
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 118 deletions.
23 changes: 16 additions & 7 deletions Model.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 17 additions & 8 deletions Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class Model {
protected createType: GraphQLInputObjectType;
protected updateType: GraphQLInputObjectType;
protected createOrUpdateType: GraphQLInputObjectType;
protected createOrUpdateUpdateType: GraphQLInputObjectType;
protected connectionType: GraphQLObjectType;
protected whereInputType: GraphQLInputObjectType;
protected whereArguments: Argument[];
Expand Down Expand Up @@ -205,20 +206,14 @@ class Model {
outputFields[uncapitalize(this.name)] = {
type: this.getBaseType(),
};
const updateFields = Object.assign({}, this.getUpdateType().getFields());
delete updateFields[idArgName];

return mutationWithClientMutationId({
name: this.name + "CreateOrUpdateMutation",
inputFields: {
create: {
type: new GraphQLNonNull(this.getCreateType()),
},
update: {
type: new GraphQLNonNull(new GraphQLInputObjectType({
name: this.name + "CreateOrUpdateMutationUpdate",
fields: updateFields,
})),
type: new GraphQLNonNull(this.getCreateOrUpdateUpdateType()),
},
},
outputFields,
Expand All @@ -243,6 +238,12 @@ class Model {
}
return this.updateType;
}
public getCreateOrUpdateUpdateType() {
if (!this.createOrUpdateUpdateType) {
this.createOrUpdateUpdateType = this.generateCreateOrUpdateUpdateType();
}
return this.createOrUpdateUpdateType;
}
public getCreateOrUpdateType() {
if (!this.createOrUpdateType) {
this.createOrUpdateType = this.generateCreateOrUpdateType();
Expand Down Expand Up @@ -594,11 +595,19 @@ class Model {
fields: () => {
return {
create: { type: new GraphQLNonNull(this.getCreateType()) },
update: { type: new GraphQLNonNull(this.getUpdateType()) },
update: { type: new GraphQLNonNull(this.getCreateOrUpdateUpdateType()) },
};
},
});
}
protected generateCreateOrUpdateUpdateType(): GraphQLInputObjectType {
const updateFields = Object.assign({}, this.getUpdateType().getFields());
delete updateFields[idArgName];
return new GraphQLInputObjectType({
name: this.name + "CreateOrUpdateUpdateType",
fields: updateFields,
});
}
protected generateWhereInputType(): GraphQLInputObjectType {
const where: GraphQLInputFieldConfigMap = {};
this.getWhereArguments().map((arg) => {
Expand Down
1 change: 1 addition & 0 deletions Resolver.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ class Resolver {
}
const globalId = toGlobalId(modelId, "" + result[model.getPrimaryKeyAttribute().realName]);
const forUpdatings = Object.assign({}, args.update);
forUpdatings.id = globalId;
return await this.updateOne(modelId, forUpdatings);
} else {
throw e;
Expand Down
86 changes: 7 additions & 79 deletions __tests__/__snapshots__/Model-spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1585,20 +1585,8 @@ Object {
},
"name": "setIsCat",
},
Object {
"field": Object {
"defaultValue": undefined,
"description": undefined,
"name": "id",
"type": Object {
"of": "ID",
"type": "GraphQLNonNull",
},
},
"name": "id",
},
],
"name": "UpdateAnimalInput",
"name": "AnimalCreateOrUpdateUpdateType",
"type": "GraphQLInputObjectType",
},
"type": "GraphQLNonNull",
Expand Down Expand Up @@ -2029,20 +2017,8 @@ Object {
},
"name": "setIsCat",
},
Object {
"field": Object {
"defaultValue": undefined,
"description": undefined,
"name": "id",
"type": Object {
"of": "ID",
"type": "GraphQLNonNull",
},
},
"name": "id",
},
],
"name": "UpdateAnimalInput",
"name": "AnimalCreateOrUpdateUpdateType",
"type": "GraphQLInputObjectType",
},
"type": "GraphQLNonNull",
Expand Down Expand Up @@ -2497,20 +2473,8 @@ Object {
},
"name": "setIsCat",
},
Object {
"field": Object {
"defaultValue": undefined,
"description": undefined,
"name": "id",
"type": Object {
"of": "ID",
"type": "GraphQLNonNull",
},
},
"name": "id",
},
],
"name": "UpdateAnimalInput",
"name": "AnimalCreateOrUpdateUpdateType",
"type": "GraphQLInputObjectType",
},
"type": "GraphQLNonNull",
Expand Down Expand Up @@ -2554,20 +2518,8 @@ Object {
},
"name": "setPets",
},
Object {
"field": Object {
"defaultValue": undefined,
"description": undefined,
"name": "id",
"type": Object {
"of": "ID",
"type": "GraphQLNonNull",
},
},
"name": "id",
},
],
"name": "UpdateuserInput",
"name": "userCreateOrUpdateUpdateType",
"type": "GraphQLInputObjectType",
},
"type": "GraphQLNonNull",
Expand Down Expand Up @@ -2948,20 +2900,8 @@ Object {
},
"name": "setIsCat",
},
Object {
"field": Object {
"defaultValue": undefined,
"description": undefined,
"name": "id",
"type": Object {
"of": "ID",
"type": "GraphQLNonNull",
},
},
"name": "id",
},
],
"name": "UpdateAnimalInput",
"name": "AnimalCreateOrUpdateUpdateType",
"type": "GraphQLInputObjectType",
},
"type": "GraphQLNonNull",
Expand Down Expand Up @@ -3378,20 +3318,8 @@ Object {
},
"name": "setIsCat",
},
Object {
"field": Object {
"defaultValue": undefined,
"description": undefined,
"name": "id",
"type": Object {
"of": "ID",
"type": "GraphQLNonNull",
},
},
"name": "id",
},
],
"name": "UpdateAnimalInput",
"name": "AnimalCreateOrUpdateUpdateType",
"type": "GraphQLInputObjectType",
},
"type": "GraphQLNonNull",
Expand Down Expand Up @@ -3912,7 +3840,7 @@ Object {
"name": "setIsCat",
},
],
"name": "AnimalCreateOrUpdateMutationUpdate",
"name": "AnimalCreateOrUpdateUpdateType",
"type": "GraphQLInputObjectType",
},
"type": "GraphQLNonNull",
Expand Down
4 changes: 2 additions & 2 deletions __tests__/__snapshots__/Schema-spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ GraphQLSchema {
"Model1CreateMutationPayload": "Model1CreateMutationPayload",
"Model1CreateOrUpdateMutationInput": "Model1CreateOrUpdateMutationInput",
"Model1CreateOrUpdateMutationPayload": "Model1CreateOrUpdateMutationPayload",
"Model1CreateOrUpdateMutationUpdate": "Model1CreateOrUpdateMutationUpdate",
"Model1CreateOrUpdateUpdateType": "Model1CreateOrUpdateUpdateType",
"Model1DeleteMutationInput": "Model1DeleteMutationInput",
"Model1DeleteMutationPayload": "Model1DeleteMutationPayload",
"Model1Edge": "Model1Edge",
Expand All @@ -91,7 +91,7 @@ GraphQLSchema {
"Model2CreateMutationPayload": "Model2CreateMutationPayload",
"Model2CreateOrUpdateMutationInput": "Model2CreateOrUpdateMutationInput",
"Model2CreateOrUpdateMutationPayload": "Model2CreateOrUpdateMutationPayload",
"Model2CreateOrUpdateMutationUpdate": "Model2CreateOrUpdateMutationUpdate",
"Model2CreateOrUpdateUpdateType": "Model2CreateOrUpdateUpdateType",
"Model2DeleteMutationInput": "Model2DeleteMutationInput",
"Model2DeleteMutationPayload": "Model2DeleteMutationPayload",
"Model2Edge": "Model2Edge",
Expand Down
11 changes: 2 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,7 @@
"@types/jest": "^19.2.2",
"coveralls": "^2.13.0",
"jest": "^19.0.2",
"tslint": "^5.0.0",
"typescript": "^2.2.2"
},
"jest": {
"automock": false,
"unmockedModulePathPatterns": [
"graphql",
"graphql-relay"
]
"tslint": "^5.1.0",
"typescript": "^2.3.1"
}
}
22 changes: 9 additions & 13 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1413,18 +1413,14 @@ mime-types@^2.1.12, mime-types@~2.1.7:
dependencies:
brace-expansion "^1.0.0"

minimist@0.0.8:
minimist@0.0.8, minimist@~0.0.1:
version "0.0.8"
resolved "https://registry.yarnpkg.com/minimist/-/minimist-0.0.8.tgz#857fcabfc3397d2625b8228262e86aa7a011b05d"

minimist@1.2.0, minimist@^1.1.1:
version "1.2.0"
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.0.tgz#a35008b20f41383eec1fb914f4cd5df79a264284"

minimist@~0.0.1:
version "0.0.10"
resolved "https://registry.yarnpkg.com/minimist/-/minimist-0.0.10.tgz#de3f98543dbf96082be48ad1a0c7cda836301dcf"

mkdirp@^0.5.1:
version "0.5.1"
resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-0.5.1.tgz#30057438eac6cf7f8c4767f38648d6697d75c903"
Expand Down Expand Up @@ -1910,9 +1906,9 @@ trim-right@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/trim-right/-/trim-right-1.0.1.tgz#cb2e1203067e0c8de1f614094b9fe45704ea6003"

tslint@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/tslint/-/tslint-5.0.0.tgz#ad3b7952f8a9b21079248bee01c2eaf92167e185"
tslint@^5.1.0:
version "5.1.0"
resolved "https://registry.yarnpkg.com/tslint/-/tslint-5.1.0.tgz#51a47baeeb58956fcd617bd2cf00e2ef0eea2ed9"
dependencies:
babel-code-frame "^6.22.0"
colors "^1.1.2"
Expand All @@ -1925,8 +1921,8 @@ tslint@^5.0.0:
tsutils "^1.4.0"

tsutils@^1.4.0:
version "1.6.0"
resolved "https://registry.yarnpkg.com/tsutils/-/tsutils-1.6.0.tgz#1fd7fac2a61369ed99cd3997f0fbb437128850f2"
version "1.8.0"
resolved "https://registry.yarnpkg.com/tsutils/-/tsutils-1.8.0.tgz#bf8118ed8e80cd5c9fc7d75728c7963d44ed2f52"

tunnel-agent@~0.4.1:
version "0.4.3"
Expand All @@ -1942,9 +1938,9 @@ type-check@~0.3.2:
dependencies:
prelude-ls "~1.1.2"

typescript@^2.2.2:
version "2.2.2"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.2.2.tgz#606022508479b55ffa368b58fee963a03dfd7b0c"
typescript@^2.3.1:
version "2.3.1"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.3.1.tgz#e3361fb395c6c3f9c69faeeabc9503f8bdecaea1"

uglify-js@^2.6:
version "2.8.22"
Expand Down

0 comments on commit afadcde

Please sign in to comment.