diff --git a/packages/dataprovider/src/buildWhere.test.ts b/packages/dataprovider/src/buildWhere.test.ts index bb508f2..2ad884a 100644 --- a/packages/dataprovider/src/buildWhere.test.ts +++ b/packages/dataprovider/src/buildWhere.test.ts @@ -172,6 +172,68 @@ describe("buildWhere", () => { ], }); }); + it("allows to filter nested data as well", async () => { + // + + const filter = { + OR: [ + { + firstName: "fooBar", + }, + { + userSocialMedia: { + instagram: "fooBar", + }, + }, + ], + }; + const result = buildWhere(filter, testUserResource, testIntrospection); + + expect(result).toEqual({ + OR: [ + { + OR: [ + { + firstName: { + contains: "fooBar", + }, + }, + { + firstName: { + contains: "foobar", + }, + }, + { + firstName: { + contains: "FooBar", + }, + }, + ], + }, + { + userSocialMedia: { + OR: [ + { + instagram: { + contains: "fooBar", + }, + }, + { + instagram: { + contains: "foobar", + }, + }, + { + instagram: { + contains: "FooBar", + }, + }, + ], + }, + }, + ], + }); + }); it("allows to NOT find certain fields", async () => { // diff --git a/packages/dataprovider/src/buildWhere.ts b/packages/dataprovider/src/buildWhere.ts index fd6b750..cd0159f 100644 --- a/packages/dataprovider/src/buildWhere.ts +++ b/packages/dataprovider/src/buildWhere.ts @@ -26,6 +26,7 @@ const getFilters = ( key: string, value: any, whereType: IntrospectionInputObjectType, + introspectionResults: IntrospectionResult, ) => { const fieldType = whereType.inputFields.find((f) => f.name === key) @@ -65,34 +66,45 @@ const getFilters = ( }, }; } - if (!isObject(value) && fieldType.kind === "INPUT_OBJECT") { + if (fieldType.kind === "INPUT_OBJECT") { // we asume for the moment that this is always a relation const inputObjectType = introspectionResults.types.find( (t) => t.name === fieldType.name, ) as IntrospectionInputObjectType; - //console.log({ inputObjectType }); - const hasSomeFilter = inputObjectType.inputFields.some( - (s) => s.name === "some", - ); - if (hasSomeFilter) { + if (!isObject(value)) { + //console.log({ inputObjectType }); + const hasSomeFilter = inputObjectType.inputFields.some( + (s) => s.name === "some", + ); + + if (hasSomeFilter) { + return { + [key]: { + some: { + id: { + equals: value, + }, + }, + }, + }; + } return { [key]: { - some: { - id: { - equals: value, - }, + id: { + equals: value, }, }, }; + } else { + // its something nested + const where = buildWhereWithType( + value, + introspectionResults, + inputObjectType, + ); + return { [key]: where }; } - return { - [key]: { - id: { - equals: value, - }, - }, - }; } return { [key]: value }; }; @@ -105,21 +117,17 @@ type Filter = { [key: string]: any; }; -export const buildWhere = ( +const buildWhereWithType = ( filter: Filter, - resource: Resource, introspectionResults: IntrospectionResult, + whereType: IntrospectionInputObjectType, ) => { - const whereType = introspectionResults.types.find( - (t) => t.name === `${resource.type.name}WhereInput`, - ) as IntrospectionInputObjectType; - const where = Object.keys(filter ?? {}).reduce((acc, key) => { if (key === "NOT" || key === "OR" || key === "AND") { return { ...acc, [key]: filter[key].map((f) => - buildWhere(f, resource, introspectionResults), + buildWhereWithType(f, introspectionResults, whereType), ), }; } @@ -128,6 +136,7 @@ export const buildWhere = ( key, filter[key], whereType, + introspectionResults, ); @@ -135,3 +144,14 @@ export const buildWhere = ( }, {}); return where; }; +export const buildWhere = ( + filter: Filter, + resource: Resource, + introspectionResults: IntrospectionResult, +) => { + const whereType = introspectionResults.types.find( + (t) => t.name === `${resource.type.name}WhereInput`, + ) as IntrospectionInputObjectType; + + return buildWhereWithType(filter, introspectionResults, whereType); +}; diff --git a/packages/dataprovider/test-data/datamodel.prisma b/packages/dataprovider/test-data/datamodel.prisma index 66c535d..b2a3e23 100644 --- a/packages/dataprovider/test-data/datamodel.prisma +++ b/packages/dataprovider/test-data/datamodel.prisma @@ -37,4 +37,4 @@ model User { yearOfBirth Int? wantsNewsletter Boolean userSocialMedia UserSocialMedia? -} +} \ No newline at end of file diff --git a/packages/dataprovider/test-data/testSchema.ts b/packages/dataprovider/test-data/testSchema.ts index fb08806..4fa9d07 100644 --- a/packages/dataprovider/test-data/testSchema.ts +++ b/packages/dataprovider/test-data/testSchema.ts @@ -52,6 +52,7 @@ const types = [ User, UserRole, UserSocialMedia, + addCrudResolvers("User"), addCrudResolvers("UserRole"), ];