Skip to content

Commit

Permalink
✨ add possibility to overrides docker image with plugins and transforms
Browse files Browse the repository at this point in the history
  • Loading branch information
Mbaye THIAM committed Apr 5, 2024
1 parent 2cb5186 commit ad90e45
Show file tree
Hide file tree
Showing 23 changed files with 314 additions and 48 deletions.
38 changes: 35 additions & 3 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"clean:modules": "npm exec --workspaces -- npx rimraf node_modules && npx rimraf node_modules",
"generate:mesh:lock": "cd packages/graphql-mesh && npm i --package-lock-only --workspaces=false",
"postinstall": "npm run postinstall -w graphql-mesh && patch-package && npm run generate:mesh:lock",
"preinstall": "concurrently \"npm run pack -w directive-spl\" \"npm run pack -w directive-headers\" \"npm run pack -w directive-no-auth\"",
"build:local:packages": "concurrently \"npm run pack -w directive-spl\" \"npm run pack -w directive-headers\" \"npm run pack -w directive-no-auth\" \"npm run pack -w inject-additional-transforms\"",
"preinstall": "npm run build:local:packages",
"start": "npm start -w graphql-mesh"
},
"devDependencies": {
Expand Down
14 changes: 4 additions & 10 deletions packages/directive-no-auth/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ export default class NoAuthDirectiveTransform implements MeshTransform {
const originalResolver =
fieldConfig.resolve != null ? fieldConfig.resolve : defaultFieldResolver

const resolver = async (next: any , _source: any, _args: any, context: any, info: any) => {
const resolver = async (next: any, _source: any, _args: any, context: any, info: any) => {
const { directives } = info.fieldNodes[0]
const upperDirective = directives.find((directive: { name: { value: string } }) => directive.name.value === 'upper')
const noAuthDirective = directives.find((directive: { name: { value: string } }) => directive.name.value === 'noAuth')
const noAuthDirective = directives.find(
(directive: { name: { value: string } }) => directive.name.value === 'noAuth'
)

/**
* In order to set headers for the request, we need override authorization headers
Expand All @@ -25,13 +26,6 @@ export default class NoAuthDirectiveTransform implements MeshTransform {
}
let result = await next(context)


if (upperDirective) {
if (typeof result === 'string') {
result = result.toUpperCase()
}
}

return result
}

Expand Down
18 changes: 13 additions & 5 deletions packages/graphql-mesh/.meshrc.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
import type { Config } from '@graphql-mesh/types/typings/config'
import { YamlConfig } from '@graphql-mesh/types'
import ConfigFromSwaggers from './utils/ConfigFromSwaggers'

const configFromSwaggers = new ConfigFromSwaggers()
const { defaultConfig, additionalResolvers, additionalTypeDefs, sources } =
const { defaultConfig, additionalTypeDefs, sources } =
configFromSwaggers.getMeshConfigFromSwaggers()

const config = <Config>{
const config = <YamlConfig.Config>{
...defaultConfig,
transforms: [
{ 'directive-spl': {} },
{ 'directive-headers': {} },
{ 'directive-no-auth': {} },
{
'inject-additional-transforms': {
additionalTransforms: defaultConfig.additionalTransforms || []
}
},
...(defaultConfig.transforms || [])
],
sources: [...sources],
additionalTypeDefs: [...(defaultConfig.additionalTypeDefs || []), ...additionalTypeDefs],
additionalResolvers: [...(defaultConfig.additionalResolvers || []), additionalResolvers]
additionalTypeDefs: [defaultConfig.additionalTypeDefs || '', ...additionalTypeDefs],
additionalResolvers: [
...(defaultConfig.additionalResolvers || []),
'./utils/additionalResolvers.ts'
]
}

export default config
3 changes: 2 additions & 1 deletion packages/graphql-mesh/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ WORKDIR /build
COPY package*.json ./
ADD local-pkg ./local-pkg
ADD patches ./patches
ADD ../../patches ./patches
RUN npm ci --omit=dev --omit=optional --omit=peer
RUN rm -rf node_modules/uWebSockets.js/*.node
RUN cd node_modules/typescript/lib && rm -rf cs de es fr it ja ko pl pt-br ru tr zh-cn zh-tw
Expand All @@ -30,5 +29,7 @@ ENV TS_NODE_TRANSPILE_ONLY=true
# swaggers sources
VOLUME /app/sources
VOLUME /app/config.yaml
VOLUME /app/transforms
VOLUME /app/plugins

CMD [ "npm", "run", "serve" ]
Empty file.
Binary file modified packages/graphql-mesh/local-pkg/directive-no-auth-1.0.0.tgz
Binary file not shown.
14 changes: 14 additions & 0 deletions packages/graphql-mesh/package-lock.json

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

8 changes: 5 additions & 3 deletions packages/graphql-mesh/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"scripts": {
"build:local:image": "./scripts/build-local-image.sh",
"build": "npm run downloadswaggers && mesh build",
"downloadswaggers": "NODE_TLS_REJECT_UNAUTHORIZED='0' sucrase-node ./scripts/download-sources.ts",
"postinstall": "patch-package",
Expand All @@ -18,12 +19,13 @@
"@graphql-mesh/transform-type-merging": "^0.96.2",
"@graphql-tools/schema": "^10.0.2",
"@graphql-tools/utils": "^10.0.12",
"glob": "^10.3.10",
"graphql": "^16.8.1",
"directive-headers": "file:./local-pkg/directive-headers-1.0.0.tgz",
"directive-no-auth": "file:./local-pkg/directive-no-auth-1.0.0.tgz",
"patch-package": "^8.0.0",
"directive-spl": "file:./local-pkg/directive-spl-1.0.0.tgz",
"glob": "^10.3.10",
"graphql": "^16.8.1",
"inject-additional-transforms": "file:./local-pkg/inject-additional-transforms-1.0.0.tgz",
"patch-package": "^8.0.0",
"sucrase": "^3.35.0"
},
"devDependencies": {
Expand Down
11 changes: 11 additions & 0 deletions packages/graphql-mesh/scripts/build-local-image.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh

set -e

# Copy parent patches to the docker context
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+9.0.2.patch patches/@graphql-tools+executor+1.2.0.patch patches/graphql+16.8.1.patch

2 changes: 1 addition & 1 deletion packages/graphql-mesh/serve.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createServer } from 'node:http'
import { createBuiltMeshHTTPHandler } from './.mesh'
import { getConfig } from './utils/config'
const config = getConfig() || {}
const config = getConfig()

const PORT = config.serve?.port ?? 4000
const HOSTNAME = config.serve?.hostname ?? 'http://0.0.0.0'
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql-mesh/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"target": "ES2022",
"module": "NodeNext",
"sourceMap": true,
"outDir": "dist"
"outDir": ".mesh"
},
"exclude": ["node_modules"]
}
16 changes: 2 additions & 14 deletions packages/graphql-mesh/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,11 @@
import { type OpenAPIV3 } from 'openapi-types'

import {
AdditionalStitchingResolverObject,
AdditionalStitchingBatchResolverObject,
AdditionalSubscriptionObject
} from '@graphql-mesh/types/typings/config'

type Resolvers =
| string
| AdditionalStitchingResolverObject
| AdditionalStitchingBatchResolverObject
| AdditionalSubscriptionObject
| {}
import { YamlConfig } from '@graphql-mesh/types'

type SwaggerName = string
type Spec = OpenAPIV3.Document
type Path = keyof Spec['paths']
type OperationId = string

type Resolvers = YamlConfig.Config['additionalResolvers'] | {}
/**
* Catalog of all operations
*/
Expand Down
6 changes: 6 additions & 0 deletions packages/graphql-mesh/utils/additionalResolvers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import ConfigFromSwaggers from './ConfigFromSwaggers'

const configFromSwaggers = new ConfigFromSwaggers()
const { additionalResolvers } = configFromSwaggers.getMeshConfigFromSwaggers()

export default additionalResolvers
11 changes: 7 additions & 4 deletions packages/graphql-mesh/utils/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Config } from '@graphql-mesh/types/typings/config'
import { YamlConfig } from '@graphql-mesh/types'
import { DefaultLogger } from '@graphql-mesh/utils'
import { load } from 'js-yaml'
import { readFileSync } from 'node:fs'
Expand All @@ -9,9 +9,9 @@ const logger = new DefaultLogger()
* Load config file from yaml or ts
* @returns Config
*/
export const getConfig = (): Config => {
export const getConfig = (): YamlConfig.Config => {
logger.info('Loading config file')
let config: Config
let config: YamlConfig.Config
// Load yaml config file
try {
const configPath = resolve('./config.yaml')
Expand Down Expand Up @@ -39,7 +39,10 @@ export const getConfig = (): Config => {
* @param config
* @returns
*/
export const getSourceOpenapiEnpoint = (source: string, config: Config): string | undefined => {
export const getSourceOpenapiEnpoint = (
source: string,
config: YamlConfig.Config
): string | undefined => {
const data = config.sources?.find((item) =>
item?.handler?.openapi?.source?.includes(source.split('/').pop())
)
Expand Down
5 changes: 0 additions & 5 deletions packages/graphql-mesh/utils/directive-typedefs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ export const directiveTypeDefs = /* GraphQL */ `
"""
directive @noAuth on FIELD
"""
This directive is used to convert the result to uppercase.
"""
directive @upper on FIELD
type LinkItem {
rel: String
href: String
Expand Down
8 changes: 8 additions & 0 deletions packages/inject-additional-transforms/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Logs
logs
*.log
npm-debug.log*

node_modules
_build
.DS_Store
37 changes: 37 additions & 0 deletions packages/inject-additional-transforms/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "inject-additional-transforms",
"version": "1.0.0",
"type": "module",
"main": "_build/cjs/index.js",
"module": "_build/esm/index.js",
"types": "_build/esm/index.d.ts",
"exports": {
".": {
"import": "./_build/esm/index.js",
"require": "./_build/cjs/index.js"
}
},
"files": [
"_build/**/*"
],
"scripts": {
"build:cjs": "tsc --project tsconfig-cjs.json",
"build:esm": "tsc --project tsconfig-esm.json",
"build": "rm -rf _build && npm run build:esm && npm run build:cjs && node ./scripts/prepare-package-json",
"pack": "npm run build && npm pack --pack-destination ../graphql-mesh/local-pkg"
},
"devDependencies": {
"@types/node": "^20.11.19",
"tslib": "^2.6.2",
"typescript": "^5.2.2"
},
"peerDependencies": {
"@graphql-mesh/types": "*",
"@graphql-tools/utils": "*",
"@graphql-mesh/utils": "*",
"graphql": "*"
},
"dependencies": {
"@graphql-tools/delegate": "^10.0.0"
}
}
Loading

0 comments on commit ad90e45

Please sign in to comment.