Skip to content

Commit

Permalink
createFetch: reduce client.js size, use Promise.resolve() (kriasoft#…
Browse files Browse the repository at this point in the history
…1517)

* createFetch: reduce client.js size, use Promise.resolve()
Fixes kriasoft#1513
* Improve Flow types in createFetch
  • Loading branch information
jorrit authored and langpavel committed Jan 24, 2018
1 parent c069def commit d28b68b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/createFetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@
*/

/* @flow */
import { graphql } from 'graphql';

import type { graphql as graphqType, GraphQLSchema } from 'graphql';

type Fetch = (url: string, options: ?any) => Promise<any>;

type Options = {
baseUrl: string,
cookie?: string,
schema?: GraphQLSchema,
graphql?: graphqType,
};

/**
Expand All @@ -23,7 +26,10 @@ type Options = {
* of boilerplate code in the application.
* https://developer.mozilla.org/docs/Web/API/Fetch_API/Using_Fetch
*/
function createFetch(fetch: Fetch, { baseUrl, cookie, schema }: Options) {
function createFetch(
fetch: Fetch,
{ baseUrl, cookie, schema, graphql }: Options,
) {
// NOTE: Tweak the default options to suite your application needs
const defaults = {
method: 'POST', // handy with GraphQL backends
Expand All @@ -38,8 +44,8 @@ function createFetch(fetch: Fetch, { baseUrl, cookie, schema }: Options) {

return async (url: string, options: any) => {
const isGraphQL = url.startsWith('/graphql');
if (schema && isGraphQL) {
// We're SSR, so route the graphql internall to avoid latency
if (schema && graphql && isGraphQL) {
// We're SSR, so route the graphql internal to avoid latency
const query = JSON.parse(options.body);
const result = await graphql(
schema,
Expand All @@ -48,13 +54,12 @@ function createFetch(fetch: Fetch, { baseUrl, cookie, schema }: Options) {
null,
query.variables,
);
return new Promise(resolve =>
resolve({
status: result.errors ? 400 : 200,
json: () => new Promise(resolveJson => resolveJson(result)),
}),
);
return Promise.resolve({
status: result.errors ? 400 : 200,
json: () => Promise.resolve(result),
});
}

return isGraphQL || url.startsWith('/api')
? fetch(`${baseUrl}${url}`, {
...defaults,
Expand Down
2 changes: 2 additions & 0 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import express from 'express';
import cookieParser from 'cookie-parser';
import bodyParser from 'body-parser';
import expressJwt, { UnauthorizedError as Jwt401Error } from 'express-jwt';
import { graphql } from 'graphql';
import expressGraphQL from 'express-graphql';
import jwt from 'jsonwebtoken';
import fetch from 'node-fetch';
Expand Down Expand Up @@ -128,6 +129,7 @@ app.get('*', async (req, res, next) => {
baseUrl: config.api.serverUrl,
cookie: req.headers.cookie,
schema,
graphql,
}),
};

Expand Down

0 comments on commit d28b68b

Please sign in to comment.