Skip to content

Commit

Permalink
Merge pull request #19 from BouyguesTelecom/feat/handle-multi-version…
Browse files Browse the repository at this point in the history
…ing-with-suffix

✨ Handle multi-versioning
  • Loading branch information
arnaud authored Jun 24, 2024
2 parents 5a52d17 + a975e19 commit a093752
Show file tree
Hide file tree
Showing 18 changed files with 655 additions and 405 deletions.
32 changes: 20 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion packages/graphql-mesh/directives/prefixSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { GraphQLSchema } from 'graphql'
import { mapSchema } from '@graphql-tools/utils'
import { MapperKind } from '@graphql-tools/utils'
import { getDirective } from '@graphql-tools/utils'
import { GraphQLObjectType } from 'graphql'
import { GraphQLObjectType, GraphQLInterfaceType } from 'graphql'
import { MeshTransform } from '@graphql-mesh/types'

export default class PrefixSchemaDirectiveTransform implements MeshTransform {
Expand All @@ -15,6 +15,14 @@ export default class PrefixSchemaDirectiveTransform implements MeshTransform {
const config = type.toConfig()
return new GraphQLObjectType(config)
}
},
[MapperKind.INTERFACE_TYPE]: type => {
const prefixDirective = getDirective(schema, type, 'prefixSchema')?.[0]
if (prefixDirective) {
type.name = prefixDirective.prefix + type.name
const config = type.toConfig()
return new GraphQLInterfaceType(config)
}
}
})
}
Expand Down
32 changes: 20 additions & 12 deletions packages/graphql-mesh/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/graphql-mesh/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"@graphql-mesh/openapi": "^0.97.4",
"@graphql-mesh/transform-filter-schema": "^0.96.2",
"@graphql-mesh/transform-naming-convention": "^0.96.2",
"@graphql-mesh/transform-prefix": "^0.96.2",
"@graphql-mesh/transform-rename": "^0.96.2",
"@graphql-mesh/transform-type-merging": "^0.96.2",
"@graphql-tools/schema": "^10.0.2",
"@graphql-tools/utils": "^10.0.12",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
diff --git a/node_modules/@graphql-mesh/transform-rename/cjs/bareRename.js b/node_modules/@graphql-mesh/transform-rename/cjs/bareRename.js
index 7e430cf..76a818e 100644
--- a/node_modules/@graphql-mesh/transform-rename/cjs/bareRename.js
+++ b/node_modules/@graphql-mesh/transform-rename/cjs/bareRename.js
@@ -84,6 +84,9 @@ class BareRename {
if (!mapKeys?.length)
return null;
return mapKeys.reduce((newName, mapKey) => {
+ if (!newName) {
+ return null
+ }
if (mapKeyIsString) {
const str = map.get(mapKey);
// avoid re-iterating over strings that have already been renamed
74 changes: 48 additions & 26 deletions packages/graphql-mesh/patches/@graphql-tools+stitch+9.0.3.patch
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ index 5e752ad..3b487f5 100644
const currentNamedType = (0, graphql_1.getNamedType)(c.type);
if (finalNamedType.toString() !== currentNamedType.toString()) {
diff --git a/node_modules/@graphql-tools/stitch/cjs/typeCandidates.js b/node_modules/@graphql-tools/stitch/cjs/typeCandidates.js
index c915942..6f02609 100644
old mode 100644
new mode 100755
index c915942..fd09fc4
--- a/node_modules/@graphql-tools/stitch/cjs/typeCandidates.js
+++ b/node_modules/@graphql-tools/stitch/cjs/typeCandidates.js
@@ -119,6 +119,25 @@ function buildTypes({ typeCandidates, directives, stitchingInfo, rootTypeNames,
@@ -119,6 +119,29 @@ function buildTypes({ typeCandidates, directives, stitchingInfo, rootTypeNames,
(typeof mergeTypes === 'function' && mergeTypes(typeCandidates[typeName], typeName)) ||
(Array.isArray(mergeTypes) && mergeTypes.includes(typeName)) ||
(stitchingInfo != null && typeName in stitchingInfo.mergedTypes)) {
Expand All @@ -29,7 +31,11 @@ index c915942..6f02609 100644
+ let finalI = candidatesI[0];
+ const otherCandidates = candidatesI.slice(1, candidatesI.length).concat(candidatesObj);
+ otherCandidates.forEach((otherCandidate) => {
+ finalI.type._fields = { ...finalI.type._fields, ...otherCandidate.type._fields };
+ Object.keys(otherCandidate.type._fields).forEach(field => {
+ if (!Object.keys(finalI.type._fields).includes(field)) {
+ finalI.type._fields[field] = otherCandidate.type._fields[field];
+ }
+ })
+ });
+ typeCandidates[typeName] = [finalI];
+ }
Expand All @@ -41,7 +47,7 @@ index c915942..6f02609 100644
typeMap[typeName] = (0, mergeCandidates_js_1.mergeCandidates)(typeName, typeCandidates[typeName], typeMergingOptions);
}
else {
@@ -128,6 +147,42 @@ function buildTypes({ typeCandidates, directives, stitchingInfo, rootTypeNames,
@@ -128,6 +151,58 @@ function buildTypes({ typeCandidates, directives, stitchingInfo, rootTypeNames,
typeMap[typeName] = candidateSelector(typeCandidates[typeName]).type;
}
}
Expand All @@ -53,31 +59,47 @@ index c915942..6f02609 100644
+ */
+ Object.values(typeMap).forEach((type) => {
+ if (type.constructor.name === "GraphQLObjectType") {
+ if (type.astNode.interfaces.length !== 0) {
+ type._interfaces = [];
+ type.astNode.interfaces.forEach((i) => {
+ const typeI = typeMap[i.name.value]
+ type._interfaces.push(typeI);
+ type._fields = {...type.getFields(), ...typeI.getFields()}
+ });
+ }
+ if (type.astNode.interfaces.length >= 2) {
+ const firstFields = type._interfaces[0]._fields
+ type._interfaces.slice(1, type._interfaces.length).forEach((i) => {
+ Object.keys(i._fields).forEach((field) => {
+ if (field in firstFields) {
+ if (i._fields[field].type.constructor.name !== firstFields[field].type.constructor.name ||
+ i._fields[field].type.constructor.name === "GraphQLObjectType") {
+ i._fields[field] = firstFields[field]
+ type._fields[field] = firstFields[field]
+ }
+ }
+ else {
+ firstFields[field] = i._fields[field]
+ }
+ const typeInterfaces = type.getInterfaces()
+
+ if (typeInterfaces.length !== 0) {
+ type._fields = type.getFields()
+
+ typeInterfaces.forEach(i => {
+ const iFields = typeMap[i.name].getFields()
+ Object.keys(iFields).forEach(keyName => {
+ type._fields[keyName] = iFields[keyName]
+ })
+ })
+ }
+
+ if (typeInterfaces.length >= 2) {
+ let uniqueFields = {}
+ let duplicateFields = {}
+
+ typeInterfaces.forEach(i => {
+ const iFields = typeMap[i.name].getFields()
+ Object.keys(iFields).forEach(keyName => {
+ if (uniqueFields[keyName] === undefined) {
+ uniqueFields[keyName] = "defined"
+ }
+ else {
+ duplicateFields[keyName] = iFields[keyName]
+ }
+ })
+ })
+ Object.keys(duplicateFields).forEach(field => {
+ if (type.getFields()[field] !== undefined) {
+ type._fields[field] = duplicateFields[field]
+ }
+ typeInterfaces.forEach(i => {
+ const iFields = typeMap[i.name].getFields()
+
+ if (iFields[field] !== undefined) {
+ typeMap[i.name]._fields[field] = duplicateFields[field]
+ }
+ })
+ })
+ }
+ }
+ });
+
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql-mesh/scripts/build-local-image.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ cp -r ../../patches/* ./patches
# build the docker image
docker build -t graphql-mesh .
# Remove the patches from the docker context
rm -rf patches/@graphql-tools+batch-execute+*.patch patches/@graphql-tools+executor+*.patch patches/graphql+*.patch
rm -rf patches/@graphql-tools+batch-execute+*.patch patches/@graphql-tools+executor+*.patch patches/graphql+*.patch patches/@graphql-tools+merge+*.patch

9 changes: 2 additions & 7 deletions packages/graphql-mesh/scripts/download-sources.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { readFileOrUrl, DefaultLogger } from '@graphql-mesh/utils'
import { getConfig } from '../utils/config'
import { getConfig } from '../utils/parseYamlConfig'
import { writeFileSync, existsSync, mkdirSync } from 'node:fs'
import { fetch } from '@whatwg-node/fetch'
const logger = new DefaultLogger()
Expand All @@ -10,17 +10,12 @@ const config = getConfig()
const sources = config?.sources?.filter((source) => source?.handler?.openapi) || []
const swaggers = sources.map((source) => source?.handler?.openapi?.source) || []

/**
* Get the name of generated from the source object
* @param {Record<string, unknown>} source
* @returns {string | undefined}
*/
const getFileName = (url: string): string | undefined => {
return sources.find((source) => source?.handler?.openapi?.source === url)?.name
}

/**
* Download the swagger from the given URL and save it to the sources folder
* Download one swagger from the given URL and save it to the sources folder
* @param {string} url
*/
const downSwaggerFromUrl = async (url: string | undefined, index: string): Promise<void> => {
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql-mesh/serve.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createServer } from 'node:http'
import { createBuiltMeshHTTPHandler } from './.mesh'
import { getConfig } from './utils/config'
import { getConfig } from './utils/parseYamlConfig'
const config = getConfig()

const PORT = config.serve?.port ?? 4000
Expand Down
Loading

0 comments on commit a093752

Please sign in to comment.