|
| 1 | +import { createContext, ensureContext } from '../src/index.js'; |
| 2 | + |
| 3 | +describe('Codegen config - Context', () => { |
| 4 | + it('loads and merge multiple schemas when using GraphQL config', async () => { |
| 5 | + const context = await createContext({ |
| 6 | + config: './packages/graphql-codegen-cli/tests/test-files/graphql.config.js', |
| 7 | + project: 'prj1', |
| 8 | + errorsOnly: true, |
| 9 | + overwrite: true, |
| 10 | + profile: true, |
| 11 | + require: [], |
| 12 | + silent: false, |
| 13 | + watch: false, |
| 14 | + }); |
| 15 | + |
| 16 | + const schema1 = /* GraphQL */ ` |
| 17 | + type Query |
| 18 | + scalar Date |
| 19 | + `; |
| 20 | + |
| 21 | + const schema2 = /* GraphQL */ ` |
| 22 | + extend type Query { |
| 23 | + me: User |
| 24 | + } |
| 25 | + type User { |
| 26 | + id: ID! |
| 27 | + name: String! |
| 28 | + createdAt: Date! |
| 29 | + } |
| 30 | + `; |
| 31 | + |
| 32 | + const schema3 = /* GraphQL */ ` |
| 33 | + extend type Query { |
| 34 | + media: Media |
| 35 | + } |
| 36 | + interface Media { |
| 37 | + id: ID! |
| 38 | + } |
| 39 | + type Image implements Media { |
| 40 | + id: ID! |
| 41 | + url: String! |
| 42 | + } |
| 43 | + type Book implements Media { |
| 44 | + id: ID! |
| 45 | + title: String! |
| 46 | + } |
| 47 | + `; |
| 48 | + |
| 49 | + const mergedSchema = await context.loadSchema([schema1, schema2, schema3]); |
| 50 | + |
| 51 | + const typeMap = mergedSchema.getTypeMap(); |
| 52 | + |
| 53 | + expect(typeMap['Query']).toBeDefined(); |
| 54 | + expect(typeMap['Date']).toBeDefined(); |
| 55 | + expect(typeMap['User']).toBeDefined(); |
| 56 | + expect(typeMap['Media']).toBeDefined(); |
| 57 | + expect(typeMap['Image']).toBeDefined(); |
| 58 | + expect(typeMap['Book']).toBeDefined(); |
| 59 | + }); |
| 60 | + |
| 61 | + it('loads and merge multiple schemas when using input config', async () => { |
| 62 | + const context = ensureContext({ |
| 63 | + generates: {}, |
| 64 | + }); |
| 65 | + |
| 66 | + const schema1 = /* GraphQL */ ` |
| 67 | + type Mutation |
| 68 | + scalar DateTime |
| 69 | + `; |
| 70 | + |
| 71 | + const schema2 = /* GraphQL */ ` |
| 72 | + extend type Mutation { |
| 73 | + createUser: User |
| 74 | + } |
| 75 | + type User { |
| 76 | + id: ID! |
| 77 | + createdAt: DateTime! |
| 78 | + } |
| 79 | + `; |
| 80 | + |
| 81 | + const mergedSchema = await context.loadSchema([schema1, schema2]); |
| 82 | + |
| 83 | + const typeMap = mergedSchema.getTypeMap(); |
| 84 | + |
| 85 | + expect(typeMap['Mutation']).toBeDefined(); |
| 86 | + expect(typeMap['DateTime']).toBeDefined(); |
| 87 | + expect(typeMap['User']).toBeDefined(); |
| 88 | + }); |
| 89 | +}); |
0 commit comments