Skip to content

Commit

Permalink
Fix context in directive tests (redwoodjs#9025)
Browse files Browse the repository at this point in the history
**Problem**
Context isolation was not behaving as expect when using directive tests.
See
https://community.redwoodjs.com/t/redwood-v6-0-0-upgrade-guide/5044/46
for more details.

**Changes**
1. We set the global context to match the context passed in to the
`mockRedwoodDirective` and then the mock directive gets the global
context during execution.

**Notes**
It looks like in v5 we didn't pass through the context that was provided
as a parameter to `mockRedwoodDirective`. It can be somewhat tricky to
understand exactly what is happening due to a few layers and that in <v6
the context was not isolated between tests.

**Outstanding**
1. People may not fully expect the side effect that passing in the
context to the mock directive sets the global context?
  • Loading branch information
Josh-Walker-GM authored Aug 9, 2023
1 parent 58a2421 commit 2ce3189
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/graphql-server/src/plugins/useRedwoodDirective.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from 'graphql'
import { Plugin } from 'graphql-yoga'

import { GlobalContext } from '../index'
import type { GlobalContext } from '../index'

export interface DirectiveParams<
FieldType = any,
Expand Down
26 changes: 17 additions & 9 deletions packages/testing/src/api/directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import type {
ValidatorDirective,
TransformerDirective,
} from '@redwoodjs/graphql-server'
import { setContext, DirectiveType } from '@redwoodjs/graphql-server'
import {
DirectiveType,
setContext,
context as globalContext,
} from '@redwoodjs/graphql-server'

export { getDirectiveName } from '@redwoodjs/graphql-server'

Expand Down Expand Up @@ -73,41 +77,45 @@ export const mockRedwoodDirective: DirectiveMocker = (

if (directive.onResolvedValue.constructor.name === 'AsyncFunction') {
return async () => {
if (context) {
setContext(context || {})
if (context !== undefined) {
setContext(context)
}

if (directive.type === DirectiveType.TRANSFORMER) {
const { mockedResolvedValue } = others as TransformerMock
return directive.onResolvedValue({
resolvedValue: mockedResolvedValue,
directiveArgs: directiveArgs || {},
directiveArgs: directiveArgs ?? {},
context: globalContext,
...others,
} as DirectiveParams)
} else {
await directive.onResolvedValue({
directiveArgs: directiveArgs || {},
directiveArgs: directiveArgs ?? {},
context: globalContext,
...others,
} as DirectiveParams)
}
}
}

return () => {
if (context) {
setContext(context || {})
if (context !== undefined) {
setContext(context)
}

if (directive.type === DirectiveType.TRANSFORMER) {
const { mockedResolvedValue } = others as TransformerMock
return directive.onResolvedValue({
resolvedValue: mockedResolvedValue,
directiveArgs: directiveArgs || {},
directiveArgs: directiveArgs ?? {},
context: globalContext,
...others,
} as DirectiveParams)
} else {
directive.onResolvedValue({
directiveArgs: directiveArgs || {},
directiveArgs: directiveArgs ?? {},
context: globalContext,
...others,
} as DirectiveParams)
}
Expand Down

0 comments on commit 2ce3189

Please sign in to comment.