Skip to content

Commit 49e9a20

Browse files
authored
Merge pull request #58 from indigotech/bugfix/after-middleware-changing-return-type
Bugfix - @after middleware changing return type
2 parents c05800a + 4b1d41a commit 49e9a20

File tree

3 files changed

+46
-10
lines changed

3 files changed

+46
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
- Improved tests by adding @Root ones - [#52](https://github.com/indigotech/graphql-schema-decorator/pull/52) - [@babbarankit](https://github.com/babbarankit)
1717
- Added @InterfaceType decorator - [#46](https://github.com/indigotech/graphql-schema-decorator/pull/46) - [@felipesabino](https://github.com/felipesabino)
1818
- Added @After middleware decorator - [#56](https://github.com/indigotech/graphql-schema-decorator/pull/56) - [@marcelorisse](https://github.com/marcelorisse)
19+
- Bugfix - @After middleware changing return type - [#58](https://github.com/indigotech/graphql-schema-decorator/pull/58) - [@felipesabino](https://github.com/felipesabino)
1920

2021
### Breaking changes
2122

src/specs/functional.spec.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,12 @@ describe('Functional', function () {
197197

198198
describe('After Middleware', function () {
199199

200+
@D.ObjectType()
201+
class ChangedReturnType {
202+
@D.Field({ nonNull: true })
203+
data: string;
204+
}
205+
200206
@D.ObjectType()
201207
class QueryType {
202208
@D.Field({ type: graphql.GraphQLString })
@@ -207,6 +213,24 @@ describe('Functional', function () {
207213
return 'Hello, world!';
208214
}
209215

216+
@D.Field({ type: ChangedReturnType })
217+
@D.After({
218+
middleware: async (
219+
context,
220+
args,
221+
result: Promise<string>,
222+
next: (error?: Error, value?: ChangedReturnType) => void,
223+
) => {
224+
let changedReturnType = {
225+
data: (await result),
226+
};
227+
next(null, changedReturnType);
228+
},
229+
})
230+
async changeReturnType(): Promise<string> {
231+
return 'Hello, world!';
232+
}
233+
210234
@D.Field({ type: graphql.GraphQLString })
211235
@D.After({
212236
middleware: (context, args, result, next) => {
@@ -244,6 +268,7 @@ describe('Functional', function () {
244268
async callError(): Promise<string> {
245269
return 'Hello, world!';
246270
}
271+
247272
}
248273

249274
@D.Schema()
@@ -291,6 +316,12 @@ describe('Functional', function () {
291316
assert(result.errors[0].message === 'Error from middleware');
292317
});
293318

319+
it('resolves @Field decorated with @After Middleware changing result type', async function () {
320+
const schema = schemaFactory(SchemaType);
321+
const result = await graphql.graphql(schema, `query { changeReturnType { data } }`);
322+
assert(result.data.changeReturnType.data === 'Hello, world!');
323+
});
324+
294325
});
295326

296327
describe('Union', function () {

src/type-factory/field.type-factory.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -158,18 +158,22 @@ export function resolverFactory(
158158
}
159159

160160
if (metadata.after) {
161-
let next: (error?: Error) => void = (error?: Error, value?: any): any => {
162-
if (error) {
163-
throw error;
164-
} else if (typeof (value) !== 'undefined') {
165-
result = value;
166-
}
167-
return result;
168-
};
169-
metadata.after.middleware.call(fieldParentClass, context, args, result, next);
161+
return new Promise((resolve, reject) => {
162+
let next: (error?: Error) => void = (error?: Error, value?: any): any => {
163+
if (error) {
164+
reject(error);
165+
} else if (typeof (value) !== 'undefined') {
166+
resolve(value);
167+
} else {
168+
resolve(result);
169+
}
170+
};
171+
metadata.after.middleware.call(fieldParentClass, context, args, result, next);
172+
});
173+
} else {
174+
return result;
170175
}
171176

172-
return result;
173177
};
174178

175179
return {

0 commit comments

Comments
 (0)