Skip to content

Commit 05a0694

Browse files
committed
Introduce has{Any,All}Directives wrappers for hasDirectives.
1 parent 2c0cdc0 commit 05a0694

File tree

2 files changed

+42
-27
lines changed

2 files changed

+42
-27
lines changed

src/utilities/graphql/__tests__/directives.ts

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import gql from 'graphql-tag';
22
import { cloneDeep } from 'lodash';
33

4-
import { shouldInclude, hasDirectives } from '../directives';
54
import { getQueryDefinition } from '../getFromAST';
5+
import {
6+
shouldInclude,
7+
hasDirectives,
8+
hasAnyDirectives,
9+
hasAllDirectives,
10+
} from '../directives';
611

712
describe('hasDirectives', () => {
813
it('should allow searching the ast for a directive', () => {
@@ -91,7 +96,7 @@ describe('hasDirectives', () => {
9196

9297
it('works with both any and all semantics', () => {
9398
expect(
94-
hasDirectives(['client', 'defer'], gql`
99+
hasAnyDirectives(['client', 'defer'], gql`
95100
query {
96101
meetings {
97102
id
@@ -100,22 +105,22 @@ describe('hasDirectives', () => {
100105
}
101106
}
102107
}
103-
`, false) // This false forces the default behavior (any)
108+
`)
104109
).toBe(true);
105110

106111
expect(
107-
hasDirectives(['client', 'defer'], gql`
112+
hasAnyDirectives(['client', 'defer'], gql`
108113
query {
109114
meetings {
110115
id
111116
room { size }
112117
}
113118
}
114-
`, false) // This false forces the default behavior (any)
119+
`)
115120
).toBe(false);
116121

117122
expect(
118-
hasDirectives(['client', 'defer'], gql`
123+
hasAnyDirectives(['client', 'defer'], gql`
119124
query {
120125
meetings {
121126
id
@@ -125,11 +130,11 @@ describe('hasDirectives', () => {
125130
}
126131
}
127132
}
128-
`, false) // This false forces the default behavior (any)
133+
`)
129134
).toBe(true);
130135

131136
expect(
132-
hasDirectives(['client', 'defer'], gql`
137+
hasAllDirectives(['client', 'defer'], gql`
133138
query {
134139
meetings {
135140
id
@@ -138,22 +143,22 @@ describe('hasDirectives', () => {
138143
}
139144
}
140145
}
141-
`, true) // This true requires all directives to be present
146+
`)
142147
).toBe(false);
143148

144149
expect(
145-
hasDirectives(['client', 'defer'], gql`
150+
hasAllDirectives(['client', 'defer'], gql`
146151
query {
147152
meetings {
148153
id
149154
room { size }
150155
}
151156
}
152-
`, true) // This true requires all directives to be present
157+
`)
153158
).toBe(false);
154159

155160
expect(
156-
hasDirectives(['client', 'defer'], gql`
161+
hasAllDirectives(['client', 'defer'], gql`
157162
query {
158163
meetings {
159164
id
@@ -163,11 +168,11 @@ describe('hasDirectives', () => {
163168
}
164169
}
165170
}
166-
`, true) // This true requires all directives to be present
171+
`)
167172
).toBe(false);
168173

169174
expect(
170-
hasDirectives(['client', 'defer'], gql`
175+
hasAllDirectives(['client', 'defer'], gql`
171176
query {
172177
meetings {
173178
id
@@ -179,11 +184,11 @@ describe('hasDirectives', () => {
179184
}
180185
}
181186
}
182-
`, true) // This true requires all directives to be present
187+
`)
183188
).toBe(true);
184189

185190
expect(
186-
hasDirectives(['live', 'client', 'defer'], gql`
191+
hasAllDirectives(['live', 'client', 'defer'], gql`
187192
query {
188193
meetings {
189194
id
@@ -195,11 +200,11 @@ describe('hasDirectives', () => {
195200
}
196201
}
197202
}
198-
`, true) // This true requires all directives to be present
203+
`)
199204
).toBe(false);
200205

201206
expect(
202-
hasDirectives(['live', 'client', 'defer'], gql`
207+
hasAllDirectives(['live', 'client', 'defer'], gql`
203208
query @live {
204209
meetings {
205210
room {
@@ -211,13 +216,13 @@ describe('hasDirectives', () => {
211216
id
212217
}
213218
}
214-
`, true) // This true requires all directives to be present
219+
`)
215220
).toBe(true);
216221
});
217222

218223
it('works when names are duplicated', () => {
219224
expect(
220-
hasDirectives(['client', 'client', 'client'], gql`
225+
hasAnyDirectives(['client', 'client', 'client'], gql`
221226
query {
222227
fromClient @client {
223228
asdf
@@ -228,18 +233,18 @@ describe('hasDirectives', () => {
228233
).toBe(true);
229234

230235
expect(
231-
hasDirectives(['client', 'client', 'client'], gql`
236+
hasAllDirectives(['client', 'client', 'client'], gql`
232237
query {
233238
fromClient @client {
234239
asdf
235240
foo
236241
}
237242
}
238-
`, true) // This true requires all directives to be present
243+
`)
239244
).toBe(true);
240245

241246
expect(
242-
hasDirectives(['live', 'live', 'defer'], gql`
247+
hasAnyDirectives(['live', 'live', 'defer'], gql`
243248
query {
244249
fromClient @client {
245250
asdf
@@ -250,18 +255,18 @@ describe('hasDirectives', () => {
250255
).toBe(false);
251256

252257
expect(
253-
hasDirectives(['live', 'live', 'defer'], gql`
258+
hasAllDirectives(['live', 'live', 'defer'], gql`
254259
query {
255260
fromClient @client {
256261
asdf
257262
foo @include(if: true)
258263
}
259264
}
260-
`, true)
265+
`)
261266
).toBe(false);
262267

263268
expect(
264-
hasDirectives(['live', 'live', 'defer'], gql`
269+
hasAllDirectives(['live', 'live', 'defer'], gql`
265270
query @live {
266271
fromClient @client {
267272
... @defer {
@@ -270,7 +275,7 @@ describe('hasDirectives', () => {
270275
}
271276
}
272277
}
273-
`, true)
278+
`)
274279
).toBe(true);
275280
});
276281
});

src/utilities/graphql/directives.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ export function getDirectiveNames(root: ASTNode) {
5555
return names;
5656
}
5757

58+
export const hasAnyDirectives = (
59+
names: string[],
60+
root: ASTNode,
61+
) => hasDirectives(names, root, false);
62+
63+
export const hasAllDirectives = (
64+
names: string[],
65+
root: ASTNode,
66+
) => hasDirectives(names, root, true);
67+
5868
export function hasDirectives(
5969
names: string[],
6070
root: ASTNode,

0 commit comments

Comments
 (0)