Skip to content

Commit 69490ce

Browse files
author
John Rix
committed
persist/push: Avoid sending object properties not declared in input types (vuex-orm#120)
1 parent 8ca32f7 commit 69490ce

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

src/actions/fetch.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export default class Fetch extends Action {
4040
model,
4141
params.filter as Data,
4242
true,
43+
undefined,
4344
Object.keys(params.filter)
4445
);
4546
}

src/actions/persist.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@ export default class Persist extends Action {
2121
if (id) {
2222
const model = this.getModelFromState(state!);
2323
const mutationName = Context.getInstance().adapter.getNameForPersist(model);
24-
const oldRecord = model.getRecordWithId(id)!;
24+
25+
const oldRecord = model.baseModel
26+
.query()
27+
.withAllRecursive()
28+
.where("$id", id)
29+
.first()!;
2530

2631
const mockReturnValue = model.$mockHook("persist", {
2732
id,

src/graphql/transformer.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
singularize
1111
} from "../support/utils";
1212
import { ConnectionMode } from "../adapters/adapter";
13+
import { GraphQLType, GraphQLField } from "../support/interfaces";
1314

1415
/**
1516
* This class provides methods to transform incoming data from GraphQL in to a format Vuex-ORM understands and
@@ -32,13 +33,15 @@ export default class Transformer {
3233
model: Model,
3334
data: Data,
3435
read: boolean,
36+
inputType?: GraphQLType | null,
3537
whitelist?: Array<String>,
3638
outgoingRecords?: Map<string, Array<string>>,
3739
recursiveCall?: boolean
3840
): Data {
3941
const context = Context.getInstance();
4042
const relations: Map<string, Relation> = model.getRelations();
4143
const returnValue: Data = {} as Data;
44+
const inputFields = inputType ? inputType.inputFields : undefined;
4245
if (outgoingRecords === undefined) outgoingRecords = new Map<string, Array<string>>();
4346
if (recursiveCall === undefined) recursiveCall = false;
4447

@@ -64,7 +67,8 @@ export default class Transformer {
6467
key,
6568
value,
6669
model,
67-
whitelist
70+
whitelist,
71+
inputFields
6872
)
6973
) {
7074
let relatedModel = Model.getRelatedModel(relations.get(key)!);
@@ -81,6 +85,7 @@ export default class Transformer {
8185
v,
8286
read,
8387
undefined,
88+
undefined,
8489
outgoingRecords,
8590
true
8691
);
@@ -102,6 +107,7 @@ export default class Transformer {
102107
value,
103108
read,
104109
undefined,
110+
undefined,
105111
outgoingRecords,
106112
true
107113
);
@@ -207,14 +213,16 @@ export default class Transformer {
207213
* @param {any} value Value of the field.
208214
* @param {Model} model Model class which contains the field.
209215
* @param {Array<String>|undefined} whitelist Contains a list of fields which should always be included.
216+
* @param {Array<GraphQLField>|undefined} schemaFields Contains a list of schema fields which are defined in the model input type for the mutation.
210217
* @returns {boolean}
211218
*/
212219
public static shouldIncludeOutgoingField(
213220
forFilter: boolean,
214221
fieldName: string,
215222
value: any,
216223
model: Model,
217-
whitelist?: Array<String>
224+
whitelist?: Array<String>,
225+
schemaFields?: Array<GraphQLField>
218226
): boolean {
219227
// Always add fields on the whitelist.
220228
if (whitelist && whitelist.includes(fieldName)) return true;
@@ -225,6 +233,9 @@ export default class Transformer {
225233
// Ignore empty fields
226234
if (value === null || value === undefined) return false;
227235

236+
// Is the field in the mutation type field list, if provided?
237+
if (schemaFields && !schemaFields.find(f => f.name === fieldName)) return false;
238+
228239
// Include all eager save connections
229240
if (model.getRelations().has(fieldName)) {
230241
// We never add relations to filters.

0 commit comments

Comments
 (0)