mirror of
https://github.com/strapi/strapi.git
synced 2025-11-08 06:07:41 +00:00
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:
parent
435b294439
commit
e53b54c218
@ -1,6 +1,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
|
const { QUERY_OPERATORS } = require('strapi-utils');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Merges
|
* Merges
|
||||||
@ -58,7 +59,9 @@ const convertToQuery = params => {
|
|||||||
const result = {};
|
const result = {};
|
||||||
|
|
||||||
_.forEach(params, (value, key) => {
|
_.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);
|
const flatObject = convertToQuery(value);
|
||||||
_.forEach(flatObject, (_value, _key) => {
|
_.forEach(flatObject, (_value, _key) => {
|
||||||
result[`${key}.${_key}`] = _value;
|
result[`${key}.${_key}`] = _value;
|
||||||
|
|||||||
@ -278,6 +278,49 @@ describe('Test Graphql API End to End', () => {
|
|||||||
},
|
},
|
||||||
[postsPayload[0]],
|
[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) => {
|
])('List posts with where clause %o', async (where, expected) => {
|
||||||
const res = await graphqlQuery({
|
const res = await graphqlQuery({
|
||||||
query: /* GraphQL */ `
|
query: /* GraphQL */ `
|
||||||
|
|||||||
@ -8,6 +8,9 @@ const {
|
|||||||
constants: { DP_PUB_STATES },
|
constants: { DP_PUB_STATES },
|
||||||
} = require('./content-types');
|
} = require('./content-types');
|
||||||
|
|
||||||
|
const BOOLEAN_OPERATORS = ['or'];
|
||||||
|
const QUERY_OPERATORS = ['_where', '_or'];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Global converter
|
* Global converter
|
||||||
* @param {Object} params
|
* @param {Object} params
|
||||||
@ -154,8 +157,6 @@ const VALID_REST_OPERATORS = [
|
|||||||
'null',
|
'null',
|
||||||
];
|
];
|
||||||
|
|
||||||
const BOOLEAN_OPERATORS = ['or'];
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse where params
|
* Parse where params
|
||||||
*/
|
*/
|
||||||
@ -216,4 +217,5 @@ const convertWhereClause = (whereClause, value) => {
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
convertRestQueryParams,
|
convertRestQueryParams,
|
||||||
VALID_REST_OPERATORS,
|
VALID_REST_OPERATORS,
|
||||||
|
QUERY_OPERATORS,
|
||||||
};
|
};
|
||||||
|
|||||||
@ -4,7 +4,11 @@
|
|||||||
* Export shared utilities
|
* Export shared utilities
|
||||||
*/
|
*/
|
||||||
const { buildQuery, hasDeepFilters } = require('./build-query');
|
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 parseMultipartData = require('./parse-multipart');
|
||||||
const sanitizeEntity = require('./sanitize-entity');
|
const sanitizeEntity = require('./sanitize-entity');
|
||||||
const parseType = require('./parse-type');
|
const parseType = require('./parse-type');
|
||||||
@ -38,6 +42,7 @@ module.exports = {
|
|||||||
templateConfiguration,
|
templateConfiguration,
|
||||||
convertRestQueryParams,
|
convertRestQueryParams,
|
||||||
VALID_REST_OPERATORS,
|
VALID_REST_OPERATORS,
|
||||||
|
QUERY_OPERATORS,
|
||||||
buildQuery,
|
buildQuery,
|
||||||
hasDeepFilters,
|
hasDeepFilters,
|
||||||
parseMultipartData,
|
parseMultipartData,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user