Skip to content

Commit d049f0c

Browse files
committed
feat: allow mutations on niorthwind (+ reset data every 30 minutes)
1 parent a7f5631 commit d049f0c

File tree

4 files changed

+49
-16
lines changed

4 files changed

+49
-16
lines changed

examples/northwind/schema.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// import { SchemaComposer } from 'graphql-compose';
88
// const schemaComposer = new SchemaComposer();
99

10-
import { schemaComposer, composeWithRelay, Resolver } from './schemaComposer';
10+
import { schemaComposer, composeWithRelay } from './schemaComposer';
1111
import { CategoryTC } from './models/category';
1212
import { CustomerTC } from './models/customer';
1313
import { EmployeeTC } from './models/employee';
@@ -16,7 +16,10 @@ import { ProductTC } from './models/product';
1616
import { RegionTC } from './models/region';
1717
import { ShipperTC } from './models/shipper';
1818
import { SupplierTC } from './models/supplier';
19-
import allowOnlyForLocalhost from './auth/allowOnlyForLocalhost';
19+
// import { allowOnlyForLocalhost } from './wrappers/allowOnlyForLocalhost';
20+
import { addQueryToPayload } from './wrappers/addQueryToPayload';
21+
import { autoResetDataIn30min } from './wrappers/autoResetDataIn30min';
22+
import seed from './data/seed';
2023

2124
composeWithRelay(schemaComposer.Query);
2225

@@ -63,7 +66,8 @@ const fields = {
6366
ViewerTC.addFields(fields);
6467

6568
schemaComposer.Mutation.addFields({
66-
...allowOnlyForLocalhost({
69+
// ...allowOnlyForLocalhost({
70+
...autoResetDataIn30min({
6771
...addQueryToPayload({
6872
createProduct: ProductTC.getResolver('createOne'),
6973
updateProduct: ProductTC.getResolver('updateById'),
@@ -76,16 +80,15 @@ schemaComposer.Mutation.addFields({
7680
updateEmployee: EmployeeTC.getResolver('updateById'),
7781
}),
7882
}),
83+
resetData: {
84+
type: 'String',
85+
description:
86+
'Remove all data and seed DB from scratch. Anyway data automatically reloaded every 30 minutes.',
87+
resolve: async () => {
88+
await seed();
89+
return 'Success';
90+
},
91+
},
7992
});
8093

81-
function addQueryToPayload(resolvers: { [name: string]: Resolver<any, any, any> }) {
82-
Object.keys(resolvers).forEach((k) => {
83-
resolvers[k].getOTC().setField('query', {
84-
type: 'Query',
85-
resolve: () => ({}),
86-
});
87-
});
88-
return resolvers;
89-
}
90-
9194
export default schemaComposer.buildSchema();
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/* @flow */
2+
3+
import { Resolver } from '../schemaComposer';
4+
5+
export function addQueryToPayload(resolvers: { [name: string]: Resolver<any, any, any> }) {
6+
Object.keys(resolvers).forEach((k) => {
7+
resolvers[k].getOTC().setField('query', {
8+
type: 'Query',
9+
resolve: () => ({}),
10+
});
11+
});
12+
return resolvers;
13+
}

examples/northwind/auth/allowOnlyForLocalhost.js renamed to examples/northwind/wrappers/allowOnlyForLocalhost.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
import type { Resolver } from 'graphql-compose';
44

5-
export default function allowOnlyForLocalhost(resolvers: {
6-
[name: string]: Resolver<any, any, any>,
7-
}) {
5+
export function allowOnlyForLocalhost(resolvers: { [name: string]: Resolver<any, any, any> }) {
86
const secureResolvers = {};
97
Object.keys(resolvers).forEach((k) => {
108
secureResolvers[k] = resolvers[k].wrapResolve((next) => (rp) => {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* @flow */
2+
3+
import type { Resolver } from 'graphql-compose';
4+
import seed from '../data/seed';
5+
6+
let clearDataTimeoutId;
7+
8+
export function autoResetDataIn30min(resolvers: { [name: string]: Resolver<any, any, any> }) {
9+
const secureResolvers = {};
10+
Object.keys(resolvers).forEach((k) => {
11+
secureResolvers[k] = resolvers[k].wrapResolve((next) => (rp) => {
12+
if (!clearDataTimeoutId) {
13+
clearDataTimeoutId = setTimeout(seed, 1000 * 30 * 60);
14+
}
15+
return next(rp);
16+
});
17+
});
18+
return secureResolvers;
19+
}

0 commit comments

Comments
 (0)