enables querying on repeaters

This commit is contained in:
James
2020-04-10 15:55:47 -04:00
parent 5eda424959
commit a62e40066b
5 changed files with 74 additions and 8 deletions

View File

@@ -38,7 +38,7 @@ function getBuildObjectType(graphQLContext) {
if (typeof option === 'object' && option.value) {
return {
...values,
[formatName(option.label)]: {
[formatName(option.value)]: {
value: option.value,
},
};

View File

@@ -1,3 +1,4 @@
/* eslint-disable no-use-before-define */
const {
GraphQLString,
GraphQLFloat,
@@ -13,8 +14,52 @@ const formatName = require('../utilities/formatName');
const combineParentName = require('../utilities/combineParentName');
const withOperators = require('./withOperators');
// Pass context to buildWhereInputType - it may be needed in the future
// for localization or similar as is the case with buildObjectType
const getBuildWhereInputType = (graphQLContext) => {
// buildWhereInputType is similar to buildObjectType and operates
// on a field basis with a few distinct differences.
//
// 1. Everything needs to be a GraphQLInputObjectType or scalar / enum
// 2. Relationships, groups, repeaters and flex content are not
// directly searchable. Instead, we need to build a chained pathname
// using dot notation so Mongo can properly search nested paths.
const buildWhereInputType = (name, fields, parentName) => {
// This is the function that builds nested paths for all
// field types with nested paths.
const recursivelyBuildNestedPaths = (field) => {
const nestedPaths = field.fields.reduce((nestedFields, nestedField) => {
const getFieldSchema = fieldToSchemaMap[nestedField.type];
const nestedFieldName = `${field.name}__${nestedField.name}`;
if (getFieldSchema) {
const fieldSchema = getFieldSchema({
...nestedField,
name: nestedFieldName,
});
if (Array.isArray(fieldSchema)) {
return [
...nestedFields,
...fieldSchema,
];
}
return [
...nestedFields,
{
key: nestedFieldName,
type: fieldSchema,
},
];
}
return nestedFields;
}, []);
return nestedPaths;
};
const fieldToSchemaMap = {
number: (field) => {
const type = GraphQLFloat;
@@ -116,7 +161,7 @@ const getBuildWhereInputType = (graphQLContext) => {
if (typeof option === 'object' && option.value) {
return {
...values,
[formatName(option.label)]: {
[formatName(option.value)]: {
value: option.value,
},
};
@@ -138,15 +183,32 @@ const getBuildWhereInputType = (graphQLContext) => {
['in', 'not_in', 'all', 'equals', 'not_equals'],
),
}),
repeater: (field) => {
return recursivelyBuildNestedPaths(field);
},
};
const fieldTypes = fields.reduce((schema, field) => {
const getFieldSchema = fieldToSchemaMap[field.type];
if (getFieldSchema) {
const fieldSchema = getFieldSchema(field);
if (Array.isArray(fieldSchema)) {
return {
...schema,
...(fieldSchema.reduce((subFields, subField) => {
return {
...subFields,
[subField.key]: subField.type,
};
}, {})),
};
}
return {
...schema,
[field.name]: getFieldSchema(field),
[field.name]: fieldSchema,
};
}

View File

@@ -1,9 +1,11 @@
const uppercase = require('./uppercase');
const formatName = (string) => {
let formatted = string.replace(/-|\//g, '_');
formatted = uppercase(formatted);
formatted = formatted.replace(/ /g, '');
const formatted = string
.replace(/\./g, '_')
.replace(/-|\//g, '_')
.replace(/ /g, '');
return formatted;
};

View File

@@ -2,7 +2,7 @@ function uppercase(str) {
const array1 = str.split(' ');
const newarray1 = [];
for (let x = 0; x < array1.length; x++) {
for (let x = 0; x < array1.length; x += 1) {
newarray1.push(array1[x].charAt(0).toUpperCase() + array1[x].slice(1));
}
return newarray1.join(' ');

View File

@@ -112,11 +112,13 @@ class ParamParser {
let localizedKey = this.getLocalizedKey(key, schemaObject);
localizedKey = key.split('__').join('.');
if (key === '_id' || key === 'id') {
localizedKey = '_id';
}
if (key.includes('.')) {
if (key.includes('.') || key.includes('__')) {
const paths = key.split('.');
schemaObject = schema.obj[paths[0]];
const localizedPath = this.getLocalizedKey(paths[0], schemaObject);