Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- Improved tests by adding @Root ones - [#52](https://github.com/indigotech/graphql-schema-decorator/pull/52) - [@babbarankit](https://github.com/babbarankit)
- Added @InterfaceType decorator - [#46](https://github.com/indigotech/graphql-schema-decorator/pull/46) - [@felipesabino](https://github.com/felipesabino)
- Added @After middleware decorator - [#56](https://github.com/indigotech/graphql-schema-decorator/pull/56) - [@marcelorisse](https://github.com/marcelorisse)
- Bugfix - @After middleware changing return type - [#58](https://github.com/indigotech/graphql-schema-decorator/pull/58) - [@felipesabino](https://github.com/felipesabino)

### Breaking changes

Expand Down
31 changes: 31 additions & 0 deletions src/specs/functional.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,12 @@ describe('Functional', function () {

describe('After Middleware', function () {

@D.ObjectType()
class ChangedReturnType {
@D.Field({ nonNull: true })
data: string;
}

@D.ObjectType()
class QueryType {
@D.Field({ type: graphql.GraphQLString })
Expand All @@ -207,6 +213,24 @@ describe('Functional', function () {
return 'Hello, world!';
}

@D.Field({ type: ChangedReturnType })
@D.After({
middleware: async (
context,
args,
result: Promise<string>,
next: (error?: Error, value?: ChangedReturnType) => void,
) => {
let changedReturnType = {
data: (await result),
};
next(null, changedReturnType);
},
})
async changeReturnType(): Promise<string> {
return 'Hello, world!';
}

@D.Field({ type: graphql.GraphQLString })
@D.After({
middleware: (context, args, result, next) => {
Expand Down Expand Up @@ -244,6 +268,7 @@ describe('Functional', function () {
async callError(): Promise<string> {
return 'Hello, world!';
}

}

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

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

});

describe('Union', function () {
Expand Down
24 changes: 14 additions & 10 deletions src/type-factory/field.type-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,18 +158,22 @@ export function resolverFactory(
}

if (metadata.after) {
let next: (error?: Error) => void = (error?: Error, value?: any): any => {
if (error) {
throw error;
} else if (typeof (value) !== 'undefined') {
result = value;
}
return result;
};
metadata.after.middleware.call(fieldParentClass, context, args, result, next);
return new Promise((resolve, reject) => {
let next: (error?: Error) => void = (error?: Error, value?: any): any => {
if (error) {
reject(error);
} else if (typeof (value) !== 'undefined') {
resolve(value);
} else {
resolve(result);
}
};
metadata.after.middleware.call(fieldParentClass, context, args, result, next);
});
} else {
return result;
}

return result;
};

return {
Expand Down