Skip to content
This repository has been archived by the owner on May 17, 2019. It is now read-only.

Commit

Permalink
Add token for passing body parser config (#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
Giancarlo Anemone committed May 3, 2019
1 parent 61b31b6 commit 7c26f4d
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 1 deletion.
14 changes: 14 additions & 0 deletions README.md
Expand Up @@ -24,6 +24,7 @@ The plugin will perform graphql queries on the server, thereby rendering your ap
- [`ApolloClientCredentialsToken`](#apolloclientcredentialstoken)
- [`GetApolloClientLinksToken`](#getapolloclientlinkstoken)
- [`ApolloClientResolversToken`](#apolloclientresolverstoken)
- [`ApolloBodyParserConfigToken`](#apollobodyparserconfigtoken)
= [GQL Macro]($gql)

---
Expand Down Expand Up @@ -200,6 +201,19 @@ import { ApolloClientResolversToken } from "fusion-apollo-universal-client";

Optional - Provides the resolvers for [local state management](https://www.apollographql.com/docs/react/essentials/local-state.html).

##### `ApolloBodyParserConfigToken`

```js
import { ApolloBodyParserConfigToken } from "fusion-apollo-universal-client";
// Example for increasing the json limit
app.register(ApolloBodyParserConfigToken, {
jsonLimit: '5mb',
});
```

Optional - Provides body parser config to koa-bodyparser for apollo-server. See https://github.com/koajs/bodyparser


#### gql

```js
Expand Down
47 changes: 46 additions & 1 deletion src/__tests__/integration.node.js
Expand Up @@ -11,6 +11,7 @@ import {
ApolloRenderEnhancer,
GraphQLSchemaToken,
ApolloClientToken,
ApolloBodyParserConfigToken,
} from '../index';
import gql from 'graphql-tag';
import App from 'fusion-react/dist';
Expand All @@ -23,7 +24,7 @@ import http from 'http';
import fetch from 'node-fetch';
import {makeExecutableSchema} from 'graphql-tools';

async function testApp(el, {typeDefs, resolvers}) {
async function testApp(el, {typeDefs, resolvers}, enhanceApp) {
const port = await getPort();
const endpoint = `http://localhost:${port}/graphql`;
const app = new App(el);
Expand All @@ -46,6 +47,9 @@ async function testApp(el, {typeDefs, resolvers}) {
// $FlowFixMe
return {}; // should hit server
});
if (enhanceApp) {
enhanceApp(app);
}
// $FlowFixMe
const server = http.createServer(app.callback());
await new Promise((resolve, reject) =>
Expand Down Expand Up @@ -87,3 +91,44 @@ test('SSR with <Query>', async t => {
server.close();
t.end();
});

test('/graphql endpoint with body parser config', async t => {
const query = gql`
query Test {
test
}
`;
const el = <div />;
const typeDefs = gql`
type Query {
test: String
}
`;
const resolvers = {
Query: {
test(parent, args, ctx) {
t.equal(ctx.path, '/graphql', 'context defaults correctly');
return 'test';
},
},
};
let called = false;
const {server, client} = await testApp(el, {typeDefs, resolvers}, app => {
app.register(ApolloBodyParserConfigToken, {
detectJSON: ctx => {
called = true;
return true;
},
});
});
const result = await client.query({query});
t.ok(called, 'calls detectJSON function');
t.deepEqual(result, {
data: {test: 'test'},
loading: false,
networkStatus: 7,
stale: false,
});
server.close();
t.end();
});
5 changes: 5 additions & 0 deletions src/plugin.js
Expand Up @@ -25,6 +25,7 @@ import {
GraphQLSchemaToken,
GraphQLEndpointToken,
ApolloClientToken,
ApolloBodyParserConfigToken,
} from './tokens';

export type DepsType = {
Expand All @@ -33,6 +34,7 @@ export type DepsType = {
schema: typeof GraphQLSchemaToken.optional,
endpoint: typeof GraphQLEndpointToken.optional,
getApolloClient: typeof ApolloClientToken,
bodyParserConfig: typeof ApolloBodyParserConfigToken.optional,
};

export type ProvidesType = (el: any, ctx: Context) => Promise<any>;
Expand All @@ -45,6 +47,7 @@ function getDeps(): DepsType {
schema: GraphQLSchemaToken.optional,
endpoint: GraphQLEndpointToken.optional,
getApolloClient: ApolloClientToken,
bodyParserConfig: ApolloBodyParserConfigToken.optional,
};
}
// $FlowFixMe
Expand Down Expand Up @@ -73,6 +76,7 @@ export default (renderFn: Render) =>
apolloContext = ctx => {
return ctx;
},
bodyParserConfig = {},
}) {
const renderMiddleware = async (ctx, next) => {
if (!ctx.element) {
Expand Down Expand Up @@ -126,6 +130,7 @@ export default (renderFn: Render) =>
},
// investigate other options
path: endpoint,
bodyParserConfig,
});
return compose([...serverMiddleware, renderMiddleware]);
} else {
Expand Down
16 changes: 16 additions & 0 deletions src/tokens.js
Expand Up @@ -34,3 +34,19 @@ export const GraphQLEndpointToken: Token<string> = createToken(
export const ApolloClientToken: Token<
InitApolloClientType<mixed>
> = createToken('ApolloClientToken');

type BodyParserConfigType = {
enableTypes?: Array<string>,
encoding?: string,
formLimit?: string,
jsonLimit?: string,
textLimit?: string,
strict?: boolean,
detectJSON?: (ctx: Context) => boolean,
extendTypes?: any,
onerror?: (err: any, ctx: Context) => any,
disableBodyParser?: (ctx: Context, next: () => Promise<any>) => Promise<any>,
};
export const ApolloBodyParserConfigToken: Token<BodyParserConfigType> = createToken(
'ApolloBodyParserConfigToken'
);

0 comments on commit 7c26f4d

Please sign in to comment.