github-graphql-node-count 0.0.0

Compute the worst-case node count GitHub's GraphQL API attributes to a query, offline, from the query text and its page-size variables.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
//! The counting rules GitHub publishes, one fixture and one asserted total each.
//!
//! Every expected total is a named constant beside the document it belongs to,
//! so a different document reaching the same number does not pass.

use github_graphql_node_count::{node_count, Variables};

/// No page-size variables: the document binds every page size as a literal.
fn no_variables() -> Variables {
    Variables::new()
}

/// One page-size variable, bound to `value`.
fn page(value: u32) -> Variables {
    Variables::from([("page".to_string(), value)])
}

/// A connection nested inside a connection: the inner page size is charged once
/// per outer node, not once overall.
const NESTED_PATH: &str = r#"
query {
  viewer {
    repositories(first: 10) {
      edges {
        node {
          issues(first: 5) {
            edges { node { title } }
          }
        }
      }
    }
  }
}
"#;

/// 10 repositories + 10 x 5 = 50 repository issues.
const NESTED_PATH_NODES: u64 = 60;

#[test]
fn a_nested_connection_multiplies_by_its_parents_page_size() {
    assert_eq!(
        node_count(NESTED_PATH, &no_variables()),
        Ok(NESTED_PATH_NODES)
    );
}

/// Three connections under one parent. An implementation that took the maximum
/// across siblings instead of the sum would answer 10 here.
const SIBLING_PATHS: &str = r#"
query {
  viewer {
    repositories(first: 10) { edges { node { name } } }
    followers(first: 7) { edges { node { login } } }
    following(first: 3) { edges { node { login } } }
  }
}
"#;

/// 10 repositories + 7 followers + 3 following.
const SIBLING_PATHS_NODES: u64 = 20;

#[test]
fn sibling_connections_sum_rather_than_compete() {
    assert_eq!(
        node_count(SIBLING_PATHS, &no_variables()),
        Ok(SIBLING_PATHS_NODES)
    );
    // The distinguishing assertion: the maximum across the siblings is 10.
    assert_ne!(node_count(SIBLING_PATHS, &no_variables()), Ok(10));
}

/// The connection that costs the most sits inside a fragment, so a counter that
/// ignored fragment definitions would answer 10.
const SPREAD_FRAGMENT: &str = r#"
query {
  viewer {
    repositories(first: 10) {
      edges { node { ...RepositoryIssues } }
    }
  }
}

fragment RepositoryIssues on Repository {
  name
  issues(first: 4) {
    edges { node { title } }
  }
}
"#;

/// 10 repositories + 10 x 4 = 40 repository issues.
const SPREAD_FRAGMENT_NODES: u64 = 50;

#[test]
fn a_spread_counts_the_fragment_it_names() {
    assert_eq!(
        node_count(SPREAD_FRAGMENT, &no_variables()),
        Ok(SPREAD_FRAGMENT_NODES)
    );
}

/// `timelineItems.nodes` is a union, so the fields under it are reached through
/// inline fragments — including one nested inside another. `LabeledEvent` costs
/// nothing, which is what makes the total attributable to the other branch.
const INLINE_FRAGMENT_UNION: &str = r#"
query {
  repository(owner: "nickderobertis", name: "onetaskgraph") {
    issue(number: 235) {
      timelineItems(first: 10) {
        nodes {
          ... on CrossReferencedEvent {
            source {
              ... on PullRequest {
                comments(first: 5) { nodes { bodyHTML } }
              }
            }
          }
          ... on LabeledEvent {
            label { name }
          }
        }
      }
    }
  }
}
"#;

/// 10 timeline items + 10 x 5 = 50 pull-request comments.
const INLINE_FRAGMENT_UNION_NODES: u64 = 60;

#[test]
fn an_inline_fragment_on_a_union_counts_against_its_parent() {
    assert_eq!(
        node_count(INLINE_FRAGMENT_UNION, &no_variables()),
        Ok(INLINE_FRAGMENT_UNION_NODES)
    );
}

/// `RepositoryIssues` is reachable only through `ViewerRepositories`, so a
/// counter that resolved one level of spread would answer 6.
const NESTED_SPREADS: &str = r#"
query {
  viewer { ...ViewerRepositories }
}

