Skip to content

Commit

Permalink
feat: add Response.createFromGraphQL() method for SSR
Browse files Browse the repository at this point in the history
  • Loading branch information
nodkz committed Dec 7, 2017
1 parent 31dbe6c commit 449c854
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ Middlewares
- `maxBatchSize` - integer representing maximum size of request to be sent in a single batch. Once a request hits the provided size in length a new batch request is ran. Actual for hardcoded limit in 100kb per request in [express-graphql](https://github.com/graphql/express-graphql/blob/master/src/parseBody.js#L112) module. (default: `102400` characters, roughly 100kb for 1-byte characters or 200kb for 2-byte characters)
- `allowMutations` - by default batching disabled for mutations, you may enable it passing `true` (default: `false`)
- **loggerMiddleware** - for logging requests and responses.
- `logger` - log function (default: `console.log.bind(console, '[RELAY-NETWORK]')`)
- `logger` - log function (default: `console.log.bind(console, '[RELAY-NETWORK]')`)
- An example of req/res output in console: <img width="968" alt="screen shot 2017-11-19 at 23 05 19" src="https://user-images.githubusercontent.com/1946920/33159466-557517e0-d03d-11e7-9711-ebdfe6e789c8.png">
- **perfMiddleware** - simple time measure for network request.
- `logger` - log function (default: `console.log.bind(console, '[RELAY-NETWORK]')`)
Expand Down Expand Up @@ -163,6 +163,27 @@ const store = new Store(source);
const environment = new Environment({ network, store });
```

### Server-side rendering (SSR)
For performant server-side rendering with `node 8` and above recommended to use special build of this network layer. Also you may execute `graphql` directly without http-request if you write custom middleware. All this recommendations present in following example:

```js
const RRNL = require('react-relay-network-modern/node8');
const graphql = require('graphql').graphql;
const schema = require('schema').default;
return new RRNL.RelayNetworkLayer([
() => async req => {
const res = await graphql({
schema,
source: req.getQueryString(),
variableValues: req.getVariables(),
contextValue: {},
});
// console.dir(res, { colors: true, depth: 5 });
return RRNL.RelayNetworkLayerResponse.createFromGraphQL(res);
},
]);
```

### How middlewares work internally

Middlewares on bottom layer use [fetch](https://github.com/github/fetch) method. So `req` is compliant with a `fetch()` options. And `res` can be obtained via `resPromise.then(res => ...)`, which returned by `fetch()`.
Expand Down
4 changes: 4 additions & 0 deletions src/RelayRequestBatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,8 @@ export default class RelayRequestBatch {
getVariables(): Variables {
throw new Error('Batch request does not have variables.');
}

getQueryString(): string {
return this.prepareBody();
}
}
11 changes: 11 additions & 0 deletions src/RelayResponse.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ export default class RelayResponse {
return r;
}

static async createFromGraphQL(res: { errors?: any, data?: any }) {
const r = new RelayResponse();
r._res = res;
r.ok = true;
r.status = 200;
r.data = res.data;
r.errors = res.errors;

return r;
}

processJsonData(json: mixed) {
this.json = json;
if (json) {
Expand Down
6 changes: 6 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@ import loggerMiddleware from './middlewares/logger';
import gqlErrorsMiddleware from './middlewares/gqlErrors';
import cacheMiddleware from './middlewares/cache';
import graphqlBatchHTTPWrapper from './express-middleware/graphqlBatchHTTPWrapper';
import RelayNetworkLayerRequest from './RelayRequest';
import RelayNetworkLayerRequestBatch from './RelayRequestBatch';
import RelayNetworkLayerResponse from './RelayResponse';

export {
RelayNetworkLayer,
RelayNetworkLayerRequest,
RelayNetworkLayerRequestBatch,
RelayNetworkLayerResponse,
batchMiddleware,
retryMiddleware,
urlMiddleware,
Expand Down

0 comments on commit 449c854

Please sign in to comment.