Skip to content

Commit 59df0c6

Browse files
committed
fix mocks
resolvers that return 'subresolvers' or fields that are set to functions for later use as resolvers were behaving not according to upstream graphql-js convention, these functions take 3 arguments, not 4, with parent available as this. See #1807 See https://github.com/graphql/graphql-js/blob/7e79bbe5f2b0e971b5e5f6fe3e7b19c43dea9f35/src/execution/execute.js#L1210-L1212
1 parent 101172e commit 59df0c6

File tree

4 files changed

+16
-7
lines changed

4 files changed

+16
-7
lines changed

.changeset/weak-peaches-count.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@
3939

4040
- Support for fragment hints has been removed in favor of selection set hints.
4141

42+
#### Mocking (`addMocksToSchema` and `@graphql-tools/mock`)
43+
44+
- Mocks returning objects with fields set as functions are now operating according to upstream graphql-js convention, i.e. these functions take three arguments, `args`, `context`, and `info` with `parent` available as `this` rather than as the first argument.
45+
4246
#### Other Utilities (`@graphql-tools/utils`)
4347

4448
- `filterSchema`'s `fieldFilter` will now filter *all* fields across Object, Interface, and Input types. For the previous Object-only behavior, switch to the `objectFieldFilter` option.
@@ -53,3 +57,4 @@
5357
- better error handling for merges #2016, #2062
5458
- fix typings #1614
5559
- disable implicit schema pruning #1817
60+
- mocks not working for functions #1807

packages/mock/src/mocking.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export function addMocksToSchema({ schema, mocks = {}, preserveResolvers = false
124124

125125
// if we're here, the field is already defined
126126
if (typeof root[fieldName] === 'function') {
127-
result = root[fieldName](root, args, context, info);
127+
result = root[fieldName](args, context, info);
128128
if (isMockList(result)) {
129129
result = result.mock(root, args, context, info, fieldType as GraphQLList<any>, mockType);
130130
}

packages/mock/tests/mocking.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,7 +1056,7 @@ describe('Mock', () => {
10561056
let jsSchema = buildSchemaFromTypeDefinitions(shorthand);
10571057
const mockMap = {
10581058
RootQuery: () => ({
1059-
returnStringArgument: (_o: any, a: Record<string, any>) => a.s,
1059+
returnStringArgument: (a: Record<string, any>) => a.s,
10601060
}),
10611061
};
10621062
jsSchema = addMocksToSchema({ schema: jsSchema, mocks: mockMap });
@@ -1075,7 +1075,7 @@ describe('Mock', () => {
10751075
let jsSchema = buildSchemaFromTypeDefinitions(shorthand);
10761076
const mockMap = {
10771077
RootMutation: () => ({
1078-
returnStringArgument: (_o: any, a: Record<string, any>) => a.s,
1078+
returnStringArgument: (a: Record<string, any>) => a.s,
10791079
}),
10801080
};
10811081
jsSchema = addMocksToSchema({ schema: jsSchema, mocks: mockMap });
@@ -1129,7 +1129,7 @@ describe('Mock', () => {
11291129
let jsSchema = buildSchemaFromTypeDefinitions(shorthand);
11301130
const mockMap = {
11311131
RootQuery: () => ({
1132-
returnListOfIntArg: (_o: any, a: Record<string, any>) =>
1132+
returnListOfIntArg: (a: Record<string, any>) =>
11331133
new MockList(a.l),
11341134
}),
11351135
Int: () => 12,
@@ -1258,13 +1258,13 @@ describe('Mock', () => {
12581258
// unintuitive corner-cases
12591259
const mockMap = {
12601260
RootQuery: () => ({
1261-
thread: (_o: any, a: Record<string, any>) => ({ id: a.id }),
1262-
threads: (_o: any, a: Record<string, any>) =>
1261+
thread: (a: Record<string, any>) => ({ id: a.id }),
1262+
threads: (a: Record<string, any>) =>
12631263
new MockList(ITEMS_PER_PAGE * a.num),
12641264
}),
12651265
Thread: () => ({
12661266
name: 'Lorem Ipsum',
1267-
posts: (_o: any, a: Record<string, any>) =>
1267+
posts: (a: Record<string, any>) =>
12681268
new MockList(
12691269
ITEMS_PER_PAGE * a.num,
12701270
(_oi: any, ai: Record<string, any>) => ({

website/docs/migration-from-tools.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ If you are using GraphQL Tools v6, there are several breaking changes to be awar
3737

3838
- Support for fragment hints has been removed in favor of selection set hints.
3939

40+
#### Mocking (`addMocksToSchema` and `@graphql-tools/mock`)
41+
42+
- Mocks returning objects with fields set as functions are now operating according to upstream graphql-js convention, i.e. these functions take three arguments, `args`, `context`, and `info` with `parent` available as `this` rather than as the first argument.
43+
4044
#### Other Utilities (`@graphql-tools/utils`)
4145

4246
- `filterSchema`'s `fieldFilter` will now filter *all* fields across Object, Interface, and Input types. For the previous Object-only behavior, switch to the `objectFieldFilter` option.

0 commit comments

Comments
 (0)