Skip to content

Commit

Permalink
♻️ Get directive type definition from lib for more isolation
Browse files Browse the repository at this point in the history
  • Loading branch information
Mbaye THIAM committed Apr 12, 2024
1 parent 9e4cf7f commit 93a28b5
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 46 deletions.
12 changes: 12 additions & 0 deletions packages/directive-headers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,15 @@ export default class HeadersDirectiveTransform implements MeshTransform {
})
}
}

export const headersDirectiveTypeDef: string = /* GraphQL */ `
input Header {
key: String
value: String
}
"""
This directive is used to add headers to the request.
"""
directive @headers(input: [Header]) on FIELD
`
7 changes: 7 additions & 0 deletions packages/directive-no-auth/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,10 @@ export default class NoAuthDirectiveTransform implements MeshTransform {
})
}
}

export const noAuthDirectiveTypeDef: string = /* GraphQL */ `
"""
This directive is used to disable the authorization header for the request
"""
directive @noAuth on FIELD
`
7 changes: 7 additions & 0 deletions packages/directive-spl/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,10 @@ export default class SplDirectiveTransform implements MeshTransform {
})
}
}

export const splDirectiveTypeDef: string = /* GraphQL */ `
"""
This is a very small, lightweight, straightforward and non-evaluated expression language to sort, filter and paginate arrays of maps.
"""
directive @SPL(query: String) on FIELD
`
14 changes: 1 addition & 13 deletions packages/directive-spl/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,4 @@
/// <reference types="vitest" />
// Configure Vitest (https://vitest.dev/config/)
import { resolve } from 'path'
import { defineConfig } from 'vite'
import dts from 'vite-plugin-dts'
// https://vitejs.dev/guide/build.html#library-mode
export default defineConfig({
build: {
lib: {
entry: resolve(__dirname, 'src/index.ts'),
name: 'my-lib',
fileName: 'my-lib'
}
},
plugins: [dts()]
})
export default defineConfig({})
13 changes: 10 additions & 3 deletions packages/graphql-mesh/.meshrc.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { YamlConfig } from '@graphql-mesh/types'
import ConfigFromSwaggers from './utils/ConfigFromSwaggers'
import { splDirectiveTypeDef } from 'directive-spl'
import { headersDirectiveTypeDef } from 'directive-headers'
import { noAuthDirectiveTypeDef } from 'directive-no-auth'

const configFromSwaggers = new ConfigFromSwaggers()
const { defaultConfig, additionalTypeDefs, sources } =
Expand All @@ -19,9 +22,13 @@ const config = <YamlConfig.Config>{
...(defaultConfig.transforms || [])
].filter(Boolean),
sources: [...sources],
additionalTypeDefs: [defaultConfig.additionalTypeDefs || '', ...additionalTypeDefs].filter(
Boolean
),
additionalTypeDefs: [
splDirectiveTypeDef,
headersDirectiveTypeDef,
noAuthDirectiveTypeDef,
additionalTypeDefs,
defaultConfig.additionalTypeDefs || ''
].filter(Boolean),
additionalResolvers: [
...(defaultConfig.additionalResolvers || []),
'./utils/additionalResolvers.ts'
Expand Down
Binary file modified packages/graphql-mesh/local-pkg/directive-headers-1.0.0.tgz
Binary file not shown.
Binary file modified packages/graphql-mesh/local-pkg/directive-no-auth-1.0.0.tgz
Binary file not shown.
Binary file modified packages/graphql-mesh/local-pkg/directive-spl-1.0.0.tgz
Binary file not shown.
Binary file not shown.
7 changes: 3 additions & 4 deletions packages/graphql-mesh/utils/ConfigFromSwaggers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { getConfig, getSourceName, getSourceOpenapiEnpoint } from './config'
import { getAvailableTypes } from './swaggers'
import { mergeObjects } from './helpers'
import { generateTypeDefsAndResolversFromSwagger } from './swaggers'
import { directiveTypeDefs } from './directive-typedefs'

export default class ConfigFromSwaggers {
swaggers: SwaggerName[] = []
Expand Down Expand Up @@ -94,7 +93,7 @@ export default class ConfigFromSwaggers {
ignoreErrorResponses: true,
operationHeaders: {
Authorization: `{context.headers["authorization"]}`,
...(this.config.sources.find((item) => source.includes(item.name))?.handler?.openapi
...(this.config.sources?.find((item) => source.includes(item.name))?.handler?.openapi
?.operationHeaders || {})
}
}
Expand Down Expand Up @@ -122,15 +121,15 @@ export default class ConfigFromSwaggers {

getMeshConfigFromSwaggers(): {
defaultConfig: any
additionalTypeDefs: string[]
additionalTypeDefs: string
additionalResolvers: any
sources: any[]
} {
const { typeDefs, resolvers } = this.createTypeDefsAndResolvers()

return {
defaultConfig: this.config,
additionalTypeDefs: [typeDefs, directiveTypeDefs].filter(Boolean),
additionalTypeDefs: typeDefs,
additionalResolvers: resolvers,
sources: [...this.getOpenApiSources(), ...this.getOtherSources()]
}
Expand Down
26 changes: 0 additions & 26 deletions packages/graphql-mesh/utils/directive-typedefs/index.ts

This file was deleted.

8 changes: 8 additions & 0 deletions packages/graphql-mesh/utils/swaggers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,15 @@ export const generateTypeDefsAndResolversFromSwagger = (
}
}

const linkItemTypeDef = /* GraphQL */ `
type LinkItem {
rel: String
href: String
}
`

let typeDefs = ''
typeDefs += linkItemTypeDef

const resolvers: Resolvers = {}

Expand Down

0 comments on commit 93a28b5

Please sign in to comment.