Skip to content

Commit 1507d60

Browse files
authored
Merge pull request #5 from antstackio/api-docs
add api documentation
2 parents 2f85f93 + 99f3252 commit 1507d60

File tree

2 files changed

+95
-2
lines changed

2 files changed

+95
-2
lines changed

API.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# Api
2+
3+
## createApp
4+
5+
Library exports a default function which can be used to create an express app that can act as a proxy. It accepts two arguments
6+
7+
- requestParams
8+
- HandlerFunc
9+
10+
```js
11+
import createApp from '@antstackio/express-graphql-proxy'
12+
13+
const requestParams = {
14+
resourceUri: '<graphql_endpoint>',
15+
headers: {
16+
// headers to be send along while querying the gql endpoint
17+
}
18+
};
19+
20+
// function that authorizes the user's query
21+
// returns true or false, which dictates whether the user has
22+
// permission to execute the query and return based on that.
23+
function handlerFunc(gqlObject, context) {
24+
// Context object also has Request and Response Object embedded
25+
// in it which can be used to override default response
26+
}
27+
28+
const app = createApp(requestParams, handlerFunc);
29+
```
30+
31+
### requestParams
32+
33+
This should contain required data for making query to the graphql endpoint.
34+
35+
- **resourceUri** : url of the graphql endpoint
36+
- **headers** : headers that should accompany the request to graphql endpoint. Usually things like authorization tokens, api keys etc.
37+
38+
example
39+
40+
```jsx
41+
const requestParams = {
42+
resourceUri: "https://www.gqlResourceApi.com/graphql",
43+
headers: {
44+
api_key: "<api_key>"
45+
}
46+
}
47+
```
48+
49+
### HandlerFunc
50+
51+
This is the primary function defined by the user which decide the resource requesting user has enough permissions and respond accordingly. It gets an abstarct object build from graphql query, and the request and response object itself as arguments. It should respond with a boolean indicating if user has permission
52+
53+
**Params**
54+
55+
- gqlObject: abstract object created from parsing the graphql query. The object makes the arguments, fields being retrieved in the query etc readily available to the user.
56+
57+
```jsx
58+
// graphql query
59+
const query = `
60+
query {
61+
hello(name: "khubo") {
62+
result
63+
}
64+
}`;
65+
66+
// after parsing
67+
68+
// gqlObject in HandlerFunc
69+
{
70+
"type":"query",
71+
"queryObjects":[
72+
{
73+
"operationName":"hello",
74+
"variables":{"name":"khubo"},
75+
"selectedFields":{"result":1}
76+
}
77+
]
78+
}
79+
```
80+
81+
- Context : It has the request and response object - same the as one exposed by an express app to its handler methods.
82+
83+
Handler function should return a boolean indicating if the requesting user has required permissions or not. If its true, then the request is proxied to the graphql service and response is send back to the user. False return a 401 authentication error.
84+
85+
### Custom Response
86+
87+
User can sent custom response by utilising the response object exposed by context.
88+
89+
```js
90+
function handlerFunc(gqlObject, context) {
91+
const { res } = context;
92+
return res.status(403).send("who the heck are you?");
93+
}
94+
```

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ $ yarn add @antstackio/express-graphql-proxy
5757
There are services that provide graphql endpoints over http leaving the developer to come up with their own mechanism for authorisation and controllings permission for the access of resources. This library help in bootstrapping a middleware proxy server that makes it easy to implement RBAC without loosing the fun in using graphql from the client.
5858

5959
## Documentation
60-
61-
TODO
60+
- [Api documentation](./API.md)
6261

6362
## Examples
6463

0 commit comments

Comments
 (0)