1 import { createRuleTester } from "../../../test-utils"
2 import { rule } from "../rule"
3
4 const ruleTester = createRuleTester()
5
6 ruleTester.run("no-wildcard-with-specific-fields", rule, {
7 valid: [
8 // Wildcard alone.
9 {
10 code: `
11 const { data } = await query.graph({
12 entity: "cart",
13 fields: ["*"],
14 })
15 `,
16 },
17 // Wildcard with relation wildcard — relations are kept.
18 {
19 code: `
20 const { data } = await query.graph({
21 entity: "cart",
22 fields: ["*", "items.*"],
23 })
24 `,
25 },
26 // Wildcard with relation field — relations are kept.
27 {
28 code: `
29 const { data } = await query.graph({
30 entity: "cart",
31 fields: ["*", "items.title"],
32 })
33 `,
34 },
35 // Wildcard with multiple relation selections only.
36 {
37 code: `
38 const { data } = await query.graph({
39 entity: "cart",
40 fields: ["*", "items.*", "region.name"],
41 })
42 `,
43 },
44 // Specific top-level fields without a wildcard.
45 {
46 code: `
47 const { data } = await query.graph({
48 entity: "cart",
49 fields: ["id", "total"],
50 })
51 `,
52 },
53 // useQueryGraphStep with wildcard alone.
54 {
55 code: `
56 const { data } = useQueryGraphStep({
57 entity: "cart",
58 fields: ["*", "items.title"],
59 })
60 `,
61 },
62 // Not a graph query — unrelated method named the same has no fields concern.
63 {
64 code: `
65 const result = something.build({
66 fields: ["*", "total"],
67 })
68 `,
69 },
70 // graph call without a fields property.
71 {
72 code: `
73 const { data } = await query.graph({
74 entity: "cart",
75 })
76 `,
77 },
78 // fields is not an inline array literal.
79 {
80 code: `
81 const { data } = await query.graph({
82 entity: "cart",
83 fields: myFields,
84 })
85 `,
86 },
87 // First argument is not an object literal.
88 {
89 code: `
90 const { data } = await query.graph(config)
91 `,
92 },
93 ],
94 invalid: [
95 // The canonical example.
96 {
97 code: `
98 const { data: [cart] } = await query.graph({
99 entity: "cart",
100 fields: ["*", "total"],
101 filters: {
102 id: "cart_123",
103 },
104 })
105 `,
106 errors: [{ messageId: "wildcardWithSpecificFields" }],
107 },
108 // Wildcard listed after the specific field.
109 {
110 code: `
111 const { data } = await query.graph({
112 entity: "cart",
113 fields: ["total", "*"],
114 })
115 `,
116 errors: [{ messageId: "wildcardWithSpecificFields" }],
117 },
118 // Multiple dropped top-level fields — reported once.
119 {
120 code: `
121 const { data } = await query.graph({
122 entity: "cart",
123 fields: ["*", "total", "subtotal"],
124 })
125 `,
126 errors: [{ messageId: "wildcardWithSpecificFields" }],
127 },
128 // Mix of dropped top-level fields and kept relation selections.
129 {
130 code: `
131 const { data } = await query.graph({
132 entity: "cart",
133 fields: ["*", "total", "items.title"],
134 })
135 `,
136 errors: [{ messageId: "wildcardWithSpecificFields" }],
137 },
138 // useQueryGraphStep.
139 {
140 code: `
141 const { data } = useQueryGraphStep({
142 entity: "cart",
143 fields: ["*", "total"],
144 })
145 `,
146 errors: [{ messageId: "wildcardWithSpecificFields" }],
147 },
148 ],
149 })