fragment ViewerRepositories on User {
  repositories(first: 6) {
    edges { node { ...RepositoryIssues } }
  }
}

fragment RepositoryIssues on Repository {
  issues(first: 2) {
    edges { node { title } }
  }
}
"#;

/// 6 repositories + 6 x 2 = 12 repository issues.
const NESTED_SPREADS_NODES: u64 = 18;

#[test]
fn a_spread_reached_through_another_spread_is_counted() {
    assert_eq!(
        node_count(NESTED_SPREADS, &no_variables()),
        Ok(NESTED_SPREADS_NODES)
    );
}

/// The shape that matters most to a consumer: one page-size constant reused down
/// a nested path, so binding it to `n` moves the total by `n` squared.
const VARIABLE_SPENT_TWICE: &str = r#"
query ViewerIssues($page: Int!) {
  viewer {
    repositories(first: $page) {
      edges {
        node {
          issues(first: $page) {
            edges { node { title } }
          }
        }
      }
    }
  }
}
"#;

/// 3 repositories + 3 x 3 = 9 repository issues.
const VARIABLE_SPENT_TWICE_AT_3: u64 = 12;
/// 5 repositories + 5 x 5 = 25 repository issues.
const VARIABLE_SPENT_TWICE_AT_5: u64 = 30;
/// 9 repositories + 9 x 9 = 81 repository issues.
const VARIABLE_SPENT_TWICE_AT_9: u64 = 90;

#[test]
fn one_variable_spent_twice_moves_the_total_by_its_square() {
    assert_eq!(
        node_count(VARIABLE_SPENT_TWICE, &page(3)),
        Ok(VARIABLE_SPENT_TWICE_AT_3)
    );
    assert_eq!(
        node_count(VARIABLE_SPENT_TWICE, &page(5)),
        Ok(VARIABLE_SPENT_TWICE_AT_5)
    );
    assert_eq!(
        node_count(VARIABLE_SPENT_TWICE, &page(9)),
        Ok(VARIABLE_SPENT_TWICE_AT_9)
    );

    // n squared, not 2n: tripling the page size from 3 to 9 multiplies the
    // repository-issue term by nine.
    for (n, total) in [
        (3u64, VARIABLE_SPENT_TWICE_AT_3),
        (5, VARIABLE_SPENT_TWICE_AT_5),
        (9, VARIABLE_SPENT_TWICE_AT_9),
    ] {
        assert_eq!(total, n + n * n);
    }
}

/// The four spellings of one page size. They differ only in the outer argument,
/// so anything but an equal count is the crate honouring one form over another.
const OUTER_FIRST_LITERAL: &str = r#"
query {
  viewer {
    repositories(first: 25) {
      edges { node { issues(first: 4) { edges { node { title } } } } }
    }
  }
}
"#;

const OUTER_LAST_LITERAL: &str = r#"
query {
  viewer {
    repositories(last: 25) {
      edges { node { issues(first: 4) { edges { node { title } } } } }
    }
  }
}
"#;

const OUTER_FIRST_VARIABLE: &str = r#"
query ViewerIssues($page: Int!) {
  viewer {
    repositories(first: $page) {
      edges { node { issues(first: 4) { edges { node { title } } } } }
    }
  }
}
"#;

const OUTER_LAST_VARIABLE: &str = r#"
query ViewerIssues($page: Int!) {
  viewer {
    repositories(last: $page) {
      edges { node { issues(first: 4) { edges { node { title } } } } }
    }
  }
}
"#;

/// 25 repositories + 25 x 4 = 100 repository issues.
const PAGE_ARGUMENT_NODES: u64 = 125;

#[test]
fn first_and_last_and_literal_and_variable_all_count_the_same() {
    assert_eq!(
        node_count(OUTER_FIRST_LITERAL, &no_variables()),
        Ok(PAGE_ARGUMENT_NODES)
    );
    assert_eq!(
        node_count(OUTER_LAST_LITERAL, &no_variables()),
        Ok(PAGE_ARGUMENT_NODES)
    );
    assert_eq!(
        node_count(OUTER_FIRST_VARIABLE, &page(25)),
        Ok(PAGE_ARGUMENT_NODES)
    );
    assert_eq!(
        node_count(OUTER_LAST_VARIABLE, &page(25)),
        Ok(PAGE_ARGUMENT_NODES)
    );
}

