Skip to content

Commit

Permalink
enhance: drop customServerHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan committed Nov 25, 2022
1 parent 30db4a2 commit 686b330
Show file tree
Hide file tree
Showing 14 changed files with 64 additions and 205 deletions.
5 changes: 5 additions & 0 deletions .changeset/clean-gorillas-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-mesh/http': patch
---

Better support for lambdas
6 changes: 0 additions & 6 deletions .changeset/tricky-teachers-battle.md

This file was deleted.

6 changes: 6 additions & 0 deletions .changeset/wet-jobs-bow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@graphql-mesh/cache-cfw-kv': patch
'@graphql-mesh/cli': patch
---

Fix CF compatibility issues
6 changes: 6 additions & 0 deletions .changeset/wild-eggs-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@graphql-mesh/cli': minor
'@graphql-mesh/types': minor
---

**BREAKING** - Drop `customServerHandler`
2 changes: 0 additions & 2 deletions examples/openapi-location-weather/.meshrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,3 @@ additionalTypeDefs: |
documents:
- ./example-query.graphql

serve:
customServerHandler: ./customServerHandler.ts
24 changes: 0 additions & 24 deletions examples/openapi-location-weather/customServerHandler.ts

This file was deleted.

1 change: 0 additions & 1 deletion examples/openapi-location-weather/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
"@graphql-mesh/runtime": "0.44.34",
"@graphql-mesh/transform-cache": "0.12.65",
"@graphql-mesh/transform-rename": "0.13.0",
"apollo-server": "3.11.1",
"graphql": "16.6.0"
}
}
5 changes: 0 additions & 5 deletions packages/cli/src/commands/serve/yaml-config.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,6 @@ type ServeConfig @md {
"""
browser: Browser
"""
If you want to use a custom GraphQL server, you can pass the path of the code file that exports a custom Mesh Server Handler
With a custom server handler, you won't be able to use the features of GraphQL Mesh HTTP Server
"""
customServerHandler: String
"""
Title of GraphiQL Playground
"""
playgroundTitle: String
Expand Down
108 changes: 41 additions & 67 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,7 @@ import { generateTsArtifacts } from './commands/ts-artifacts';
import { serveMesh } from './commands/serve/serve';
import { fs, path as pathModule, process } from '@graphql-mesh/cross-helpers';
import { FsStoreStorageAdapter, MeshStore } from '@graphql-mesh/store';
import {
writeFile,
pathExists,
rmdirs,
DefaultLogger,
loadFromModuleExportExpression,
defaultImportFn,
} from '@graphql-mesh/utils';
import { writeFile, pathExists, rmdirs, DefaultLogger, defaultImportFn } from '@graphql-mesh/utils';
import { handleFatalError } from './handleFatalError';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
Expand Down Expand Up @@ -61,15 +54,16 @@ export const DEFAULT_CLI_PARAMS: GraphQLMeshCLIParams = {
export async function graphqlMesh(
cliParams = DEFAULT_CLI_PARAMS,
args = hideBin(process.argv),
cwdPath = process.cwd()
cwdPath = process.cwd(),
) {
let baseDir = cwdPath;
let logger: Logger = new DefaultLogger(cliParams.initialLoggerPrefix);
return yargs(args)
.help()
.option('r', {
alias: 'require',
describe: 'Loads specific require.extensions before running the codegen and reading the configuration',
describe:
'Loads specific require.extensions before running the codegen and reading the configuration',
type: 'array' as const,
default: [],
coerce: (externalModules: string[]) =>
Expand All @@ -78,11 +72,14 @@ export async function graphqlMesh(
const localModulePath = pathModule.resolve(baseDir, module);
const islocalModule = fs.existsSync(localModulePath);
return defaultImportFn(islocalModule ? localModulePath : module);
})
}),
),
})
.option('dir', {
describe: 'Modified the base directory to use for looking for ' + cliParams.configName + ' config file',
describe:
'Modified the base directory to use for looking for ' +
cliParams.configName +
' config file',
type: 'string',
default: baseDir,
coerce: dir => {
Expand Down Expand Up @@ -148,9 +145,10 @@ export async function graphqlMesh(
// We already handle Mesh instance errors inside `serveMesh`
// eslint-disable-next-line @typescript-eslint/no-floating-promises
meshInstance$.then(({ schema }) =>
writeFile(pathModule.join(outputDir, 'schema.graphql'), printSchemaWithDirectives(schema)).catch(e =>
logger.error(`An error occured while writing the schema file: `, e)
)
writeFile(
pathModule.join(outputDir, 'schema.graphql'),
printSchemaWithDirectives(schema),
).catch(e => logger.error(`An error occured while writing the schema file: `, e)),
);

// eslint-disable-next-line @typescript-eslint/no-floating-promises
Expand Down Expand Up @@ -197,10 +195,12 @@ export function createBuiltMeshHTTPHandler(): MeshHTTPHandler<MeshContext> {
fileType: 'ts',
codegenConfig: meshConfig.config.codegen,
},
cliParams
cliParams,
).catch(e => {
logger.error(`An error occurred while building the artifacts: ${e.stack || e.message}`);
})
logger.error(
`An error occurred while building the artifacts: ${e.stack || e.message}`,
);
}),
);
const serveMeshOptions: ServeMeshOptions = {
baseDir,
Expand All @@ -209,23 +209,11 @@ export function createBuiltMeshHTTPHandler(): MeshHTTPHandler<MeshContext> {
logger: meshConfig.logger.child('Server'),
rawServeConfig: meshConfig.config.serve,
};
if (meshConfig.config.serve?.customServerHandler) {
const customServerHandler = await loadFromModuleExportExpression<any>(
meshConfig.config.serve.customServerHandler,
{
defaultExportName: 'default',
cwd: baseDir,
importFn: defaultImportFn,
}
);
await customServerHandler(serveMeshOptions);
} else {
await serveMesh(serveMeshOptions, cliParams);
}
await serveMesh(serveMeshOptions, cliParams);
} catch (e) {
handleFatalError(e, logger);
}
}
},
)
.command<{ port: number; prod: boolean; validate: boolean }>(
cliParams.prodServerCommand,
Expand All @@ -240,7 +228,7 @@ export function createBuiltMeshHTTPHandler(): MeshHTTPHandler<MeshContext> {
const builtMeshArtifactsPath = pathModule.join(baseDir, cliParams.artifactsDir);
if (!(await pathExists(builtMeshArtifactsPath))) {
throw new Error(
`Seems like you haven't build the artifacts yet to start production server! You need to build artifacts first with "${cliParams.commandName} build" command!`
`Seems like you haven't build the artifacts yet to start production server! You need to build artifacts first with "${cliParams.commandName} build" command!`,
);
}
process.env.NODE_ENV = 'production';
Expand All @@ -256,20 +244,11 @@ export function createBuiltMeshHTTPHandler(): MeshHTTPHandler<MeshContext> {
logger: getMeshOptions.logger.child('Server'),
rawServeConfig,
};
if (rawServeConfig?.customServerHandler) {
const customServerHandler = await loadFromModuleExportExpression<any>(rawServeConfig.customServerHandler, {
defaultExportName: 'default',
cwd: baseDir,
importFn: defaultImportFn,
});
await customServerHandler(serveMeshOptions);
} else {
await serveMesh(serveMeshOptions, cliParams);
}
await serveMesh(serveMeshOptions, cliParams);
} catch (e) {
handleFatalError(e, logger);
}
}
},
)
.command(
cliParams.validateCommand,
Expand All @@ -280,7 +259,7 @@ export function createBuiltMeshHTTPHandler(): MeshHTTPHandler<MeshContext> {
try {
if (!(await pathExists(pathModule.join(baseDir, cliParams.artifactsDir)))) {
throw new Error(
`You cannot validate artifacts now because you don't have built artifacts yet! You need to build artifacts first with "${cliParams.commandName} build" command!`
`You cannot validate artifacts now because you don't have built artifacts yet! You need to build artifacts first with "${cliParams.commandName} build" command!`,
);
}

Expand All @@ -294,7 +273,7 @@ export function createBuiltMeshHTTPHandler(): MeshHTTPHandler<MeshContext> {
{
readonly: false,
validate: true,
}
},
);

logger.info(`Reading the configuration`);
Expand All @@ -320,7 +299,7 @@ export function createBuiltMeshHTTPHandler(): MeshHTTPHandler<MeshContext> {
if (destroy) {
destroy();
}
}
},
)
.command<{ fileType: 'json' | 'ts' | 'js'; throwOnInvalidConfig: boolean }>(
cliParams.buildArtifactsCommand,
Expand Down Expand Up @@ -376,7 +355,7 @@ export function createBuiltMeshHTTPHandler(): MeshHTTPHandler<MeshContext> {
{
readonly: false,
validate: false,
}
},
);

logger.info(`Reading the configuration`);
Expand All @@ -396,10 +375,15 @@ export function createBuiltMeshHTTPHandler(): MeshHTTPHandler<MeshContext> {

logger.info(`Generating the unified schema`);
const { schema, destroy, rawSources } = await getMesh(meshConfig);
await writeFile(pathModule.join(outputDir, 'schema.graphql'), printSchemaWithDirectives(schema));
await writeFile(
pathModule.join(outputDir, 'schema.graphql'),
printSchemaWithDirectives(schema),
);

logger.info(`Generating artifacts`);
meshConfig.importCodes.add(`import { createMeshHTTPHandler, MeshHTTPHandler } from '@graphql-mesh/http';`);
meshConfig.importCodes.add(
`import { createMeshHTTPHandler, MeshHTTPHandler } from '@graphql-mesh/http';`,
);
meshConfig.codes.add(`
export function createBuiltMeshHTTPHandler(): MeshHTTPHandler<MeshContext> {
return createMeshHTTPHandler<MeshContext>({
Expand All @@ -425,7 +409,7 @@ export function createBuiltMeshHTTPHandler(): MeshHTTPHandler<MeshContext> {
fileType: args.fileType,
codegenConfig: meshConfig.config.codegen,
},
cliParams
cliParams,
);

logger.info(`Cleanup`);
Expand All @@ -434,7 +418,7 @@ export function createBuiltMeshHTTPHandler(): MeshHTTPHandler<MeshContext> {
} catch (e) {
handleFatalError(e, logger);
}
}
},
)
.command<{ source: string }>(
cliParams.sourceServerCommand + ' <source>',
Expand All @@ -455,7 +439,9 @@ export function createBuiltMeshHTTPHandler(): MeshHTTPHandler<MeshContext> {
initialLoggerPrefix: cliParams.initialLoggerPrefix,
});
logger = meshConfig.logger;
const sourceIndex = meshConfig.sources.findIndex(rawSource => rawSource.name === args.source);
const sourceIndex = meshConfig.sources.findIndex(
rawSource => rawSource.name === args.source,
);
if (sourceIndex === -1) {
throw new Error(`Source ${args.source} not found`);
}
Expand All @@ -474,19 +460,7 @@ export function createBuiltMeshHTTPHandler(): MeshHTTPHandler<MeshContext> {
rawServeConfig: meshConfig.config.serve,
playgroundTitle: `${args.source} GraphiQL`,
};
if (meshConfig.config.serve?.customServerHandler) {
const customServerHandler = await loadFromModuleExportExpression<any>(
meshConfig.config.serve.customServerHandler,
{
defaultExportName: 'default',
cwd: baseDir,
importFn: defaultImportFn,
}
);
await customServerHandler(serveMeshOptions);
} else {
await serveMesh(serveMeshOptions, cliParams);
}
}
await serveMesh(serveMeshOptions, cliParams);
},
).argv;
}
4 changes: 0 additions & 4 deletions packages/types/src/config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,6 @@
}
]
},
"customServerHandler": {
"type": "string",
"description": "If you want to use a custom GraphQL server, you can pass the path of the code file that exports a custom Mesh Server Handler\nWith a custom server handler, you won't be able to use the features of GraphQL Mesh HTTP Server"
},
"playgroundTitle": {
"type": "string",
"description": "Title of GraphiQL Playground"
Expand Down
5 changes: 0 additions & 5 deletions packages/types/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,6 @@ export interface ServeConfig {
* This feature can be disabled by passing `false` (Any of: String, Boolean)
*/
browser?: string | boolean;
/**
* If you want to use a custom GraphQL server, you can pass the path of the code file that exports a custom Mesh Server Handler
* With a custom server handler, you won't be able to use the features of GraphQL Mesh HTTP Server
*/
customServerHandler?: string;
/**
* Title of GraphiQL Playground
*/
Expand Down
2 changes: 0 additions & 2 deletions website/src/generated-markdown/ServeConfig.generated.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ If this is provided, Mesh will be served via HTTPS:
This feature can be disabled by passing `false` One of:
* `String`
* `Boolean`
* `customServerHandler` (type: `String`) - If you want to use a custom GraphQL server, you can pass the path of the code file that exports a custom Mesh Server Handler
With a custom server handler, you won't be able to use the features of GraphQL Mesh HTTP Server
* `playgroundTitle` (type: `String`) - Title of GraphiQL Playground
* `trustProxy` (type: `String`) - Configure Express Proxy Handling
[Learn more](https://expressjs.com/en/guide/behind-proxies.html)
Loading

1 comment on commit 686b330

@severin
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ardatan what's the reason for this change?

Please sign in to comment.