diff --git a/__tests__/graphql/create-complex-fields.test.ts b/__tests__/graphql/create-complex-fields.test.ts index 6df6af2..f1d188d 100644 --- a/__tests__/graphql/create-complex-fields.test.ts +++ b/__tests__/graphql/create-complex-fields.test.ts @@ -44,3 +44,54 @@ test("createComplexFieldsFunc - empty define", async() => { expect(fields.testInstanceMethod.resolve).toBeInstanceOf(Function); expect(fields.testInstanceMethod.type).toEqual(GraphQLInt); }); + +test("createComplexFieldsFunc - before/after hooks", async() => { + const db = new Database(); + db.registerAdapter(new SequelizeAdapter({}, { + dialect: "sqlite", + }) as GqlizeAdapter, "sqlite"); + const itemDef = { + name: "Item", + define: {}, + relationships: [], + expose: { + instanceMethods: { + query: { + testInstanceMethod: { + type: GraphQLInt, + before(args: any, context: any) { + return { + ...args, + amount: args.amount + 1, + }; + }, + after(result: any, context: any) { + return result + 100; + }, + }, + }, + }, + }, + instanceMethods: { + testInstanceMethod(args: any) { + return args.amount; + }, + }, + } as Definition; + await db.addDefinition(itemDef); + await db.initialise(); + await db.sync(); + const schemaCache = createSchemaCache(); + schemaCache.types.Item = new GraphQLObjectType({ + name: "Item", + fields: {} + }); + const func = createComplexFieldsFunc(itemDef.name || "", db, itemDef, {}, schemaCache); + const fields = func(); + const result = await fields.testInstanceMethod.resolve({ + testInstanceMethod(args: any) { + return args.amount; + }, + }, {amount: 1}, {}, {}); + expect(result).toEqual(102); +}); diff --git a/src/graphql/create-complex-fields.ts b/src/graphql/create-complex-fields.ts index 5fc65d2..a0aa7c0 100644 --- a/src/graphql/create-complex-fields.ts +++ b/src/graphql/create-complex-fields.ts @@ -17,7 +17,7 @@ export default function createComplexFieldsFunc( if (definition.expose?.instanceMethods?.query) { const instanceMethods = definition.expose.instanceMethods.query; Object.keys(instanceMethods).forEach((methodName) => { - const {type, args} = instanceMethods[methodName]; + const {type, args, before, after} = instanceMethods[methodName]; let targetType = (typeof type === "string") ? schemaCache.types[type] : type; if (!targetType) { //target does not exist.. excluded from base types? @@ -34,7 +34,14 @@ export default function createComplexFieldsFunc( args, description: (definition.comments?.fields || {})[methodName], async resolve(source: any, args: any, context: any, info: any) { - return source[methodName].apply(source, [args, context]); + if (before) { + args = await before(args, context); + } + let result = await source[methodName].apply(source, [args, context]); + if (after) { + result = await after(result, context); + } + return result; }, }; }); diff --git a/src/types/index.ts b/src/types/index.ts index 71dbaad..665d633 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -225,15 +225,19 @@ export type Definition = { [name: string]: { type: any; args?: any; + before?: any; + after?: any; } } mutations?: { [name: string]: { type: any; args?: any; + before?: any; + after?: any; } } - + } } instanceMethods?: {