Skip to content

Commit e2887f2

Browse files
authored
fix(mysql): Propagate tables and tableNames options to loader (#8665)
* fix(mysql): Propagate tables and tableNames options to loadGraphQLSchemaFromMySQL - The `tables` and `tableNames` config options are defined in types and used inside `loadGraphQLSchemaFromMySQL` from `@omnigraph/mysql`, but they were not connected * fix(mysql): Utilize tableFieldsConfig options - The `handleTableName` already expects `tableFieldsConfig` to be passed. Hence, simply passing it down * test(mysql): Add comprehensive tests for mysql schema loader * test(mysql): Add basic test for legacy mysql handler * chore(changeset): Add changeset for mysql loader fix
1 parent 06966e8 commit e2887f2

File tree

6 files changed

+1973
-0
lines changed

6 files changed

+1973
-0
lines changed

.changeset/some-spoons-lead.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@graphql-mesh/mysql': patch
3+
'@omnigraph/mysql': patch
4+
---
5+
6+
Fix tables and tableFields config options inside mysql handler and schema loader

packages/legacy/handlers/mysql/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ export default class MySQLHandler implements MeshHandler {
6464
}
6565
return loadGraphQLSchemaFromMySQL(this.name, {
6666
endpoint: endpointUrl.toString(),
67+
tables: this.config.tables,
68+
tableFields: this.config.tableFields,
6769
ssl: {
6870
rejectUnauthorized: this.config.ssl?.rejectUnauthorized,
6971
caPath: this.config.ssl?.ca,
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import type { GraphQLSchema } from 'graphql';
2+
import InMemoryLRUCache from '@graphql-mesh/cache-inmemory-lru';
3+
import MySQLHandler from '@graphql-mesh/mysql';
4+
import { InMemoryStoreStorageAdapter, MeshStore } from '@graphql-mesh/store';
5+
import type { YamlConfig } from '@graphql-mesh/types';
6+
import { loadFromModuleExportExpression } from '@graphql-mesh/utils';
7+
import { loadGraphQLSchemaFromMySQL, type LoadGraphQLSchemaFromMySQLOpts } from '@omnigraph/mysql';
8+
9+
jest.mock('@graphql-mesh/utils');
10+
jest.mock('@omnigraph/mysql');
11+
12+
const noOpFunction: any = () => {};
13+
14+
describe('mysql', () => {
15+
let store: MeshStore;
16+
using cache = new InMemoryLRUCache();
17+
beforeEach(() => {
18+
store = new MeshStore('.mesh', new InMemoryStoreStorageAdapter(), {
19+
readonly: false,
20+
validate: false,
21+
});
22+
});
23+
24+
it('correctly builds schema from loadGraphQLSchemaFromMySQL after passing configuration', async () => {
25+
const graphQLSchemaMock: GraphQLSchema = {
26+
description: 'someGraphQLSchemaMock',
27+
} as any;
28+
29+
const configMock: YamlConfig.MySQLHandler = {
30+
database: 'testdb',
31+
tables: ['testTable'],
32+
tableFields: [
33+
{
34+
table: 'testTable',
35+
fields: ['testField'],
36+
},
37+
],
38+
ssl: {
39+
rejectUnauthorized: true,
40+
ca: 'someCA',
41+
},
42+
};
43+
44+
jest.mocked(loadGraphQLSchemaFromMySQL).mockResolvedValue(graphQLSchemaMock);
45+
46+
const handler = new MySQLHandler({
47+
name: 'test',
48+
config: configMock,
49+
baseDir: __dirname,
50+
cache,
51+
pubsub: noOpFunction,
52+
store,
53+
importFn: noOpFunction,
54+
logger: noOpFunction,
55+
});
56+
57+
const expectedOptions: LoadGraphQLSchemaFromMySQLOpts = {
58+
endpoint: 'mysqls://localhost:3306/testdb',
59+
ssl: {
60+
rejectUnauthorized: configMock.ssl!.rejectUnauthorized,
61+
caPath: configMock.ssl!.ca,
62+
},
63+
tables: configMock.tables,
64+
tableFields: configMock.tableFields,
65+
};
66+
67+
const { schema } = await handler.getMeshSource();
68+
69+
expect(schema).toBe(graphQLSchemaMock);
70+
expect(loadGraphQLSchemaFromMySQL).toHaveBeenCalledWith('test', expectedOptions);
71+
});
72+
});

packages/loaders/mysql/src/schema.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export interface LoadGraphQLSchemaFromMySQLOpts {
3636
endpoint: string;
3737
ssl?: MySQLSSLOptions;
3838
tables?: string[];
39+
tableFields?: TableFieldConfig[];
3940
}
4041

4142
export async function loadGraphQLSchemaFromMySQL(
@@ -97,6 +98,7 @@ export async function loadGraphQLSchemaFromMySQL(
9798
subgraphName,
9899
tableName,
99100
tables,
101+
tableFieldsConfig: opts.tableFields,
100102
schemaComposer,
101103
introspectionConnection,
102104
autoIncrementedColumns,

0 commit comments

Comments
 (0)