Skip to content

Commit

Permalink
fix: [core] hamronize hooks payload (#2406)
Browse files Browse the repository at this point in the history
  • Loading branch information
EmrysMyrddin authored Feb 10, 2025
1 parent 4a3eafb commit a3e0d70
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 6 deletions.
7 changes: 7 additions & 0 deletions .changeset/famous-buckets-beg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@envelop/types': minor
'@envelop/core': minor
---

Add `context` field to `onExecute` and `onSubscribe` hooks payloads. This harmonize all the hooks
invovled in the handling of a graphql operation.
2 changes: 2 additions & 0 deletions .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ jobs:

- name: Setup env
uses: the-guild-org/shared-config/setup@v1
with:
node-version-file: '.node-version'

- name: Build
run: pnpm run build
Expand Down
9 changes: 5 additions & 4 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ jobs:
uses: actions/checkout@v4
- name: Setup env
uses: the-guild-org/shared-config/setup@v1
with:
node-version-file: '.node-version'
- name: Format
run: pnpm run lint:prettier
- name: Lint
run: pnpm run ci:lint && pnpm run lint
- name: Use GraphQL v${{matrix.graphql_version}}
run: node ./scripts/match-graphql.js ${{matrix.graphql_version}}
- name: Install Dependencies using pnpm
run: pnpm install --no-frozen-lockfile && git checkout pnpm-lock.yaml
- name: Build
run: pnpm run ts:check
- name: Test ESM & CJS exports integrity
Expand All @@ -50,10 +50,11 @@ jobs:
uses: actions/checkout@v4
- name: Setup env
uses: the-guild-org/shared-config/setup@v1
with:
node-version: ${{matrix.node-version}}

- name: Use GraphQL v${{matrix.graphql_version}}
run: node ./scripts/match-graphql.js ${{matrix.graphql_version}}
- name: Install Dependencies
run: pnpm install --no-frozen-lockfile && git checkout pnpm-lock.yaml
- name: Cache Jest
uses: actions/cache@v4
with:
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/website.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ jobs:

- uses: the-guild-org/shared-config/setup@v1
name: setup env
with:
node-version-file: '.node-version'
- uses: the-guild-org/shared-config/website-cf@v1
name: build and deploy website
env:
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ export function createEnvelopOrchestrator<PluginsContext extends DefaultContext>
setSubscribeFn: newSubscribeFn => {
subscribeFn = newSubscribeFn;
},
context,
extendContext: extension => {
Object.assign(context, extension);
},
Expand Down Expand Up @@ -494,6 +495,7 @@ export function createEnvelopOrchestrator<PluginsContext extends DefaultContext>
setResultAndStopExecution: stopResult => {
result = stopResult;
},
context,
extendContext: extension => {
if (typeof extension === 'object') {
Object.assign(context, extension);
Expand Down
6 changes: 4 additions & 2 deletions packages/core/test/execute.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,17 @@ describe('execute', () => {
it('Should wrap and trigger events correctly', async () => {
const spiedPlugin = createSpiedPlugin();
const teskit = createTestkit([spiedPlugin.plugin], schema);
await teskit.execute(query, {}, { test: 1 });
const context = { test: 1 };
await teskit.execute(query, {}, context);
expect(spiedPlugin.spies.beforeExecute).toHaveBeenCalledTimes(1);
expect(spiedPlugin.spies.beforeExecute).toHaveBeenCalledWith({
executeFn: expect.any(Function),
setExecuteFn: expect.any(Function),
extendContext: expect.any(Function),
setResultAndStopExecution: expect.any(Function),
context,
args: {
contextValue: expect.objectContaining({ test: 1 }),
contextValue: context,
rootValue: {},
schema: expect.any(GraphQLSchema),
operationName: undefined,
Expand Down
8 changes: 8 additions & 0 deletions packages/types/src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,10 @@ export type OnExecuteEventPayload<ContextType> = {
* Set a execution result and skip calling the execute function.
*/
setResultAndStopExecution: (newResult: ExecutionResult) => void;
/**
* The context object.
*/
context: Readonly<ContextType>;
/**
* Extend the context object with a partial.
*/
Expand Down Expand Up @@ -412,6 +416,10 @@ export type OnSubscribeEventPayload<ContextType> = {
* Replace the current subscribe function with a new one that will be used for setting up the subscription.
*/
setSubscribeFn: (newSubscribe: SubscribeFunction) => void;
/**
* The context object.
*/
context: Readonly<ContextType>;
/**
* Extend the context object with a partial.
*/
Expand Down

0 comments on commit a3e0d70

Please sign in to comment.