Skip to content

Commit

Permalink
fix(core): do not trigger onSchemaChange if the schema is the same (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan authored Jan 13, 2025
1 parent 0ecf831 commit 8d89758
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/afraid-hairs-reflect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@envelop/core': patch
---

When the schema is the same, do not trigger `onSchemaChange` hook
3 changes: 3 additions & 0 deletions packages/core/src/orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ export function createEnvelopOrchestrator<PluginsContext extends DefaultContext>
// to allow setting the schema from the onPluginInit callback. We also need to make sure
// here not to call the same plugin that initiated the schema switch.
const replaceSchema = (newSchema: any, ignorePluginIndex = -1) => {
if (schema === newSchema) {
return;
}
schema = newSchema;

if (initDone) {
Expand Down
34 changes: 33 additions & 1 deletion packages/core/test/schema-change.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { buildSchema, GraphQLSchema } from 'graphql';
import { createSpiedPlugin, createTestkit } from '@envelop/testing';
import { Plugin } from '@envelop/types';
import { query, schema } from './common.js';
import { schema } from './common.js';

describe('schemaChange', () => {
it('Should trigger schema change initially when schema is available', async () => {
Expand Down Expand Up @@ -38,4 +38,36 @@ describe('schemaChange', () => {
expect(pluginA.onSchemaChange).toHaveBeenCalledTimes(1);
expect(pluginB.onSchemaChange).toHaveBeenCalledTimes(1);
});

it('should not trigger if the schema is the same', async () => {
let setSchemaFn: (newSchema: GraphQLSchema) => void = (newSchema: GraphQLSchema) => {
throw new Error('setSchemaFn not initialized');
};
const setSchemaPlugin: Plugin = {
onPluginInit({ setSchema }) {
setSchemaFn = setSchema;
},
};
const onSchemaChangePlugin: Plugin = {
onSchemaChange: jest.fn(),
};
const newSchema = buildSchema(/* GraphQL */ `
type Query {
foo: String!
}
`);
createTestkit([setSchemaPlugin, onSchemaChangePlugin]);
setSchemaFn(schema);
expect(onSchemaChangePlugin.onSchemaChange).toHaveBeenCalledTimes(1);
setSchemaFn(schema);
expect(onSchemaChangePlugin.onSchemaChange).toHaveBeenCalledTimes(1);
setSchemaFn(newSchema);
expect(onSchemaChangePlugin.onSchemaChange).toHaveBeenCalledTimes(2);
setSchemaFn(schema);
expect(onSchemaChangePlugin.onSchemaChange).toHaveBeenCalledTimes(3);
setSchemaFn(newSchema);
expect(onSchemaChangePlugin.onSchemaChange).toHaveBeenCalledTimes(4);
setSchemaFn(newSchema);
expect(onSchemaChangePlugin.onSchemaChange).toHaveBeenCalledTimes(4);
});
});

0 comments on commit 8d89758

Please sign in to comment.