|
1 | 1 | import { Graph } from 'graphlib'; |
2 | | -import { buildSchema, Kind, ObjectTypeDefinitionNode, parse, print } from 'graphql'; |
| 2 | +import { |
| 3 | + buildClientSchema, |
| 4 | + buildSchema, |
| 5 | + introspectionFromSchema, |
| 6 | + Kind, |
| 7 | + ObjectTypeDefinitionNode, |
| 8 | + parse, |
| 9 | + print, |
| 10 | +} from 'graphql'; |
3 | 11 | import dedent from 'ts-dedent'; |
4 | 12 |
|
5 | | -import { ObjectTypeDefinitionBuilder, topologicalSortAST, topsort } from '../src/graphql'; |
| 13 | +import { isGeneratedByIntrospection, ObjectTypeDefinitionBuilder, topologicalSortAST, topsort } from '../src/graphql'; |
6 | 14 |
|
7 | 15 | describe('graphql', () => { |
8 | 16 | describe('ObjectTypeDefinitionBuilder', () => { |
@@ -238,3 +246,53 @@ describe('topologicalSortAST', () => { |
238 | 246 | expect(sortedSchema).toBe(expectedSortedSchema); |
239 | 247 | }); |
240 | 248 | }); |
| 249 | + |
| 250 | +describe('isGeneratedByIntrospection function', () => { |
| 251 | + const schemaDefinition = /* GraphQL */ ` |
| 252 | + scalar CustomScalar |
| 253 | +
|
| 254 | + interface Node { |
| 255 | + id: ID! |
| 256 | + } |
| 257 | +
|
| 258 | + type UserType implements Node { |
| 259 | + id: ID! |
| 260 | + name: String! |
| 261 | + email: String! |
| 262 | + } |
| 263 | +
|
| 264 | + union SearchResult = UserType |
| 265 | +
|
| 266 | + enum Role { |
| 267 | + ADMIN |
| 268 | + USER |
| 269 | + } |
| 270 | +
|
| 271 | + input UserInput { |
| 272 | + name: String! |
| 273 | + email: String! |
| 274 | + role: Role! |
| 275 | + } |
| 276 | +
|
| 277 | + type Query { |
| 278 | + user(id: ID!): UserType! |
| 279 | + search(text: String!): [SearchResult] |
| 280 | + } |
| 281 | +
|
| 282 | + type Mutation { |
| 283 | + createUser(input: UserInput!): UserType! |
| 284 | + } |
| 285 | + `; |
| 286 | + |
| 287 | + test('returns false for a schema not generated by introspection', () => { |
| 288 | + const schema = buildSchema(schemaDefinition); |
| 289 | + expect(isGeneratedByIntrospection(schema)).toBe(false); |
| 290 | + }); |
| 291 | + |
| 292 | + test('returns true for a schema generated by introspection', () => { |
| 293 | + const schema = buildSchema(schemaDefinition); |
| 294 | + const query = introspectionFromSchema(schema); |
| 295 | + const clientSchema = buildClientSchema(query); |
| 296 | + expect(isGeneratedByIntrospection(clientSchema)).toBe(true); |
| 297 | + }); |
| 298 | +}); |
0 commit comments