Skip to content

Commit d1994d3

Browse files
committed
fix(ElasticSearchApi): Add body arg to POST, PUT methods
1 parent b8947bc commit d1994d3

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

src/ElasticApiParser.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
GraphQLBoolean,
1111
GraphQLObjectType,
1212
GraphQLEnumType,
13+
GraphQLNonNull,
1314
} from 'graphql';
1415
import { GraphQLJSON, upperFirst, TypeComposer } from 'graphql-compose';
1516

@@ -62,6 +63,8 @@ export type ElasticCaSettingsT = {
6263
},
6364
url?: ElasticCaSettingsUrlT,
6465
urls?: ElasticCaSettingsUrlT[],
66+
needBody?: true,
67+
method?: string,
6568
};
6669

6770
export default class ElasticApiParser {
@@ -296,7 +299,14 @@ export default class ElasticApiParser {
296299
settings: ?ElasticCaSettingsT
297300
): GraphQLFieldConfigArgumentMap {
298301
const result = {};
299-
const { params, urls, url } = settings || {};
302+
const { params, urls, url, method, needBody } = settings || {};
303+
304+
if (method === 'POST' || method === 'PUT') {
305+
result.body = {
306+
type: needBody ? new GraphQLNonNull(GraphQLJSON) : GraphQLJSON,
307+
};
308+
}
309+
300310
if (params) {
301311
Object.keys(params).forEach(k => {
302312
const fieldConfig = this.paramToGraphQLArgConfig(params[k], k);

src/__tests__/ElasticApiParser-test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
GraphQLBoolean,
1010
GraphQLObjectType,
1111
GraphQLEnumType,
12+
GraphQLNonNull,
1213
} from 'graphql';
1314
import { GraphQLJSON, TypeComposer } from 'graphql-compose';
1415

@@ -272,6 +273,25 @@ describe('ElasticApiParser', () => {
272273
});
273274

274275
describe('settingsToArgMap()', () => {
276+
it ('should create body arg if POST or PUT method', () => {
277+
const args = parser.settingsToArgMap({
278+
params: {},
279+
method: 'POST',
280+
});
281+
expect(args).toMatchObject({ body: {} });
282+
expect(args.body.type).toEqual(GraphQLJSON);
283+
});
284+
285+
it ('should create required body arg if POST or PUT method', () => {
286+
const args = parser.settingsToArgMap({
287+
params: {},
288+
method: 'POST',
289+
needBody: true,
290+
});
291+
expect(args).toMatchObject({ body: {} });
292+
expect(args.body.type).toBeInstanceOf(GraphQLNonNull);
293+
});
294+
275295
it('should traverse params', () => {
276296
expect(
277297
parser.settingsToArgMap({

src/__tests__/__snapshots__/ElasticApiParser-test.js.snap

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ Object {
3232
"description": "The analyzer to use for the query string",
3333
"type": "String",
3434
},
35+
"body": Object {
36+
"type": "JSON",
37+
},
3538
"defaultOperator": Object {
3639
"defaultValue": "OR",
3740
"type": "ElasticEnum_DefaultOperator",
@@ -179,6 +182,9 @@ Object {
179182
"description": "The analyzer to use for the query string",
180183
"type": "String",
181184
},
185+
"body": Object {
186+
"type": "JSON",
187+
},
182188
"conflicts": Object {
183189
"defaultValue": "abort",
184190
"type": "ElasticEnum_Conflicts",

0 commit comments

Comments
 (0)