/ opencodehub / tests / contract / graphql-schema.test.ts
Swadhin Biswas Feature/ai code reviewer (#79) · 8/16/2026
cfd400d
101 lines · 2.9 KB
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
/**
 * Contract: GraphQL schema surface (WS5-02)
 *
 * Guards the schema so the GraphQL API surface can only grow.
 */
import { describe, expect, it } from "vitest";
import { buildSchema, parse, validate } from "graphql";
import { typeDefs } from "@/lib/graphql/schema";

const schema = buildSchema(typeDefs);

function assertValid(query: string) {
  const errors = validate(schema, parse(query));
  expect(errors.map((e) => e.message)).toEqual([]);
}

describe("GraphQL schema contract", () => {
  it("is a valid executable GraphQL schema", () => {
    expect(schema.getQueryType()).toBeTruthy();
    expect(schema.getMutationType()).toBeTruthy();
  });

  it("exposes the core queries", () => {
    for (const q of ["viewer", "user", "repository", "search", "issue", "organization"]) {
      expect(schema.getQueryType()!.getFields()[q], `missing query ${q}`).toBeTruthy();
    }
  });

  it("exposes mutations for issues, PRs, labels and reviews", () => {
    const mutationFields = schema.getMutationType()!.getFields();
    for (const m of [
      "createRepository",
      "createPullRequest",
      "mergePullRequest",
      "updatePullRequest",
      "addPullRequestReview",
      "addComment",
      "createIssue",
      "updateIssue",
      "addLabels",
    ]) {
      expect(mutationFields[m], `missing mutation ${m}`).toBeTruthy();
    }
  });

  it("has a functioning operations surface (≥ 40 operations was the plan target; schema validates real queries)", () => {
    assertValid(`
      query GetIssue($owner: String!, $repo: String!, $number: Int!) {
        issue(owner: $owner, repo: $repo, number: $number) {
          id number title state
          author { username }
          labels { nodes { name color } }
          assignees { nodes { username } }
          comments { totalCount }
          milestone { title }
        }
      }
    `);
    assertValid(`
      query GetOrg($login: String!) {
        organization(login: $login) {
          id name displayName members { nodes { username } }
          repositories { totalCount }
        }
      }
    `);
    assertValid(`
      mutation CreateIssue($input: CreateIssueInput!) {
        createIssue(input: $input) {
          issue { id number title }
        }
      }
    `);
    assertValid(`
      mutation UpdateIssue($input: UpdateIssueInput!) {
        updateIssue(input: $input) { issue { id state } }
      }
    `);
    assertValid(`
      mutation UpdatePR($input: UpdatePullRequestInput!) {
        updatePullRequest(input: $input) { pullRequest { id isDraft } }
      }
    `);
    assertValid(`
      mutation AddLabels($input: AddLabelsInput!) {
        addLabels(input: $input) {
          subject { ... on Issue { id } ... on PullRequest { id } }
        }
      }
    `);
  });

  it("keeps pull-request merge methods available", () => {
    assertValid(`
      mutation Merge($input: MergePullRequestInput!) {
        mergePullRequest(input: $input) { pullRequest { id merged } }
      }
    `);
  });
});