Skip to content

Commit fba849d

Browse files
committed
feat: skip arguments when not in input type
1 parent d32aad4 commit fba849d

15 files changed

+75
-4
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@vuex-orm/plugin-graphql",
3-
"version": "1.0.0-rc.38",
3+
"version": "1.0.0-rc.39",
44
"description": "Vuex-ORM persistence plugin to sync the store against a GraphQL API.",
55
"main": "dist/vuex-orm-graphql.cjs.js",
66
"browser": "dist/vuex-orm-graphql.esm.js",

src/graphql/transformer.ts

+21
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 } from "../support/interfaces";
1314

1415
/**
1516
* This class provides methods to transform incoming data from GraphQL in to a format Vuex-ORM understands and
@@ -222,6 +223,9 @@ export default class Transformer {
222223
// Ignore empty fields
223224
if (value === null || value === undefined) return false;
224225

226+
// Ignore fields that don't exist in the input type
227+
if (!this.inputTypeContainsField(model, fieldName)) return false;
228+
225229
// Include all eager save connections
226230
if (model.getRelations().has(fieldName)) {
227231
// We never add relations to filters.
@@ -239,6 +243,23 @@ export default class Transformer {
239243
return true;
240244
}
241245

246+
/**
247+
* Tells whether a field is in the input type.
248+
* @param {Model} model
249+
* @param {string} fieldName
250+
*/
251+
private static inputTypeContainsField(model: Model, fieldName: string): boolean {
252+
const inputTypeName = `${model.singularName}Input`;
253+
const inputType: GraphQLType | null = Context.getInstance().schema!.getType(
254+
inputTypeName,
255+
false
256+
);
257+
258+
if (inputType === null) throw new Error(`Type ${inputType} doesn't exist.`);
259+
260+
return inputType.inputFields!.find(f => f.name === fieldName) != null;
261+
}
262+
242263
/**
243264
* Registers a record for recursion detection.
244265
* @param {Map<string, Array<string>>} records Map of IDs.

test/integration/actions/customMutation.spec.ts

+4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ mutation UpvotePost($captchaToken: String!, $id: ID!) {
3535
author {
3636
id
3737
name
38+
role
3839
profile {
3940
id
4041
email
@@ -51,6 +52,7 @@ mutation UpvotePost($captchaToken: String!, $id: ID!) {
5152
author {
5253
id
5354
name
55+
role
5456
profile {
5557
id
5658
email
@@ -98,6 +100,7 @@ mutation UpvotePost($captchaToken: String!, $id: ID!) {
98100
author {
99101
id
100102
name
103+
role
101104
profile {
102105
id
103106
email
@@ -114,6 +117,7 @@ mutation UpvotePost($captchaToken: String!, $id: ID!) {
114117
author {
115118
id
116119
name
120+
role
117121
profile {
118122
id
119123
email

test/integration/actions/customQuery.spec.ts

+4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ query UnpublishedPosts($authorId: ID!) {
3030
author {
3131
id
3232
name
33+
role
3334
profile {
3435
id
3536
email
@@ -46,6 +47,7 @@ query UnpublishedPosts($authorId: ID!) {
4647
author {
4748
id
4849
name
50+
role
4951
profile {
5052
id
5153
email
@@ -92,6 +94,7 @@ query UnpublishedPosts($authorId: ID!, $id: ID!) {
9294
author {
9395
id
9496
name
97+
role
9598
profile {
9699
id
97100
email
@@ -108,6 +111,7 @@ query UnpublishedPosts($authorId: ID!, $id: ID!) {
108111
author {
109112
id
110113
name
114+
role
111115
profile {
112116
id
113117
email

test/integration/actions/deleteAndDestroy.spec.ts

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ mutation DeletePost($id: ID!) {
3232
author {
3333
id
3434
name
35+
role
3536
profile {
3637
id
3738
email
@@ -48,6 +49,7 @@ mutation DeletePost($id: ID!) {
4849
author {
4950
id
5051
name
52+
role
5153
profile {
5254
id
5355
email

test/integration/actions/destroy.spec.ts

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ mutation DeletePost($id: ID!) {
3232
author {
3333
id
3434
name
35+
role
3536
profile {
3637
id
3738
email
@@ -48,6 +49,7 @@ mutation DeletePost($id: ID!) {
4849
author {
4950
id
5051
name
52+
role
5153
profile {
5254
id
5355
email

test/integration/actions/fetch.spec.ts

+6
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ query Post($id: ID!) {
2929
author {
3030
id
3131
name
32+
role
3233
profile {
3334
id
3435
email
@@ -45,6 +46,7 @@ query Post($id: ID!) {
4546
author {
4647
id
4748
name
49+
role
4850
profile {
4951
id
5052
email
@@ -110,6 +112,7 @@ query User($id: ID!) {
110112
user(id: $id) {
111113
id
112114
name
115+
role
113116
profile {
114117
id
115118
email
@@ -165,6 +168,7 @@ query Users($profileId: ID!, $posts: [PostFilter]!) {
165168
nodes {
166169
id
167170
name
171+
role
168172
profile {
169173
id
170174
email
@@ -208,6 +212,7 @@ query Users($profile: ProfileInput!) {
208212
nodes {
209213
id
210214
name
215+
role
211216
profile {
212217
id
213218
email
@@ -237,6 +242,7 @@ query Users {
237242
nodes {
238243
id
239244
name
245+
role
240246
profile {
241247
id
242248
email

test/integration/actions/push.spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ mutation UpdateUser($id: ID!, $user: UserInput!) {
3030
updateUser(id: $id, user: $user) {
3131
id
3232
name
33+
role
3334
profile {
3435
id
3536
email

test/integration/plugin.spec.ts

+6
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ describe("Plugin GraphQL", () => {
3131

3232
describe("$isPersisted", () => {
3333
test("is false for newly created records", async () => {
34+
await Context.getInstance().loadSchema();
35+
3436
const insertedData = await User.insert({ data: { name: "Snoopy" } });
3537
let user: Data = insertedData.users[0] as Data;
3638
expect(user.$isPersisted).toBeFalsy();
@@ -40,6 +42,8 @@ describe("Plugin GraphQL", () => {
4042
});
4143

4244
test("is true for persisted records", async () => {
45+
await Context.getInstance().loadSchema();
46+
4347
const insertedData = await User.insert({ data: { name: "Snoopy" } });
4448
let user: Data = insertedData.users[0] as Data;
4549

@@ -52,6 +56,8 @@ describe("Plugin GraphQL", () => {
5256
});
5357

5458
test("is true for fetched records", async () => {
59+
await Context.getInstance().loadSchema();
60+
5561
// @ts-ignore
5662
await User.fetch(1);
5763

test/integration/test-utils.spec.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ let vuexOrmGraphQL;
99

1010
const userData = {
1111
id: 42,
12-
name: "Charlie Brown"
12+
name: "Charlie Brown",
13+
role: "user"
1314
};
1415

1516
const userResult = {
@@ -19,6 +20,7 @@ const userResult = {
1920
$id: "42",
2021
$isPersisted: true,
2122
name: "Charlie Brown",
23+
role: "user",
2224
profileId: 0,
2325
posts: [],
2426
comments: [],
@@ -51,6 +53,7 @@ describe("TestUtils", () => {
5153
const userData2 = JSON.parse(JSON.stringify(userData));
5254
userData2.id = 8;
5355
userData2.name = "Snoopy";
56+
userData2.role = "admin";
5457

5558
mock("fetch")
5659
.for(User)
@@ -70,6 +73,7 @@ describe("TestUtils", () => {
7073
$id: "8",
7174
$isPersisted: true,
7275
name: "Snoopy",
76+
role: "admin",
7377
profileId: 0,
7478
posts: [],
7579
comments: [],
@@ -80,6 +84,7 @@ describe("TestUtils", () => {
8084
$id: "42",
8185
$isPersisted: true,
8286
name: "Charlie Brown",
87+
role: "user",
8388
profileId: 0,
8489
posts: [],
8590
comments: [],

test/support/mock-data.ts

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export class User extends ORMModel {
1717
id: this.uid(),
1818
name: this.string(""),
1919
profileId: this.number(0),
20+
role: this.string(""),
2021
posts: this.hasMany(Post, "authorId"),
2122
comments: this.hasMany(Comment, "authorId"),
2223
profile: this.belongsTo(Profile, "profileId")

test/support/mock-schema.ts

+4
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ export const typeDefs = `
8585
profileId: ID
8686
posts: PostConnection
8787
comments: CommentConnection
88+
role: String
8889
profile: Profile
8990
}
9091
@@ -400,18 +401,21 @@ const users = [
400401
{
401402
id: 1,
402403
name: "Charlie Brown",
404+
role: "user",
403405
profileId: 1
404406
},
405407

406408
{
407409
id: 2,
408410
name: "Peppermint Patty",
411+
role: "user",
409412
profileId: 2
410413
},
411414

412415
{
413416
id: 3,
414417
name: "Snoopy",
418+
role: "admin",
415419
profileId: 3
416420
}
417421
];

test/unit/model.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ describe("Model", () => {
3636

3737
describe(".getQueryFields", () => {
3838
test("returns a list of the models fields", () => {
39-
expect(model.getQueryFields()).toEqual(["id", "name"]);
39+
expect(model.getQueryFields()).toEqual(["id", "name", "role"]);
4040
});
4141
});
4242

0 commit comments

Comments
 (0)