Support query operators _or & _where in graphql with deep nesting (#8332)

* SUpport query operators _or _where in graphql with deep nesting

Signed-off-by: Alexandre Bodin <bodin.alex@gmail.com>

* Add testing for complexe graphql queries

Signed-off-by: Alexandre Bodin <bodin.alex@gmail.com>
This commit is contained in:
Alexandre BODIN 2020-10-15 13:22:40 +02:00 committed by GitHub
parent 435b294439
commit e53b54c218
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 57 additions and 4 deletions

View File

@ -1,6 +1,7 @@
'use strict';
const _ = require('lodash');
const { QUERY_OPERATORS } = require('strapi-utils');
/**
* Merges
@ -58,7 +59,9 @@ const convertToQuery = params => {
const result = {};
_.forEach(params, (value, key) => {
if (_.isPlainObject(value)) {
if (QUERY_OPERATORS.includes(key)) {
result[key] = _.isArray(value) ? value.map(convertToQuery) : convertToQuery(value);
} else if (_.isPlainObject(value)) {
const flatObject = convertToQuery(value);
_.forEach(flatObject, (_value, _key) => {
result[`${key}.${_key}`] = _value;

View File

@ -278,6 +278,49 @@ describe('Test Graphql API End to End', () => {
},
[postsPayload[0]],
],
[
{
_or: [{ name_in: ['post 2'] }, { bigint_eq: 1316130638171 }],
},
[postsPayload[0], postsPayload[1]],
],
[
{
_where: { nullable_null: false },
},
[postsPayload[0]],
],
[
{
_where: { _or: { nullable_null: false } },
},
[postsPayload[0]],
],
[
{
_where: [{ nullable_null: false }],
},
[postsPayload[0]],
],
[
{
_where: [{ _or: [{ name_in: ['post 2'] }, { bigint_eq: 1316130638171 }] }],
},
[postsPayload[0], postsPayload[1]],
],
[
{
_where: [
{
_or: [
{ name_in: ['post 2'] },
{ _or: [{ bigint_eq: 1316130638171 }, { nullable_null: false }] },
],
},
],
},
[postsPayload[0], postsPayload[1]],
],
])('List posts with where clause %o', async (where, expected) => {
const res = await graphqlQuery({
query: /* GraphQL */ `

View File

@ -8,6 +8,9 @@ const {
constants: { DP_PUB_STATES },
} = require('./content-types');
const BOOLEAN_OPERATORS = ['or'];
const QUERY_OPERATORS = ['_where', '_or'];
/**
* Global converter
* @param {Object} params
@ -154,8 +157,6 @@ const VALID_REST_OPERATORS = [
'null',
];
const BOOLEAN_OPERATORS = ['or'];
/**
* Parse where params
*/
@ -216,4 +217,5 @@ const convertWhereClause = (whereClause, value) => {
module.exports = {
convertRestQueryParams,
VALID_REST_OPERATORS,
QUERY_OPERATORS,
};

View File

@ -4,7 +4,11 @@
* Export shared utilities
*/
const { buildQuery, hasDeepFilters } = require('./build-query');
const { convertRestQueryParams, VALID_REST_OPERATORS } = require('./convert-rest-query-params');
const {
convertRestQueryParams,
VALID_REST_OPERATORS,
QUERY_OPERATORS,
} = require('./convert-rest-query-params');
const parseMultipartData = require('./parse-multipart');
const sanitizeEntity = require('./sanitize-entity');
const parseType = require('./parse-type');
@ -38,6 +42,7 @@ module.exports = {
templateConfiguration,
convertRestQueryParams,
VALID_REST_OPERATORS,
QUERY_OPERATORS,
buildQuery,
hasDeepFilters,
parseMultipartData,