|
| 1 | +import assert from 'node:assert'; |
| 2 | +import { describe, test } from 'node:test'; |
| 3 | +import { sql } from 'slonik'; |
| 4 | +import { createStorage } from '../../services/storage/src/index'; |
| 5 | +import { initMigrationTestingEnvironment } from './utils/testkit'; |
| 6 | + |
| 7 | +const TEST_CASES: Array<{ in: any; out: any }> = [ |
| 8 | + { |
| 9 | + in: { |
| 10 | + 'input-name': [ |
| 11 | + 1, |
| 12 | + { |
| 13 | + checkQueries: true, |
| 14 | + checkInputType: true, |
| 15 | + checkMutations: true, |
| 16 | + caseSensitiveInputType: true, |
| 17 | + }, |
| 18 | + ], |
| 19 | + alphabetize: [ |
| 20 | + 1, |
| 21 | + { |
| 22 | + fields: ['ObjectTypeDefinition'], |
| 23 | + values: ['EnumTypeDefinition'], |
| 24 | + arguments: ['FieldDefinition'], |
| 25 | + definitions: true, |
| 26 | + }, |
| 27 | + ], |
| 28 | + 'description-style': [1, { style: 'block' }], |
| 29 | + 'naming-convention': [ |
| 30 | + 1, |
| 31 | + { |
| 32 | + types: 'PascalCase', |
| 33 | + Argument: 'camelCase', |
| 34 | + FieldDefinition: 'camelCase', |
| 35 | + DirectiveDefinition: 'camelCase', |
| 36 | + EnumValueDefinition: 'UPPER_CASE', |
| 37 | + InputValueDefinition: 'camelCase', |
| 38 | + 'FieldDefinition[parent.name.value=Query]': { |
| 39 | + forbiddenPrefixes: ['query', 'get'], |
| 40 | + forbiddenSuffixes: ['Query'], |
| 41 | + }, |
| 42 | + 'FieldDefinition[parent.name.value=Mutation]': { |
| 43 | + forbiddenPrefixes: ['mutation'], |
| 44 | + forbiddenSuffixes: ['Mutation'], |
| 45 | + }, |
| 46 | + 'FieldDefinition[parent.name.value=Subscription]': { |
| 47 | + forbiddenPrefixes: ['subscription'], |
| 48 | + forbiddenSuffixes: ['Subscription'], |
| 49 | + }, |
| 50 | + }, |
| 51 | + ], |
| 52 | + 'require-nullable-fields-with-oneof': [1], |
| 53 | + 'no-case-insensitive-enum-values-duplicates': [1], |
| 54 | + 'require-field-of-type-query-in-mutation-result': [1], |
| 55 | + }, |
| 56 | + out: { |
| 57 | + 'input-name': [ |
| 58 | + 1, |
| 59 | + { |
| 60 | + checkQueries: true, |
| 61 | + checkInputType: true, |
| 62 | + checkMutations: true, |
| 63 | + caseSensitiveInputType: true, |
| 64 | + }, |
| 65 | + ], |
| 66 | + alphabetize: [ |
| 67 | + 1, |
| 68 | + { |
| 69 | + fields: ['ObjectTypeDefinition'], |
| 70 | + values: true, |
| 71 | + arguments: ['FieldDefinition'], |
| 72 | + definitions: true, |
| 73 | + }, |
| 74 | + ], |
| 75 | + 'description-style': [1, { style: 'block' }], |
| 76 | + 'naming-convention': [ |
| 77 | + 1, |
| 78 | + { |
| 79 | + types: 'PascalCase', |
| 80 | + Argument: 'camelCase', |
| 81 | + FieldDefinition: 'camelCase', |
| 82 | + DirectiveDefinition: 'camelCase', |
| 83 | + EnumValueDefinition: 'UPPER_CASE', |
| 84 | + InputValueDefinition: 'camelCase', |
| 85 | + 'FieldDefinition[parent.name.value=Query]': { |
| 86 | + forbiddenPrefixes: ['query', 'get'], |
| 87 | + forbiddenSuffixes: ['Query'], |
| 88 | + }, |
| 89 | + 'FieldDefinition[parent.name.value=Mutation]': { |
| 90 | + forbiddenPrefixes: ['mutation'], |
| 91 | + forbiddenSuffixes: ['Mutation'], |
| 92 | + }, |
| 93 | + 'FieldDefinition[parent.name.value=Subscription]': { |
| 94 | + forbiddenPrefixes: ['subscription'], |
| 95 | + forbiddenSuffixes: ['Subscription'], |
| 96 | + }, |
| 97 | + }, |
| 98 | + ], |
| 99 | + 'unique-enum-value-names': [1], |
| 100 | + 'require-nullable-fields-with-oneof': [1], |
| 101 | + 'require-field-of-type-query-in-mutation-result': [1], |
| 102 | + }, |
| 103 | + }, |
| 104 | + { |
| 105 | + in: { |
| 106 | + 'input-name': [ |
| 107 | + 2, |
| 108 | + { |
| 109 | + checkQueries: false, |
| 110 | + checkInputType: true, |
| 111 | + checkMutations: false, |
| 112 | + caseSensitiveInputType: true, |
| 113 | + }, |
| 114 | + ], |
| 115 | + 'relay-arguments': [2, { includeBoth: true }], |
| 116 | + 'relay-page-info': [2], |
| 117 | + 'relay-edge-types': [ |
| 118 | + 2, |
| 119 | + { withEdgeSuffix: true, shouldImplementNode: true, listTypeCanWrapOnlyEdgeType: false }, |
| 120 | + ], |
| 121 | + 'naming-convention': [ |
| 122 | + 1, |
| 123 | + { types: 'PascalCase', FieldDefinition: 'camelCase', allowLeadingUnderscore: true }, |
| 124 | + ], |
| 125 | + 'no-typename-prefix': [1], |
| 126 | + 'strict-id-in-types': [ |
| 127 | + 1, |
| 128 | + { |
| 129 | + exceptions: { |
| 130 | + types: ['Aggregate', 'PageInfo', 'Color', 'Location', 'RGBA', 'RichText'], |
| 131 | + suffixes: ['Connection', 'Edge'], |
| 132 | + }, |
| 133 | + acceptedIdNames: ['id'], |
| 134 | + acceptedIdTypes: ['ID'], |
| 135 | + }, |
| 136 | + ], |
| 137 | + 'no-hashtag-description': [1], |
| 138 | + 'relay-connection-types': [2], |
| 139 | + 'unique-enum-value-names': [1], |
| 140 | + }, |
| 141 | + out: { |
| 142 | + 'input-name': [ |
| 143 | + 2, |
| 144 | + { |
| 145 | + checkQueries: false, |
| 146 | + checkInputType: true, |
| 147 | + checkMutations: false, |
| 148 | + caseSensitiveInputType: true, |
| 149 | + }, |
| 150 | + ], |
| 151 | + 'relay-arguments': [2, { includeBoth: true }], |
| 152 | + 'relay-page-info': [2], |
| 153 | + 'relay-edge-types': [ |
| 154 | + 2, |
| 155 | + { withEdgeSuffix: true, shouldImplementNode: true, listTypeCanWrapOnlyEdgeType: false }, |
| 156 | + ], |
| 157 | + 'naming-convention': [ |
| 158 | + 1, |
| 159 | + { types: 'PascalCase', FieldDefinition: 'camelCase', allowLeadingUnderscore: true }, |
| 160 | + ], |
| 161 | + 'no-typename-prefix': [1], |
| 162 | + 'strict-id-in-types': [ |
| 163 | + 1, |
| 164 | + { |
| 165 | + exceptions: { |
| 166 | + types: ['Aggregate', 'PageInfo', 'Color', 'Location', 'RGBA', 'RichText'], |
| 167 | + suffixes: ['Connection', 'Edge'], |
| 168 | + }, |
| 169 | + acceptedIdNames: ['id'], |
| 170 | + acceptedIdTypes: ['ID'], |
| 171 | + }, |
| 172 | + ], |
| 173 | + 'no-hashtag-description': [1], |
| 174 | + 'relay-connection-types': [2], |
| 175 | + 'unique-enum-value-names': [1], |
| 176 | + }, |
| 177 | + }, |
| 178 | + { |
| 179 | + in: { |
| 180 | + 'require-description': [ |
| 181 | + 1, |
| 182 | + { |
| 183 | + types: true, |
| 184 | + rootField: false, |
| 185 | + FieldDefinition: false, |
| 186 | + EnumTypeDefinition: false, |
| 187 | + DirectiveDefinition: false, |
| 188 | + OperationDefinition: false, |
| 189 | + InputValueDefinition: false, |
| 190 | + ScalarTypeDefinition: false, |
| 191 | + InputObjectTypeDefinition: false, |
| 192 | + }, |
| 193 | + ], |
| 194 | + 'require-deprecation-reason': [1], |
| 195 | + }, |
| 196 | + out: { |
| 197 | + 'require-description': [ |
| 198 | + 1, |
| 199 | + { |
| 200 | + types: true, |
| 201 | + FieldDefinition: false, |
| 202 | + EnumTypeDefinition: false, |
| 203 | + DirectiveDefinition: false, |
| 204 | + OperationDefinition: false, |
| 205 | + InputValueDefinition: false, |
| 206 | + ScalarTypeDefinition: false, |
| 207 | + InputObjectTypeDefinition: false, |
| 208 | + }, |
| 209 | + ], |
| 210 | + 'require-deprecation-reason': [1], |
| 211 | + }, |
| 212 | + }, |
| 213 | + { |
| 214 | + in: { 'require-description': [1, { types: false }] }, |
| 215 | + out: { 'require-description': [1, {}] }, |
| 216 | + }, |
| 217 | +]; |
| 218 | + |
| 219 | +await describe('migration: policy upgrade: graphql-eslint v3 -> v4', async () => { |
| 220 | + for (const [index, testCase] of TEST_CASES.entries()) { |
| 221 | + await test('should migrate all known breaking changes, sample ' + index, async () => { |
| 222 | + const { db, runTo, complete, done, seed, connectionString } = |
| 223 | + await initMigrationTestingEnvironment(); |
| 224 | + const storage = await createStorage(connectionString, 1); |
| 225 | + try { |
| 226 | + // Run migrations all the way to the point before the one we are testing |
| 227 | + await runTo('2025.03.20T00-00-00.dangerous_breaking.ts'); |
| 228 | + |
| 229 | + // Seed the database with some data (schema_sdl, supergraph_sdl, composite_schema_sdl) |
| 230 | + const admin = await seed.user({ |
| 231 | + user: { |
| 232 | + name: 'test' + index, |
| 233 | + email: `test_${Date.now()}@test.com`, |
| 234 | + }, |
| 235 | + }); |
| 236 | + const organization = await seed.organization({ |
| 237 | + organization: { |
| 238 | + name: `org-${Date.now()}`, |
| 239 | + }, |
| 240 | + user: admin, |
| 241 | + }); |
| 242 | + |
| 243 | + // Create an invitation to simulate a pending invitation |
| 244 | + await db.query(sql` |
| 245 | + INSERT INTO "schema_policy_config" ("resource_type", "resource_id", "config") VALUES ('ORGANIZATION', ${organization.id}, ${sql.jsonb(testCase.in)}); |
| 246 | + `); |
| 247 | + |
| 248 | + // run the next migrations |
| 249 | + await runTo('2025.03.26T00-00-00.graphql-eslint.v4.ts'); |
| 250 | + |
| 251 | + // assert scopes are still in place and identical |
| 252 | + const newRecord = await db.oneFirst(sql` |
| 253 | + SELECT config FROM schema_policy_config WHERE resource_id = ${organization.id}`); |
| 254 | + |
| 255 | + assert.deepStrictEqual(newRecord, testCase.out); |
| 256 | + |
| 257 | + await complete(); |
| 258 | + } finally { |
| 259 | + await done(); |
| 260 | + await storage.destroy(); |
| 261 | + } |
| 262 | + }); |
| 263 | + } |
| 264 | +}); |
0 commit comments