Skip to content

Commit

Permalink
feat: allow to filter for nested data
Browse files Browse the repository at this point in the history
  • Loading branch information
macrozone committed May 20, 2020
1 parent 0a4ad4e commit d32bf49
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 25 deletions.
62 changes: 62 additions & 0 deletions packages/dataprovider/src/buildWhere.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<NexusGenArgTypes["Query"]["users"]["where"]>({
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 () => {
//

Expand Down
68 changes: 44 additions & 24 deletions packages/dataprovider/src/buildWhere.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const getFilters = (
key: string,
value: any,
whereType: IntrospectionInputObjectType,

introspectionResults: IntrospectionResult,
) => {
const fieldType = whereType.inputFields.find((f) => f.name === key)
Expand Down Expand Up @@ -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 };
};
Expand All @@ -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),
),
};
}
Expand All @@ -128,10 +136,22 @@ export const buildWhere = (
key,
filter[key],
whereType,

introspectionResults,
);

return { ...acc, ...filters };
}, {});
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);
};
2 changes: 1 addition & 1 deletion packages/dataprovider/test-data/datamodel.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ model User {
yearOfBirth Int?
wantsNewsletter Boolean
userSocialMedia UserSocialMedia?
}
}
1 change: 1 addition & 0 deletions packages/dataprovider/test-data/testSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const types = [
User,
UserRole,
UserSocialMedia,

addCrudResolvers("User"),
addCrudResolvers("UserRole"),
];
Expand Down

0 comments on commit d32bf49

Please sign in to comment.