|
| 1 | +"use strict"; |
| 2 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 3 | +const merge_1 = require("../merge"); |
| 4 | +describe('mergeQueries', () => { |
| 5 | + test('should allow fields present in allowed queries', () => { |
| 6 | + const requestQuery = `{ user { id, name, email } }`; |
| 7 | + const allowedQueries = [`{ user { id, name } }`]; |
| 8 | + const expected = `{ |
| 9 | + user { |
| 10 | + id |
| 11 | + name |
| 12 | + } |
| 13 | +}`; |
| 14 | + expect((0, merge_1.mergeQueries)(requestQuery, allowedQueries)).toBe(expected); |
| 15 | + }); |
| 16 | + test('should exclude fields not present in allowed queries', () => { |
| 17 | + const requestQuery = `{ user { id, name, email } }`; |
| 18 | + const allowedQueries = [`{ user { id } }`]; |
| 19 | + const expected = `{ |
| 20 | + user { |
| 21 | + id |
| 22 | + } |
| 23 | +}`; |
| 24 | + expect((0, merge_1.mergeQueries)(requestQuery, allowedQueries)).toBe(expected); |
| 25 | + }); |
| 26 | + test('should handle nested queries', () => { |
| 27 | + const requestQuery = `{ user { id, profile { name, age } } }`; |
| 28 | + const allowedQueries = [`{ user { profile { name } } }`]; |
| 29 | + const expected = `{ |
| 30 | + user { |
| 31 | + profile { |
| 32 | + name |
| 33 | + } |
| 34 | + } |
| 35 | +}`; |
| 36 | + expect((0, merge_1.mergeQueries)(requestQuery, allowedQueries)).toBe(expected); |
| 37 | + }); |
| 38 | + test('should handle queries with arguments', () => { |
| 39 | + const requestQuery = `{ users(age: 30) { id, name } }`; |
| 40 | + const allowedQueries = [`{ users { name } }`]; |
| 41 | + const expected = `{ |
| 42 | + users(age: 30) { |
| 43 | + name |
| 44 | + } |
| 45 | +}`; |
| 46 | + expect((0, merge_1.mergeQueries)(requestQuery, allowedQueries)).toBe(expected); |
| 47 | + }); |
| 48 | + test('should strictly follow allowed queries with aliases', () => { |
| 49 | + const requestQuery = `{ user: users { id, name } }`; |
| 50 | + const allowedQueries = [`{ user: users { name } }`]; |
| 51 | + const expected = `{ |
| 52 | + user: users { |
| 53 | + name |
| 54 | + } |
| 55 | +}`; |
| 56 | + expect((0, merge_1.mergeQueries)(requestQuery, allowedQueries)).toBe(expected); |
| 57 | + }); |
| 58 | + test('should strictly follow allowed queries without aliases', () => { |
| 59 | + const requestQuery = `{ users { id, name } }`; |
| 60 | + const allowedQueries = [`{ users { name } }`]; |
| 61 | + const expected = `{ |
| 62 | + users { |
| 63 | + name |
| 64 | + } |
| 65 | +}`; |
| 66 | + expect((0, merge_1.mergeQueries)(requestQuery, allowedQueries)).toBe(expected); |
| 67 | + }); |
| 68 | + test('should handle mutations', () => { |
| 69 | + const requestQuery = `mutation { updateUser(id: 1, data: { name: "New Name" }) { id, name } }`; |
| 70 | + const allowedQueries = [ |
| 71 | + `mutation { updateUser(id: 1, data: { name: "New Name" }) { name } }`, |
| 72 | + ]; |
| 73 | + const expected = `mutation { |
| 74 | + updateUser(id: 1, data: {name: "New Name"}) { |
| 75 | + name |
| 76 | + } |
| 77 | +}`; |
| 78 | + expect((0, merge_1.mergeQueries)(requestQuery, allowedQueries)).toBe(expected); |
| 79 | + }); |
| 80 | + test('should handle multiple allowed queries', () => { |
| 81 | + const requestQuery = `{ user { id, name, email } }`; |
| 82 | + const allowedQueries = [`{ user { id } }`, `{ user { name } }`]; |
| 83 | + const expected = `{ |
| 84 | + user { |
| 85 | + id |
| 86 | + name |
| 87 | + } |
| 88 | +}`; |
| 89 | + expect((0, merge_1.mergeQueries)(requestQuery, allowedQueries)).toBe(expected); |
| 90 | + }); |
| 91 | + test('should handle empty requestQuery', () => { |
| 92 | + const requestQuery = ''; |
| 93 | + const allowedQueries = [`{ user { id, name } }`]; |
| 94 | + const expected = ''; |
| 95 | + expect((0, merge_1.mergeQueries)(requestQuery, allowedQueries)).toBe(expected); |
| 96 | + }); |
| 97 | + test('should handle empty allowedQueries', () => { |
| 98 | + const requestQuery = `{ user { id, name, email } }`; |
| 99 | + const allowedQueries = []; |
| 100 | + const expected = ''; |
| 101 | + expect((0, merge_1.mergeQueries)(requestQuery, allowedQueries)).toBe(expected); |
| 102 | + }); |
| 103 | + test('should handle mutations', () => { |
| 104 | + const requestQuery = `mutation { updateUser(id: 1, data: { name: "New Name" }) { id, name } }`; |
| 105 | + const allowedQueries = [ |
| 106 | + `mutation { updateUser(id: 1, data: { name: "New Name" }) { name } }`, |
| 107 | + ]; |
| 108 | + const expected = `mutation { |
| 109 | + updateUser(id: 1, data: {name: "New Name"}) { |
| 110 | + name |
| 111 | + } |
| 112 | +}`; |
| 113 | + expect((0, merge_1.mergeQueries)(requestQuery, allowedQueries)).toBe(expected); |
| 114 | + }); |
| 115 | + test('should handle deeply nested queries', () => { |
| 116 | + const requestQuery = `{ user { id, profile { name, address { street, city } } } }`; |
| 117 | + const allowedQueries = [`{ user { profile { address { street } } } }`]; |
| 118 | + const expected = `{ |
| 119 | + user { |
| 120 | + profile { |
| 121 | + address { |
| 122 | + street |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | +}`; |
| 127 | + expect((0, merge_1.mergeQueries)(requestQuery, allowedQueries)).toBe(expected); |
| 128 | + }); |
| 129 | + test('should handle nested mutations', () => { |
| 130 | + const requestQuery = `mutation { updateUser(id: 1, data: { profile: { name: "New Name" } }) { id } }`; |
| 131 | + const allowedQueries = [ |
| 132 | + `mutation { updateUser(id: 1, data: { profile: { name: "New Name" } }) { id } }`, |
| 133 | + ]; |
| 134 | + const expected = `mutation { |
| 135 | + updateUser(id: 1, data: {profile: {name: "New Name"}}) { |
| 136 | + id |
| 137 | + } |
| 138 | +}`; |
| 139 | + expect((0, merge_1.mergeQueries)(requestQuery, allowedQueries)).toBe(expected); |
| 140 | + }); |
| 141 | +}); |
0 commit comments