Skip to content
Open
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
51 changes: 51 additions & 0 deletions __tests__/graphql/create-complex-fields.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
11 changes: 9 additions & 2 deletions src/graphql/create-complex-fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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;
},
};
});
Expand Down
6 changes: 5 additions & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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?: {
Expand Down