From 24afabece51aee171f902776d3f59b4a17026c49 Mon Sep 17 00:00:00 2001 From: Arda TANRIKULU Date: Wed, 7 Sep 2022 00:38:09 +0300 Subject: [PATCH] Enable Automatic Type Merging for relevant handlers --- .changeset/honest-candles-act.md | 8 ++++ packages/handlers/mongoose/package.json | 1 - packages/handlers/mongoose/src/index.ts | 42 +++++++------------ .../handlers/mongoose/yaml-config.graphql | 4 -- packages/handlers/mysql/src/index.ts | 14 +++++++ packages/runtime/src/get-mesh.ts | 2 +- packages/runtime/src/types.ts | 2 - packages/types/src/index.ts | 1 + 8 files changed, 40 insertions(+), 34 deletions(-) create mode 100644 .changeset/honest-candles-act.md diff --git a/.changeset/honest-candles-act.md b/.changeset/honest-candles-act.md new file mode 100644 index 0000000000000..f663562cc8164 --- /dev/null +++ b/.changeset/honest-candles-act.md @@ -0,0 +1,8 @@ +--- +'@graphql-mesh/mongoose': patch +'@graphql-mesh/mysql': patch +'@graphql-mesh/runtime': patch +'@graphql-mesh/types': patch +--- + +Enable Automatic Type Merging by default diff --git a/packages/handlers/mongoose/package.json b/packages/handlers/mongoose/package.json index bc83a089832a2..1097529810be4 100644 --- a/packages/handlers/mongoose/package.json +++ b/packages/handlers/mongoose/package.json @@ -38,7 +38,6 @@ "graphql-compose-mongoose": "9.7.1", "graphql-compose-connection": "8.2.1", "graphql-compose-pagination": "8.3.0", - "@graphql-tools/stitching-directives": "2.3.8", "tslib": "^2.4.0" }, "publishConfig": { diff --git a/packages/handlers/mongoose/src/index.ts b/packages/handlers/mongoose/src/index.ts index 9776d4cfad6d3..07c8280e6cf32 100644 --- a/packages/handlers/mongoose/src/index.ts +++ b/packages/handlers/mongoose/src/index.ts @@ -4,7 +4,6 @@ import { MeshHandlerOptions, MeshPubSub, MeshHandler, MeshSource, YamlConfig, Im import { connect, disconnect, ConnectOptions, Document, Model } from 'mongoose'; import { loadFromModuleExportExpression } from '@graphql-mesh/utils'; import { specifiedDirectives } from 'graphql'; -import { stitchingDirectives } from '@graphql-tools/stitching-directives'; const modelQueryOperations = [ 'findById', @@ -53,6 +52,8 @@ export default class MongooseHandler implements MeshHandler { } const schemaComposer = new SchemaComposer(); + const typeMergingOptions: MeshSource['merge'] = {}; + await Promise.all([ Promise.all( this.config.models?.map(async modelConfig => { @@ -81,16 +82,13 @@ export default class MongooseHandler implements MeshHandler { ) ), ]); - if (this.config.autoTypeMerging) { - modelTC.setDirectiveByName('key', { - selectionSet: /* GraphQL */ ` - { - id - } - `, - }); - modelTC.setFieldDirectiveByName(`${modelConfig.name}_dataLoaderMany`, 'merge'); - } + const typeName = modelTC.getTypeName(); + typeMergingOptions[typeName] = { + selectionSet: `{ id }`, + key: ({ id }) => id, + argsFromKeys: ids => ({ ids }), + fieldName: `${typeName}_dataLoaderMany`, + }; }) || [] ), Promise.all( @@ -117,16 +115,13 @@ export default class MongooseHandler implements MeshHandler { ) ), ]); - if (this.config.autoTypeMerging) { - discriminatorTC.setDirectiveByName('key', { - selectionSet: /* GraphQL */ ` - { - id - } - `, - }); - discriminatorTC.setFieldDirectiveByName(`${discriminatorConfig.name}_dataLoaderMany`, 'merge'); - } + const typeName = discriminatorTC.getTypeName(); + typeMergingOptions[typeName] = { + selectionSet: `{ id }`, + key: ({ id }) => id, + argsFromKeys: ids => ({ ids }), + fieldName: `${typeName}_dataLoaderMany`, + }; }) || [] ), ]); @@ -134,11 +129,6 @@ export default class MongooseHandler implements MeshHandler { // graphql-compose doesn't add @defer and @stream to the schema specifiedDirectives.forEach(directive => schemaComposer.addDirective(directive)); - if (this.config.autoTypeMerging) { - const defaultStitchingDirectives = stitchingDirectives(); - defaultStitchingDirectives.allStitchingDirectives.forEach(directive => schemaComposer.addDirective(directive)); - } - const schema = schemaComposer.buildSchema(); return { diff --git a/packages/handlers/mongoose/yaml-config.graphql b/packages/handlers/mongoose/yaml-config.graphql index 9e708d6124477..1ae4f5f30bd31 100644 --- a/packages/handlers/mongoose/yaml-config.graphql +++ b/packages/handlers/mongoose/yaml-config.graphql @@ -6,10 +6,6 @@ type MongooseHandler @md { connectionString: String models: [MongooseModel] discriminators: [MongooseModel] - """ - Enable Automatic Type Merging/Federation support - """ - autoTypeMerging: Boolean } type MongooseModel { diff --git a/packages/handlers/mysql/src/index.ts b/packages/handlers/mysql/src/index.ts index abe1dba049ff9..7c6f2401665b2 100644 --- a/packages/handlers/mysql/src/index.ts +++ b/packages/handlers/mysql/src/index.ts @@ -216,6 +216,7 @@ export default class MySQLHandler implements MeshHandler { }); const tables = await introspectionConnection.getDatabaseTables(pool.config.connectionConfig.database); const tableNames = this.config.tables || Object.keys(tables); + const typeMergingOptions: MeshSource['merge'] = {}; await Promise.all( tableNames.map(async tableName => { if (this.config.tables && !this.config.tables.includes(tableName)) { @@ -429,6 +430,19 @@ export default class MySQLHandler implements MeshHandler { }); }) ); + typeMergingOptions[objectTypeName] = { + selectionSet: `{ ${[...primaryKeys].join(' ')} }`, + args: obj => { + const where = {}; + for (const primaryKey of primaryKeys) { + where[primaryKey] = obj[primaryKey]; + } + return { + where, + }; + }, + valuesFromResults: results => results[0], + }; schemaComposer.Query.addFields({ [tableName]: { type: '[' + objectTypeName + ']', diff --git a/packages/runtime/src/get-mesh.ts b/packages/runtime/src/get-mesh.ts index a0352b0a335db..588300de3de7c 100644 --- a/packages/runtime/src/get-mesh.ts +++ b/packages/runtime/src/get-mesh.ts @@ -193,7 +193,7 @@ export async function getMesh(options: GetMeshOptions): Promise { contextVariables: source.contextVariables || {}, handler: apiSource.handler, batch: 'batch' in source ? source.batch : true, - merge: apiSource.merge, + merge: source.merge, createProxyingResolver: createProxyingResolverFactory(apiName), }); } catch (e: any) { diff --git a/packages/runtime/src/types.ts b/packages/runtime/src/types.ts index 665bb393cf0ab..bd7ee1a65b038 100644 --- a/packages/runtime/src/types.ts +++ b/packages/runtime/src/types.ts @@ -12,7 +12,6 @@ import { import { DocumentNode, ExecutionResult } from 'graphql'; import { IResolvers, Source } from '@graphql-tools/utils'; import { MESH_CONTEXT_SYMBOL } from './constants'; -import { MergedTypeConfig } from '@graphql-tools/delegate'; import { MeshInstance } from './get-mesh'; import { envelop } from '@envelop/core'; @@ -35,7 +34,6 @@ export type MeshResolvedSource = { name: string; handler: MeshHandler; transforms?: MeshTransform[]; - merge?: Record; }; export type ExecuteMeshFn = ( diff --git a/packages/types/src/index.ts b/packages/types/src/index.ts index 9230f96e5641a..53b4462f79451 100644 --- a/packages/types/src/index.ts +++ b/packages/types/src/index.ts @@ -25,6 +25,7 @@ export type MeshSource = { executor?: Executor; contextVariables?: Record; batch?: boolean; + merge?: Record; }; export interface KeyValueCacheSetOptions {