|
| 1 | +import { parse } from 'graphql'; |
| 2 | +import { getArgumentsWithDirectives } from '../src/index.js'; |
| 3 | + |
| 4 | +describe('getArgumentsWithDirectives', () => { |
| 5 | + it('Should detect single basic directive', () => { |
| 6 | + const node = parse(/* GraphQL */ ` |
| 7 | + type A { |
| 8 | + f1(anArg: String @a): Int |
| 9 | + } |
| 10 | + `); |
| 11 | + |
| 12 | + const result = getArgumentsWithDirectives(node); |
| 13 | + expect(result['A.f1']).toEqual({ anArg: [{ name: 'a', args: {} }] }); |
| 14 | + }); |
| 15 | + |
| 16 | + it('Should detect single basic directive in a type extension', () => { |
| 17 | + const node = parse(/* GraphQL */ ` |
| 18 | + extend type A { |
| 19 | + f1(anArg: String @a): Int |
| 20 | + } |
| 21 | + `); |
| 22 | + |
| 23 | + const result = getArgumentsWithDirectives(node); |
| 24 | + expect(result['A.f1']).toEqual({ anArg: [{ name: 'a', args: {} }] }); |
| 25 | + }); |
| 26 | + |
| 27 | + it('Should parse string argument correctly', () => { |
| 28 | + const node = parse(/* GraphQL */ ` |
| 29 | + type A { |
| 30 | + f1(anArg: String @a(f: "1")): Int |
| 31 | + } |
| 32 | + `); |
| 33 | + |
| 34 | + const result = getArgumentsWithDirectives(node); |
| 35 | + expect(result['A.f1']).toEqual({ anArg: [{ name: 'a', args: { f: '1' } }] }); |
| 36 | + }); |
| 37 | + |
| 38 | + it('Should parse multiple arguments correctly', () => { |
| 39 | + const node = parse(/* GraphQL */ ` |
| 40 | + type A { |
| 41 | + f1(anArg: String @a(a1: "1", a2: 10)): Int |
| 42 | + } |
| 43 | + `); |
| 44 | + |
| 45 | + const result = getArgumentsWithDirectives(node); |
| 46 | + expect(result['A.f1']).toEqual({ anArg: [{ name: 'a', args: { a1: '1', a2: 10 } }] }); |
| 47 | + }); |
| 48 | + |
| 49 | + it('Should parse object arg correctly', () => { |
| 50 | + const node = parse(/* GraphQL */ ` |
| 51 | + type A { |
| 52 | + f1(anArg: String @a(a1: { foo: "bar" })): Int |
| 53 | + } |
| 54 | + `); |
| 55 | + |
| 56 | + const result = getArgumentsWithDirectives(node); |
| 57 | + expect(result['A.f1']).toEqual({ anArg: [{ name: 'a', args: { a1: { foo: 'bar' } } }] }); |
| 58 | + }); |
| 59 | + |
| 60 | + it('Should parse array arg correctly', () => { |
| 61 | + const node = parse(/* GraphQL */ ` |
| 62 | + type A { |
| 63 | + f1(anArg: String @a(a1: [1, 2, 3])): Int |
| 64 | + } |
| 65 | + `); |
| 66 | + |
| 67 | + const result = getArgumentsWithDirectives(node); |
| 68 | + expect(result['A.f1']).toEqual({ anArg: [{ name: 'a', args: { a1: [1, 2, 3] } }] }); |
| 69 | + }); |
| 70 | + |
| 71 | + it('Should parse complex array arg correctly', () => { |
| 72 | + const node = parse(/* GraphQL */ ` |
| 73 | + type A { |
| 74 | + f1(anArg: String @a(a1: ["a", 1, { c: 3, d: true }])): Int |
| 75 | + } |
| 76 | + `); |
| 77 | + |
| 78 | + const result = getArgumentsWithDirectives(node); |
| 79 | + expect(result['A.f1']).toEqual({ anArg: [{ name: 'a', args: { a1: ['a', 1, { c: 3, d: true }] } }] }); |
| 80 | + }); |
| 81 | + |
| 82 | + it('Should detect multiple directives', () => { |
| 83 | + const node = parse(/* GraphQL */ ` |
| 84 | + type A { |
| 85 | + f1(anArg: String @a @b): Int |
| 86 | + } |
| 87 | + `); |
| 88 | + |
| 89 | + const result = getArgumentsWithDirectives(node); |
| 90 | + expect(result['A.f1']).toEqual({ |
| 91 | + anArg: [ |
| 92 | + { name: 'a', args: {} }, |
| 93 | + { name: 'b', args: {} }, |
| 94 | + ], |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + it('Should detect multiple directives and multiple arguments', () => { |
| 99 | + const node = parse(/* GraphQL */ ` |
| 100 | + type A { |
| 101 | + f1(anArg: String @a @b, anotherArg: String @c): Int |
| 102 | + } |
| 103 | + `); |
| 104 | + |
| 105 | + const result = getArgumentsWithDirectives(node); |
| 106 | + expect(result['A.f1']).toEqual({ |
| 107 | + anArg: [ |
| 108 | + { name: 'a', args: {} }, |
| 109 | + { name: 'b', args: {} }, |
| 110 | + ], |
| 111 | + anotherArg: [{ name: 'c', args: {} }], |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + it('Should detect multiple directives and multiple fields', () => { |
| 116 | + const node = parse(/* GraphQL */ ` |
| 117 | + type A { |
| 118 | + f1(anArg: String @a @b): Int |
| 119 | + f2(anotherArg: String @c): Int |
| 120 | + } |
| 121 | + `); |
| 122 | + |
| 123 | + const result = getArgumentsWithDirectives(node); |
| 124 | + expect(result['A.f1']).toEqual({ |
| 125 | + anArg: [ |
| 126 | + { name: 'a', args: {} }, |
| 127 | + { name: 'b', args: {} }, |
| 128 | + ], |
| 129 | + }); |
| 130 | + expect(result['A.f2']).toEqual({ anotherArg: [{ name: 'c', args: {} }] }); |
| 131 | + }); |
| 132 | + |
| 133 | + it('Should detect multiple types', () => { |
| 134 | + const node = parse(/* GraphQL */ ` |
| 135 | + type A { |
| 136 | + f1(anArg: String @a): Int |
| 137 | + } |
| 138 | +
|
| 139 | + type B { |
| 140 | + f2(anArg: String @a): Int |
| 141 | + } |
| 142 | + `); |
| 143 | + |
| 144 | + const result = getArgumentsWithDirectives(node); |
| 145 | + expect(result['A.f1']).toEqual({ anArg: [{ name: 'a', args: {} }] }); |
| 146 | + expect(result['B.f2']).toEqual({ anArg: [{ name: 'a', args: {} }] }); |
| 147 | + }); |
| 148 | + |
| 149 | + it('Should include only fields with arguments with directives', () => { |
| 150 | + const node = parse(/* GraphQL */ ` |
| 151 | + type A { |
| 152 | + f1: String @a |
| 153 | + f2(anArg: Int): Int |
| 154 | + f3(anArg: String @a): Int |
| 155 | + } |
| 156 | + `); |
| 157 | + |
| 158 | + const result = getArgumentsWithDirectives(node); |
| 159 | + expect(result['A.f3']).toBeDefined(); |
| 160 | + expect(Object.keys(result).length).toBe(1); |
| 161 | + }); |
| 162 | +}); |
0 commit comments