|
2 | 2 | * Endpoint Schema Source |
3 | 3 | * |
4 | 4 | * Fetches GraphQL schema via introspection from a live endpoint. |
5 | | - * Wraps the existing fetchSchema() function with the SchemaSource interface. |
| 5 | + * Optionally fetches _meta query in parallel for M:N junction key metadata. |
6 | 6 | */ |
7 | | -import { fetchSchema } from '../fetch-schema'; |
8 | | -import type { SchemaSource, SchemaSourceResult } from './types'; |
| 7 | +import { fetchSchema, fetchGraphqlQuery } from '../fetch-schema'; |
| 8 | +import type { MetaTableInfo, SchemaSource, SchemaSourceResult } from './types'; |
9 | 9 | import { SchemaSourceError } from './types'; |
10 | 10 |
|
| 11 | +/** |
| 12 | + * _meta GraphQL query — fetches M:N junction key metadata. |
| 13 | + * Only the fields needed for enriching CleanManyToManyRelation are selected. |
| 14 | + */ |
| 15 | +const META_QUERY = `{ |
| 16 | + _meta { |
| 17 | + tables { |
| 18 | + name |
| 19 | + schemaName |
| 20 | + relations { |
| 21 | + manyToMany { |
| 22 | + fieldName |
| 23 | + type |
| 24 | + junctionTable { name } |
| 25 | + junctionLeftKeyAttributes { name } |
| 26 | + junctionRightKeyAttributes { name } |
| 27 | + leftKeyAttributes { name } |
| 28 | + rightKeyAttributes { name } |
| 29 | + rightTable { name } |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | +}`; |
| 35 | + |
| 36 | +interface MetaQueryResponse { |
| 37 | + _meta: { |
| 38 | + tables: MetaTableInfo[]; |
| 39 | + }; |
| 40 | +} |
| 41 | + |
11 | 42 | export interface EndpointSchemaSourceOptions { |
12 | 43 | /** |
13 | 44 | * GraphQL endpoint URL |
@@ -41,30 +72,45 @@ export class EndpointSchemaSource implements SchemaSource { |
41 | 72 | } |
42 | 73 |
|
43 | 74 | async fetch(): Promise<SchemaSourceResult> { |
44 | | - const result = await fetchSchema({ |
| 75 | + const fetchOpts = { |
45 | 76 | endpoint: this.options.endpoint, |
46 | 77 | authorization: this.options.authorization, |
47 | 78 | headers: this.options.headers, |
48 | 79 | timeout: this.options.timeout, |
49 | | - }); |
| 80 | + }; |
| 81 | + |
| 82 | + // Run introspection and _meta query in parallel. |
| 83 | + // _meta is best-effort: if the endpoint doesn't expose it, we proceed without. |
| 84 | + const [introspectionResult, metaResult] = await Promise.all([ |
| 85 | + fetchSchema(fetchOpts), |
| 86 | + fetchGraphqlQuery<MetaQueryResponse>({ ...fetchOpts, query: META_QUERY }) |
| 87 | + .catch((): null => null), |
| 88 | + ]); |
50 | 89 |
|
51 | | - if (!result.success) { |
| 90 | + if (!introspectionResult.success) { |
52 | 91 | throw new SchemaSourceError( |
53 | | - result.error ?? 'Unknown error fetching schema', |
| 92 | + introspectionResult.error ?? 'Unknown error fetching schema', |
54 | 93 | this.describe(), |
55 | 94 | ); |
56 | 95 | } |
57 | 96 |
|
58 | | - if (!result.data) { |
| 97 | + if (!introspectionResult.data) { |
59 | 98 | throw new SchemaSourceError( |
60 | 99 | 'No introspection data returned', |
61 | 100 | this.describe(), |
62 | 101 | ); |
63 | 102 | } |
64 | 103 |
|
65 | | - return { |
66 | | - introspection: result.data, |
| 104 | + const result: SchemaSourceResult = { |
| 105 | + introspection: introspectionResult.data, |
67 | 106 | }; |
| 107 | + |
| 108 | + // Attach _meta data if available |
| 109 | + if (metaResult?.success && metaResult.data?._meta?.tables) { |
| 110 | + result.tablesMeta = metaResult.data._meta.tables; |
| 111 | + } |
| 112 | + |
| 113 | + return result; |
68 | 114 | } |
69 | 115 |
|
70 | 116 | describe(): string { |
|
0 commit comments