Skip to content

Commit 783ee1b

Browse files
pdrvskybirkir
authored andcommitted
feat(query): add possibility to filter query by subdocuments id (#282)
1 parent f44ae0e commit 783ee1b

3 files changed

Lines changed: 17 additions & 2 deletions

File tree

packages/prime-core/src/modules/external/utils/documentWhereBuilder.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const operators = {
2626
in: 'IN',
2727
contains: 'LIKE',
2828
not: '!=',
29+
id: 'SIMILAR TO',
2930
};
3031

3132
const modes = { OR: 'orWhere', AND: 'andWhere' };
@@ -59,6 +60,8 @@ export const documentWhereBuilder = (
5960
let value = whereOrValue;
6061
if (fieldName === 'contains') {
6162
value = `%${value}%`;
63+
} else if (fieldName === 'id') {
64+
value = `%(,${value}("|\\Z))%`;
6265
}
6366
const key = Buffer.from(value).toString('hex');
6467
queryBuilder[mode](`${column} ${operator} :${key}`, { [key]: value });

packages/prime-field-document/__tests__/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ describe('PrimeFieldDocument', () => {
4545
expect(typeof PrimeFieldDocument).toBe('function');
4646
});
4747

48-
it('should have null where type', async () => {
49-
expect(await test.whereType(createContext())).toBeNull();
48+
it('should have where type', async () => {
49+
test = createTestField();
50+
expect(await test.whereType(createContext())).toBeTruthy();
5051
});
5152

5253
it('should have null output type with no options', async () => {

packages/prime-field-document/src/PrimeFieldDocument.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { PrimeField, PrimeFieldContext } from '@primecms/field';
22
import {
33
GraphQLID,
4+
GraphQLInputObjectType,
45
GraphQLList,
56
GraphQLObjectType,
67
GraphQLString,
78
GraphQLUnionType,
89
} from 'graphql';
10+
import { camelCase, upperFirst } from 'lodash';
911

1012
interface Options {
1113
schemaIds: string[];
@@ -121,4 +123,13 @@ export class PrimeFieldDocument extends PrimeField {
121123
type: options.multiple ? new GraphQLList(GraphQLID) : GraphQLID,
122124
};
123125
}
126+
127+
public async whereType(context: PrimeFieldContext) {
128+
return new GraphQLInputObjectType({
129+
name: `${context.name}_${upperFirst(camelCase(this.schemaField.name))}_Where`,
130+
fields: {
131+
id: { type: GraphQLID },
132+
},
133+
});
134+
}
124135
}

0 commit comments

Comments
 (0)