@stonecrop/rockfoil 0.10.16 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { wrapPlans } from 'postgraphile/utils';
|
|
2
|
+
/**
|
|
3
|
+
* Creates a PostGraphile plugin that wraps GraphQL query and mutation plans with before/after hooks
|
|
4
|
+
* @param hookMap - Configuration mapping field names to before/after hook functions
|
|
5
|
+
* @public
|
|
6
|
+
*/
|
|
7
|
+
export const createPglRockfoilPlugin = (hookMap) => {
|
|
8
|
+
const queryPlans = {};
|
|
9
|
+
const mutationPlans = {};
|
|
10
|
+
for (const [fieldname, plans] of Object.entries(hookMap)) {
|
|
11
|
+
if (plans.beforeQuery || plans.afterQuery) {
|
|
12
|
+
queryPlans[fieldname] = {
|
|
13
|
+
plan: (plan, $source, fieldArgs, info) => {
|
|
14
|
+
// Process before query hooks
|
|
15
|
+
if (plans.beforeQuery) {
|
|
16
|
+
plans.beforeQuery(plan, $source, fieldArgs, info);
|
|
17
|
+
}
|
|
18
|
+
// Execute the query plan
|
|
19
|
+
let $result = plan();
|
|
20
|
+
// Process after query hooks
|
|
21
|
+
if (plans.afterQuery) {
|
|
22
|
+
$result = plans.afterQuery($result, plan, $source, fieldArgs, info);
|
|
23
|
+
}
|
|
24
|
+
return $result;
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
if (plans.beforeMutation || plans.afterMutation) {
|
|
29
|
+
mutationPlans[fieldname] = {
|
|
30
|
+
plan: (plan, $source, fieldArgs, info) => {
|
|
31
|
+
// Process before mutation hooks
|
|
32
|
+
if (plans.beforeMutation) {
|
|
33
|
+
plans.beforeMutation(plan, $source, fieldArgs, info);
|
|
34
|
+
}
|
|
35
|
+
// Execute the mutation plan
|
|
36
|
+
let $result = plan();
|
|
37
|
+
// Process after mutation hooks
|
|
38
|
+
if (plans.afterMutation) {
|
|
39
|
+
$result = plans.afterMutation($result, plan, $source, fieldArgs, info);
|
|
40
|
+
}
|
|
41
|
+
return $result;
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return wrapPlans({
|
|
47
|
+
Query: queryPlans,
|
|
48
|
+
Mutation: mutationPlans,
|
|
49
|
+
});
|
|
50
|
+
};
|
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"postgraphile.test.d.ts","sourceRoot":"","sources":["../../tests/postgraphile.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { describe, it, expect, vi } from 'vitest';
|
|
2
|
+
// Mock postgraphile/utils with a simple implementation
|
|
3
|
+
vi.mock('postgraphile/utils', () => ({
|
|
4
|
+
wrapPlans: (config) => ({
|
|
5
|
+
name: 'TestPlugin',
|
|
6
|
+
version: '1.0.0',
|
|
7
|
+
_config: config, // Store config for testing
|
|
8
|
+
}),
|
|
9
|
+
}));
|
|
10
|
+
import { createPglRockfoilPlugin } from '../src/middleware/postgraphile';
|
|
11
|
+
describe('Rockfoil PostGraphile Plugin', () => {
|
|
12
|
+
describe('createPglRockfoilPlugin', () => {
|
|
13
|
+
it('should create a valid plugin with empty hook map', () => {
|
|
14
|
+
const plugin = createPglRockfoilPlugin({});
|
|
15
|
+
expect(plugin).toBeDefined();
|
|
16
|
+
expect(plugin.name).toBe('TestPlugin');
|
|
17
|
+
expect(plugin.version).toBe('1.0.0');
|
|
18
|
+
});
|
|
19
|
+
it('should create a plugin with query hooks', () => {
|
|
20
|
+
const hookMap = {
|
|
21
|
+
testField: {
|
|
22
|
+
beforeQuery: vi.fn(),
|
|
23
|
+
afterQuery: vi.fn(),
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
const plugin = createPglRockfoilPlugin(hookMap);
|
|
27
|
+
expect(plugin).toBeDefined();
|
|
28
|
+
expect(plugin.name).toBe('TestPlugin');
|
|
29
|
+
});
|
|
30
|
+
it('should create a plugin with mutation hooks', () => {
|
|
31
|
+
const hookMap = {
|
|
32
|
+
createTest: {
|
|
33
|
+
beforeMutation: vi.fn(),
|
|
34
|
+
afterMutation: vi.fn(),
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
const plugin = createPglRockfoilPlugin(hookMap);
|
|
38
|
+
expect(plugin).toBeDefined();
|
|
39
|
+
expect(plugin.name).toBe('TestPlugin');
|
|
40
|
+
});
|
|
41
|
+
it('should handle complex hook map', () => {
|
|
42
|
+
const hookMap = {
|
|
43
|
+
field1: {
|
|
44
|
+
beforeQuery: vi.fn(),
|
|
45
|
+
},
|
|
46
|
+
field2: {
|
|
47
|
+
afterQuery: vi.fn(),
|
|
48
|
+
},
|
|
49
|
+
field3: {
|
|
50
|
+
beforeMutation: vi.fn(),
|
|
51
|
+
afterMutation: vi.fn(),
|
|
52
|
+
},
|
|
53
|
+
};
|
|
54
|
+
const plugin = createPglRockfoilPlugin(hookMap);
|
|
55
|
+
expect(plugin).toBeDefined();
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
});
|