Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions .cursor/rules/examples.mdc
Original file line number Diff line number Diff line change
Expand Up @@ -218,14 +218,13 @@ const resolvers = {

### Example Scripts
- Add npm scripts for running examples
- Use DEBUG environment variable
- Include clear naming

```json
{
"scripts": {
"start-composition": "DEBUG=graphql-component ts-node examples/composition/server/index.ts",
"start-federation": "DEBUG=graphql-component ts-node examples/federation/run-federation-example.ts"
"start-composition": "ts-node examples/composition/server/index.ts",
"start-federation": "ts-node examples/federation/run-federation-example.ts"
}
}
```
Expand Down
1 change: 0 additions & 1 deletion .cursor/rules/overview.mdc
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ The library supports both traditional schema stitching for monolithic applicatio
- **tape** - Testing framework for unit tests
- **eslint** - Code linting with TypeScript support
- **prettier** - Code formatting
- **debug** - Runtime debugging utilities
- **ts-node** - TypeScript execution for examples and development

## Example Dependencies
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- [TESTS] Added test to verify dataSources availability in middleware
- [PERFORMANCE] Enhanced context building with parallel import processing, and middleware optimization
- [TESTS] Added comprehensive performance regression tests to validate optimization correctness and prevent breaking changes
- [SECURITY] Removed `debug`

### v6.0.1

Expand Down
7 changes: 0 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,13 +265,6 @@ Both examples are accessible at `http://localhost:4000/graphql` when running.

You can find the complete example code in the [`examples/`](./examples/) directory.

## Debugging

Enable debug logging with:
```bash
DEBUG=graphql-component:* node your-app.js
```

## Repository Structure

- `src/` - Core library code
Expand Down
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
"build": "tsc",
"prepublish": "npm run build",
"test": "tape -r ts-node/register \"test/**/*.ts\"",
"start-composition": "DEBUG=graphql-component ts-node examples/composition/server/index.ts",
"start-federation": "DEBUG=graphql-component ts-node examples/federation/run-federation-example.ts",
"start-composition": "ts-node examples/composition/server/index.ts",
"start-federation": "ts-node examples/federation/run-federation-example.ts",
"lint": "npx eslint src/index.ts",
"cover": "nyc npm test",
"update-deps": "ncu -u && npm install",
Expand All @@ -33,8 +33,7 @@
"@graphql-tools/mock": "^9.0.6",
"@graphql-tools/schema": "^10.0.8",
"@graphql-tools/stitch": "^9.4.0",
"@graphql-tools/utils": "^10.5.6",
"debug": "^4.3.7"
"@graphql-tools/utils": "^10.5.6"
},
"peerDependencies": {
"graphql": "^16.0.0"
Expand Down
31 changes: 5 additions & 26 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import debugConfig from 'debug';
import { buildFederatedSchema } from '@apollo/federation';
import { GraphQLResolveInfo, GraphQLScalarType, GraphQLSchema } from 'graphql';

Expand All @@ -16,8 +15,6 @@ import { stitchSchemas } from '@graphql-tools/stitch';
import { addMocksToSchema, IMocks } from '@graphql-tools/mock';
import { SubschemaConfig } from '@graphql-tools/delegate';

const debug = debugConfig('graphql-component');

export type ResolverFunction = (_: any, args: any, ctx: any, info: GraphQLResolveInfo) => any;

export interface IGraphQLComponentConfigObject {
Expand Down Expand Up @@ -211,7 +208,6 @@ export default class GraphQLComponent<TContextType extends ComponentContext = Co

// Handle namespace context if present
if (context) {
debug(`building ${context.namespace} context`);

if (!ctx[context.namespace]) {
ctx[context.namespace] = {};
Expand All @@ -231,7 +227,6 @@ export default class GraphQLComponent<TContextType extends ComponentContext = Co
get context(): IContextWrapper {
// Cache middleware array to avoid recreation
const contextFn = async (context: Record<string, unknown>): Promise<ComponentContext> => {
debug(`building root context`);

// Inject dataSources early so middleware can access them
const dataSources = this._dataSourceContextInject(context);
Expand All @@ -255,9 +250,8 @@ export default class GraphQLComponent<TContextType extends ComponentContext = Co

// Apply middleware more efficiently
if (this._middleware.length > 0) {
for (const { name, fn } of this._middleware) {
debug(`applying ${name} middleware`);
processedContext = await fn(processedContext);
for (const mw of this._middleware) {
processedContext = await mw.fn(processedContext);
}
}

Expand All @@ -272,7 +266,7 @@ export default class GraphQLComponent<TContextType extends ComponentContext = Co
fn = name;
name = 'unknown';
}
debug(`adding ${name} middleware`);

this._middleware.push({ name: name as string, fn: fn! });

return contextFn;
Expand Down Expand Up @@ -333,27 +327,22 @@ export default class GraphQLComponent<TContextType extends ComponentContext = Co
}

if (this._mocks !== undefined && typeof this._mocks === 'boolean' && this._mocks === true) {
debug(`adding default mocks to the schema for ${this.name}`);
// if mocks are a boolean support simply applying default mocks
this._schema = addMocksToSchema({ schema: this._schema, preserveResolvers: true });
}
else if (this._mocks !== undefined && typeof this._mocks === 'object') {
debug(`adding custom mocks to the schema for ${this.name}`);
// else if mocks is an object, that means the user provided
// custom mocks, with which we pass them to addMocksToSchema so they are applied
this._schema = addMocksToSchema({ schema: this._schema, mocks: this._mocks, preserveResolvers: true });
}

if (this._pruneSchema) {
debug(`pruning the schema for ${this.name}`);
this._schema = pruneSchema(this._schema, this._pruneSchemaOptions);
}

debug(`created schema for ${this.name}`);

return this._schema;
} catch (error) {
debug(`Error creating schema for ${this.name}: ${error}`);
}
catch (error) {
throw new Error(`Failed to create schema for component ${this.name}: ${error.message}`);
}
}
Expand Down Expand Up @@ -450,7 +439,6 @@ module.exports = GraphQLComponent;
*/
const createDataSourceContextInjector = (dataSources: IDataSource[], dataSourceOverrides: IDataSource[]): DataSourceInjectionFunction => {
const intercept = (instance: IDataSource, context: any) => {
debug(`intercepting ${instance.constructor.name}`);

return new Proxy(instance, {
get(target, key) {
Expand Down Expand Up @@ -502,12 +490,9 @@ const memoize = function (parentType: string, fieldName: string, resolve: Resolv
const path = info && info.path && info.path.key;
const key = `${path}_${JSON.stringify(args)}`;

debug(`executing ${parentType}.${fieldName}`);

let cached = _cache.get(context);

if (cached && cached[key]) {
debug(`return cached result of memoized ${parentType}.${fieldName}`);
return cached[key];
}

Expand All @@ -521,8 +506,6 @@ const memoize = function (parentType: string, fieldName: string, resolve: Resolv

_cache.set(context, cached);

debug(`cached ${parentType}.${fieldName}`);

return result;
};
};
Expand All @@ -541,7 +524,6 @@ const bindResolvers = function (bindContext: IGraphQLComponent, resolvers: IReso
for (const [type, fields] of Object.entries(resolvers)) {
// dont bind an object that is an instance of a graphql scalar
if (fields instanceof GraphQLScalarType) {
debug(`not binding ${type}'s fields since ${type}'s fields are an instance of GraphQLScalarType`)
boundResolvers[type] = fields;
continue;
}
Expand All @@ -552,17 +534,14 @@ const bindResolvers = function (bindContext: IGraphQLComponent, resolvers: IReso

for (const [field, resolver] of Object.entries(fields)) {
if (['Query', 'Mutation'].indexOf(type) > -1) {
debug(`memoized ${type}.${field}`);
boundResolvers[type][field] = memoize(type, field, resolver.bind(bindContext));
}
else {
// only bind resolvers that are functions
if (typeof resolver === 'function') {
debug(`binding ${type}.${field}`);
boundResolvers[type][field] = resolver.bind(bindContext);
}
else {
debug(`not binding ${type}.${field} since ${field} is not mapped to a function`);
boundResolvers[type][field] = resolver;
}
}
Expand Down