Skip to content

Commit

Permalink
Improvements runtime (#4487)
Browse files Browse the repository at this point in the history
* Improvements and fixes on runtime

* Execution improvements

* Fix errors

* Go

* Remove .vercelignore

* chore(dependencies): updated changesets for modified dependencies

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
ardatan and github-actions[bot] committed Sep 13, 2022
1 parent 8bf1863 commit 39f440e
Show file tree
Hide file tree
Showing 14 changed files with 161 additions and 182 deletions.
7 changes: 7 additions & 0 deletions .changeset/@graphql-mesh_cli-4487-dependencies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@graphql-mesh/cli": patch
---

dependencies updates:

- Removed dependency [`graphql-scalars@1.18.0` ↗︎](https://www.npmjs.com/package/graphql-scalars/v/1.18.0) (from `dependencies`)
8 changes: 8 additions & 0 deletions .changeset/@graphql-mesh_merger-bare-4487-dependencies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@graphql-mesh/merger-bare": patch
---

dependencies updates:

- Added dependency [`@graphql-mesh/merger-stitching@0.16.23` ↗︎](https://www.npmjs.com/package/@graphql-mesh/merger-stitching/v/0.16.23) (to `dependencies`)
- Removed dependency [`@graphql-tools/wrap@9.2.0` ↗︎](https://www.npmjs.com/package/@graphql-tools/wrap/v/9.2.0) (from `dependencies`)
10 changes: 10 additions & 0 deletions .changeset/violet-mayflies-live.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
'@graphql-mesh/cli': patch
'@graphql-mesh/merger-bare': patch
'@graphql-mesh/runtime': patch
---

- Do not assume scalars' types by using graphql-scalars
- Create unified executor only once at startup
- Respect predefined type definitions for scalars in the source types `.mesh/sources/SOURCE_NAME/types.ts` like `BigInt: bigint`
- Respect introspection fields correctly
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ package-lock.json
.next/
/website/src/pages/docs/api/
/.husky/_/
newrelic-agent.log
1 change: 0 additions & 1 deletion .vercelignore

This file was deleted.

5 changes: 0 additions & 5 deletions newrelic_agent.log

This file was deleted.

1 change: 0 additions & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
"@graphql-mesh/http": "0.1.8",
"@graphql-tools/utils": "8.12.0",
"graphql-yoga": "3.0.0-alpha-20220905163021-e923bb34",
"graphql-scalars": "1.18.0",
"dotenv": "16.0.2",
"dnscache": "1.0.2",
"graphql-import-node": "0.0.5",
Expand Down
38 changes: 9 additions & 29 deletions packages/cli/src/commands/ts-artifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { pathExists, writeFile, writeJSON } from '@graphql-mesh/utils';
import { generateOperations } from './generate-operations';
import { GraphQLMeshCLIParams } from '..';
import JSON5 from 'json5';
import { resolvers as scalarResolvers } from 'graphql-scalars';

const unifiedContextIdentifier = 'MeshContext';

Expand Down Expand Up @@ -64,18 +63,17 @@ async function generateTypesForApi(options: {
schema: GraphQLSchema;
name: string;
contextVariables: Record<string, string>;
codegenScalarsConfig: Record<string, string>;
}) {
const config = {
skipTypename: true,
namingConvention: 'keep',
enumsAsTypes: true,
ignoreEnumValuesFromSchema: true,
};
const baseTypes = await codegen({
filename: options.name + '_types.ts',
documents: [],
config: {
skipTypename: true,
namingConvention: 'keep',
enumsAsTypes: true,
ignoreEnumValuesFromSchema: true,
scalars: options.codegenScalarsConfig,
},
config,
schemaAst: options.schema,
schema: undefined as any, // This is not necessary on codegen. Will be removed later
skipDocumentsValidation: true,
Expand All @@ -88,7 +86,7 @@ async function generateTypesForApi(options: {
typescript: tsBasePlugin,
},
});
const codegenHelpers = new CodegenHelpers(options.schema, {}, {});
const codegenHelpers = new CodegenHelpers(options.schema, config, {});
const namespace = pascalCase(`${options.name}Types`);
const sdkIdentifier = pascalCase(`${options.name}Sdk`);
const contextIdentifier = pascalCase(`${options.name}Context`);
Expand Down Expand Up @@ -218,21 +216,6 @@ export async function generateTsArtifacts(
}
);
}
const codegenScalarsConfig = {
File: 'File',
Upload: 'File',
};
for (const resolverName in scalarResolvers) {
const scalarResolver = scalarResolvers[resolverName];
codegenScalarsConfig[scalarResolver.name] = scalarResolver.extensions?.codegenScalarType;
}
for (const typeName in unifiedSchema.getTypeMap()) {
const type = unifiedSchema.getType(typeName);
const codegenScalarType = type.extensions?.codegenScalarType;
if (codegenScalarType) {
codegenScalarsConfig[typeName] = codegenScalarType;
}
}
const codegenOutput =
'// @ts-nocheck\n' +
(
Expand All @@ -250,10 +233,9 @@ export async function generateTsArtifacts(
enumsAsTypes: true,
ignoreEnumValuesFromSchema: true,
useIndexSignature: true,
noSchemaStitching: mergerType !== 'stitching',
noSchemaStitching: false,
contextType: unifiedContextIdentifier,
federation: mergerType === 'federation',
scalars: codegenScalarsConfig,
...codegenConfig,
},
schemaAst: unifiedSchema,
Expand Down Expand Up @@ -281,9 +263,7 @@ export async function generateTsArtifacts(
schema: sourceSchema,
name: source.name,
contextVariables: source.contextVariables,
codegenScalarsConfig,
});

if (item) {
const content = item.sdk.codeAst + '\n' + item.context.codeAst;
await writeFile(pathModule.join(artifactsDir, `sources/${source.name}/types.ts`), content);
Expand Down
2 changes: 1 addition & 1 deletion packages/mergers/bare/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
"dependencies": {
"@graphql-mesh/types": "0.83.5",
"@graphql-mesh/utils": "0.41.10",
"@graphql-tools/wrap": "9.2.0",
"@graphql-tools/schema": "9.0.4",
"@graphql-tools/utils": "8.12.0",
"@graphql-mesh/merger-stitching": "0.16.23",
"tslib": "^2.4.0"
},
"publishConfig": {
Expand Down
59 changes: 18 additions & 41 deletions packages/mergers/bare/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,22 @@
import { MeshMerger, MeshMergerContext, Logger, MeshMergerOptions, RawSourceOutput } from '@graphql-mesh/types';
import { MeshMerger, MeshMergerContext, MeshMergerOptions, RawSourceOutput } from '@graphql-mesh/types';
import { applySchemaTransforms } from '@graphql-mesh/utils';
import { addResolversToSchema, mergeSchemas } from '@graphql-tools/schema';
import { asArray, getDocumentNodeFromSchema } from '@graphql-tools/utils';
import { wrapSchema } from '@graphql-tools/wrap';
import { buildASTSchema, extendSchema, GraphQLSchema } from 'graphql';
import { asArray, mapSchema } from '@graphql-tools/utils';
import { extendSchema, GraphQLSchema } from 'graphql';
import StitchingMerger from '@graphql-mesh/merger-stitching';

export default class BareMerger implements MeshMerger {
name = 'bare';
private logger: Logger;
constructor(options: MeshMergerOptions) {
this.logger = options.logger;
}
private stitchingMerger: StitchingMerger;
constructor(private options: MeshMergerOptions) {}

handleSingleWrappedExtendedSource({ rawSources: [rawSource], typeDefs, resolvers }: MeshMergerContext) {
let schema = wrapSchema(rawSource);
if (typeDefs.length > 0 || asArray(resolvers).length > 0) {
for (const typeDef of typeDefs) {
schema = extendSchema(schema, typeDef);
}
for (const resolversObj of asArray(resolvers)) {
addResolversToSchema({
schema,
resolvers: resolversObj,
updateResolversInPlace: true,
});
}
}
this.logger.debug(`Attaching a dummy sourceMap to the final schema`);
schema.extensions = schema.extensions || {};
Object.defineProperty(schema.extensions, 'sourceMap', {
get: () => {
return {
get() {
return schema;
},
};
},
});
return {
schema,
};
handleSingleWrappedExtendedSource(mergerCtx: MeshMergerContext) {
// switch to stitching merger
this.name = 'stitching';
this.options.logger.debug(`Switching to Stitching merger due to the transforms and additional resolvers`);
this.options.logger = this.options.logger.child('Stitching Proxy');
this.stitchingMerger = this.stitchingMerger || new StitchingMerger(this.options);
return this.stitchingMerger.getUnifiedSchema(mergerCtx);
}

handleSingleRegularSource({ rawSources: [rawSource], typeDefs, resolvers }: MeshMergerContext) {
Expand All @@ -56,15 +33,15 @@ export default class BareMerger implements MeshMerger {
});
}
}
this.logger.debug(`Attaching a dummy sourceMap to the final schema`);
this.options.logger.debug(`Attaching a dummy sourceMap to the final schema`);
schema.extensions = schema.extensions || {};
Object.defineProperty(schema.extensions, 'sourceMap', {
get: () => {
return {
get() {
// We should return a version of the schema only with the source-level transforms
// But we should prevent the existing schema from being mutated internally
const nonExecutableSchema = buildASTSchema(getDocumentNodeFromSchema(schema));
const nonExecutableSchema = mapSchema(schema);
return applySchemaTransforms(nonExecutableSchema, rawSource, nonExecutableSchema, rawSource.transforms);
},
};
Expand All @@ -87,7 +64,7 @@ export default class BareMerger implements MeshMerger {
return this.handleSingleRegularSource({ rawSources, typeDefs, resolvers });
}
const sourceMap = new Map<RawSourceOutput, GraphQLSchema>();
this.logger.debug(`Applying transforms for each source`);
this.options.logger.debug(`Applying transforms for each source`);
const schemas = rawSources.map(source => {
let schema = source.schema;
let sourceLevelSchema = source.schema;
Expand All @@ -102,14 +79,14 @@ export default class BareMerger implements MeshMerger {
return schema;
});

this.logger.debug(`Merging sources`);
this.options.logger.debug(`Merging sources`);
const unifiedSchema = mergeSchemas({
schemas,
typeDefs,
resolvers,
});

this.logger.debug(`Attaching sources to the unified schema`);
this.options.logger.debug(`Attaching sources to the unified schema`);
unifiedSchema.extensions = unifiedSchema.extensions || {};
Object.defineProperty(unifiedSchema.extensions, 'sourceMap', {
get: () => sourceMap,
Expand Down

0 comments on commit 39f440e

Please sign in to comment.