Skip to content

Commit a176e67

Browse files
Initial Commit of GraphQLRestRouter
0 parents  commit a176e67

File tree

16 files changed

+1854
-0
lines changed

16 files changed

+1854
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/*.js
2+
node_modules
3+
*.swp

ICacheEngine.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export default interface ICacheEngine {
2+
get: (key: string, setFn?: () => string|number|boolean) => string|number|boolean;
3+
set: (key: string, value: string|number|boolean) => void;
4+
}

InMemoryCache.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import ICacheEngine from './ICacheEngine';
2+
3+
const STORE_EXPIRATION_CHECK_IN_MS = 10;
4+
const DEFAULT_CACHE_TIME_IN_MS = 10;
5+
6+
export default class InMemoryCache implements ICacheEngine {
7+
private store: { [key: string]: string|number|boolean } = {};
8+
private storeCacheExpiration: { [key: string]: number } = {};
9+
10+
constructor() {
11+
this.monitorStoreForExpiredValues();
12+
}
13+
14+
get(key: string): string|number|boolean {
15+
return this.store[key];
16+
}
17+
18+
set(key: string, value: string|number|boolean, cacheTimeInMs: number = DEFAULT_CACHE_TIME_IN_MS): void {
19+
this.store[key] = value;
20+
21+
this.storeCacheExpiration[key] = new Date().getTime() + cacheTimeInMs;
22+
}
23+
24+
private monitorStoreForExpiredValues(): void {
25+
const { store, storeCacheExpiration } = this;
26+
27+
setInterval((): void => {
28+
const currentTime = new Date().getTime();
29+
30+
Object.keys(storeCacheExpiration).forEach((key: string): void => {
31+
if (storeCacheExpiration[key] < currentTime) {
32+
delete storeCacheExpiration[key];
33+
delete store[key]
34+
}
35+
});
36+
}, STORE_EXPIRATION_CHECK_IN_MS);
37+
}
38+
}

README.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# graphql-rest-router
2+
3+
GraphQL Rest Router allows you to expose an internal GraphQL API as a REST API.
4+
5+
- Introduction
6+
- DSL / Documentation
7+
- Example Usage
8+
- Caching
9+
- Custom Routes
10+
- Supported Servers
11+
12+
_Example Usage_
13+
```js
14+
import GraphQLRestRouter from 'graphql-rest-router';
15+
const Schema = `
16+
query UserById($id: Int!) {
17+
getUserById(id: $id) {
18+
...UserDetails
19+
}
20+
}
21+
22+
query UserByEmail($email: String!, $activeOnly: Boolean, $adminOnly: Boolean) {
23+
getUserByEmail(email: $email, activeOnly: $activeOnly, adminOnly: $adminOnly) {
24+
...UserDetails
25+
}
26+
}
27+
28+
fragment UserDetails on User {
29+
id
30+
email
31+
}
32+
`;
33+
34+
const api = new GraphQLRestRouter(url, Schema, {
35+
cacheEngine: GraphQLRestRouter.InMemoryCache,
36+
37+
defaultCacheTimeInMs: 300,
38+
39+
autoDiscoverEndpoints: false,
40+
41+
headers: {
42+
'x-jwt': 1234,
43+
},
44+
});
45+
46+
api.mount({
47+
operationName: 'UserByEmail'
48+
path: '/user/:email',
49+
50+
method: 'POST',
51+
52+
variables: {
53+
'adminOnly': true,
54+
},
55+
56+
defaultVariables: {
57+
'activeOnly': true,
58+
},
59+
60+
cacheTimeInMs: 0,
61+
}); // creates GET /user/:email?activeOnly=:active
62+
63+
api.mount('UserById'); // creates GET /UserById?id=:id
64+
65+
api.mount('UserById').at('/:id') // creates GET /:id
66+
.setCacheTimeInMs(200);
67+
68+
api.mount('UserById').at('/:id').as('POST')
69+
.disableCache()
70+
.modifyRequest(request => {
71+
request.addHeader('x-jwt', 'test');
72+
})
73+
.modifyResponse(response => {
74+
response.setStatus(200);
75+
});
76+
77+
api.mount('UserById').withOptions({
78+
path: '/user/0',
79+
80+
method: 'GET',
81+
variables: {
82+
id: 3
83+
}
84+
});
85+
86+
// Export Options
87+
api.asExpressRouter();
88+
api.asKoaRouter();
89+
api.listen(3000, () => console.log('Callback'));
90+
```

0 commit comments

Comments
 (0)