| 1 | import { readFileSync } from 'node:fs'; |
| 2 | import path from 'node:path |
| 3 | import { schema as |
| 4 | import { |
| 5 | buildSchema, |
| 6 | extendSchema, |
| 7 | type GraphQLNamedType, |
| 8 | type GraphQLType, |
| 9 | isEnumType, |
| 10 | isInputObjectType, |
| 11 | isInterfaceType, |
| 12 | isListType, |
| 13 | isNonNullType, |
| 14 | isObjectType, |
| 15 | isUnionType, |
| 16 | parse, |
| 17 | } from 'graphql'; |
| 18 | import { describe, expect |
| 19 | |
| 20 | const ours = buildSchema |
| 21 | readFileSync( |
| 22 | path.join(import. |
| 23 | 'utf8', |
| 24 | ), |
| 25 | ); |
| 26 | /** Fields github.com serves, and gh 2.102 asks for, that @octokit/graphql-schema 15.26.1 (the latest release) predates. Each is copied from GitHub's API reference. */ |
| 27 | const NEWER_THAN_PACKAGE = ` |
| 28 | enum IssueTypeColor { BLUE GRAY GREEN ORANGE PINK PURPLE RED YELLOW } |
| 29 | type IssueType implements Node { color: IssueTypeColor! description: String id: ID! isEnabled: Boolean! name: String! } |
| 30 | extend type Issue { issueType: IssueType } |
| 31 | `; |
| 32 | |
| 33 | const theirs = extendSchema |
| 34 | buildSchema(github.idl |
| 35 | parse(NEWER_THAN_PACKAGE), |
| 36 | { assumeValidSDL: true }, |
| 37 | ); |
| 38 | |
| 39 | /** `[User!]!` -> `L!(N!(User))`: shape and name, so two types compare as strings. */ |
| 40 | function shape(type: |
| 41 | if (isNonNullType(type |
| 42 | if (isListType(type |
| 43 | return type.name |
| 44 | } |
| 45 | |
| 46 | /** Ours may be stricter than GitHub's output type (non-null where GitHub allows null) but never looser, and never a different name or list shape. */ |
| 47 | function outputConforms(ourType: |
| 48 | if (isNonNullType(theirType |
| 49 | return ( |
| 50 | isNonNullType(ourType) && |
| 51 | ); |
| 52 | if (isNonNullType(ourType |
| 53 | if (isListType(theirType |
| 54 | return ( |
| 55 | isListType(ourType) && |
| 56 | ); |
| 57 | return ( |
| 58 | !isListType(ourType) |
| 59 | (ourType as GraphQLNamedType |
| 60 | ); |
| 61 | } |
| 62 | |
| 63 | const ourTypes = Object |
| 64 | (type) => |
| 65 | !type.name.startsWith |
| 66 | !['String', 'Int |
| 67 | ); |
| 68 | |
| 69 | describe('github.schema.gql conforms to GitHub', () |
| 70 | it.each(ourTypes |
| 71 | '%s', |
| 72 | (name, type) |
| 73 | const theirType = |
| 74 | expect(theirType, ` |
| 75 | expect( |
| 76 | theirType!.constructor.name |
| 77 | `${name} is a different kind of type on GitHub`, |
| 78 | ).toBe(type. |
| 79 | const problems: |
| 80 | |
| 81 | if ( |
| 82 | (isObjectType(type) |
| 83 | (isObjectType(theirType) |
| 84 | ) { |
| 85 | const theirFields = |
| 86 | for (const field |
| 87 | const theirField = |
| 88 | if (!theirField) |
| 89 | problems.push(`${ |
| 90 | continue; |
| 91 | } |
| 92 | if (!outputConforms(field |
| 93 | problems.push( |
| 94 | `${name}.${field |
| 95 | ); |
| 96 | for (const arg |
| 97 | const theirArg = |
| 98 | (candidate) => |
| 99 | ); |
| 100 | if (!theirArg) |
| 101 | problems.push( |
| 102 | `${name}.${field |
| 103 | ); |
| 104 | else if ( |
| 105 | shape(arg.type |
| 106 | !( |
| 107 | isNonNullType(theirArg.type |
| 108 | shape(arg.type |
| 109 | ) |
| 110 | ) |
| 111 | problems.push( |
| 112 | `${name}.${field |
| 113 | ); |
| 114 | } |
| 115 | } |
| 116 | const theirInterfaces = |
| 117 | for (const implemented |
| 118 | if (!theirInterfaces.includes |
| 119 | problems.push( |
| 120 | `${name} implements ${implemented |
| 121 | ); |
| 122 | } |
| 123 | |
| 124 | if (isEnumType(type |
| 125 | const theirValues = |
| 126 | for (const value |
| 127 | if (!theirValues.includes |
| 128 | problems.push(`${ |
| 129 | } |
| 130 | |
| 131 | if (isUnionType(type |
| 132 | const theirMembers = |
| 133 | for (const member |
| 134 | if (!theirMembers.includes |
| 135 | problems.push(`${ |
| 136 | } |
| 137 | |
| 138 | if (isInputObjectType(type |
| 139 | const theirFields = |
| 140 | for (const field |
| 141 | const theirField = |
| 142 | if (!theirField) |
| 143 | problems.push(`${ |
| 144 | else if ( |
| 145 | shape(field.type |
| 146 | shape(theirField.type |
| 147 | ) |
| 148 | problems.push( |
| 149 | `${name}.${field |
| 150 | ); |
| 151 | else if (isNonNullType( |
| 152 | problems.push( |
| 153 | `${name}.${field |
| 154 | ); |
| 155 | } |
| 156 | for (const theirField |
| 157 | if ( |
| 158 | isNonNullType(theirField.type |
| 159 | theirField.defaultValue === |
| 160 | !type.getFields()[theirField |
| 161 | ) |
| 162 | problems.push( |
| 163 | `${name}.${theirField |
| 164 | ); |
| 165 | } |
| 166 | |
| 167 | expect(problems).toEqual |
| 168 | }, |
| 169 | ); |
| 170 | }); |