/// GitHub rejects a connection supplying both; the crate takes the larger so the
/// answer it does give stays a worst case.
const BOTH_FIRST_AND_LAST: &str = r#"
query {
  viewer {
    repositories(first: 3, last: 30) {
      edges { node { name } }
    }
  }
}
"#;

/// The larger of the two page sizes, 30.
const BOTH_FIRST_AND_LAST_NODES: u64 = 30;

#[test]
fn a_field_supplying_both_first_and_last_takes_the_larger() {
    assert_eq!(
        node_count(BOTH_FIRST_AND_LAST, &no_variables()),
        Ok(BOTH_FIRST_AND_LAST_NODES)
    );
}

/// Working from text alone, this crate can only see a connection that says
/// `first`/`last`. A field with no page size adds no nodes and no multiplier —
/// the documented blind spot, asserted so it stays deliberate.
const NO_PAGE_SIZE_ANYWHERE: &str = r#"
query {
  viewer {
    login
    repository(owner: "nickderobertis", name: "onevcs") {
      name
      issues { edges { node { title } } }
    }
  }
}
"#;

/// Nothing carries `first`/`last`, so nothing is charged.
const NO_PAGE_SIZE_ANYWHERE_NODES: u64 = 0;

#[test]
fn a_document_with_no_page_size_costs_nothing_here() {
    assert_eq!(
        node_count(NO_PAGE_SIZE_ANYWHERE, &no_variables()),
        Ok(NO_PAGE_SIZE_ANYWHERE_NODES)
    );
}

/// A shorthand operation: a bare selection set with no `query` keyword.
const SHORTHAND_OPERATION: &str = r#"
{
  viewer {
    repositories(first: 3) { edges { node { name } } }
  }
}
"#;

/// 3 repositories.
const SHORTHAND_OPERATION_NODES: u64 = 3;

#[test]
fn a_shorthand_operation_is_counted() {
    assert_eq!(
        node_count(SHORTHAND_OPERATION, &no_variables()),
        Ok(SHORTHAND_OPERATION_NODES)
    );
}

/// A mutation whose payload reads a connection. `$body` is declared but no
/// `first:`/`last:` references it, so it need not be bound.
const MUTATION_OPERATION: &str = r#"
mutation AddComment($body: String!) {
  addComment(input: { subjectId: "MDU6SXNzdWUx", body: $body }) {
    commentEdge {
      node {
        reactions(first: 5) { nodes { content } }
      }
    }
  }
}
"#;

/// 5 reactions on the created comment.
const MUTATION_OPERATION_NODES: u64 = 5;

#[test]
fn a_mutation_is_counted_and_an_unreferenced_variable_need_not_be_bound() {
    assert_eq!(
        node_count(MUTATION_OPERATION, &no_variables()),
        Ok(MUTATION_OPERATION_NODES)
    );
}

/// GitHub's schema has no subscription root today, but the GraphQL grammar
/// admits one — so the crate answers rather than panicking if a caller hands it
/// one.
const SUBSCRIPTION_OPERATION: &str = r#"
subscription WatchIssues {
  issueEvents {
    issue {
      comments(first: 8) { nodes { bodyHTML } }
    }
  }
}
"#;

/// 8 comments.
const SUBSCRIPTION_OPERATION_NODES: u64 = 8;

#[test]
fn a_subscription_is_counted() {
    assert_eq!(
        node_count(SUBSCRIPTION_OPERATION, &no_variables()),
        Ok(SUBSCRIPTION_OPERATION_NODES)
    );
}

/// A fragment the document defines but never spreads is dead weight GitHub would
/// reject; it is not counted here either.
const UNUSED_FRAGMENT: &str = r#"
query {
  viewer {
    repositories(first: 4) { edges { node { name } } }
  }
}

fragment Unused on Repository {
  issues(first: 100) { edges { node { title } } }
}
"#;

/// 4 repositories; the unspread fragment's 100 issues are not charged.
const UNUSED_FRAGMENT_NODES: u64 = 4;

#[test]
fn a_fragment_that_is_never_spread_is_not_counted() {
    assert_eq!(
        node_count(UNUSED_FRAGMENT, &no_variables()),
        Ok(UNUSED_FRAGMENT_NODES)
    );
}