Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(query): add in and notIn filters to NumberQuery #150

Merged
merged 3 commits into from
Oct 29, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/comparators/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,10 @@ export const numberComparators: QueryToComparator<NumberQuery> = {
lte(expected, actual) {
return actual <= expected
},
in(expected, actual) {
return expected.includes(actual)
},
notIn(expected, actual) {
return !numberComparators.in(expected, actual)
},
}
3 changes: 2 additions & 1 deletion src/model/generateGraphQLHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ function createComparatorGraphQLInputType(
name,
fields: Object.keys(comparators).reduce<GraphQLInputFieldConfigMap>(
(fields, comparatorFn) => {
fields[comparatorFn] = { type }
const fieldType = ['between', 'notBetween', 'in', 'notIn'].includes(comparatorFn) ? GraphQLList(type) : type
fields[comparatorFn] = { type: fieldType }
return fields
},
{},
Expand Down
2 changes: 2 additions & 0 deletions src/query/queryTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ export interface NumberQuery {
gte: number
lt: number
lte: number
in: number[]
notIn: number[]
}

export interface BooleanQuery {
Expand Down
14 changes: 8 additions & 6 deletions test/model/toGraphQLSchema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,30 @@ test('generates a graphql schema', () => {
notEquals: ID
contains: ID
notContains: ID
in: ID
notIn: ID
in: [ID]
notIn: [ID]
}

input StringQueryType {
equals: String
notEquals: String
contains: String
notContains: String
in: String
notIn: String
in: [String]
notIn: [String]
}

input IntQueryType {
equals: Int
notEquals: Int
between: Int
notBetween: Int
between: [Int]
notBetween: [Int]
gt: Int
gte: Int
lt: Int
lte: Int
in: [Int]
notIn: [Int]
}

type Mutation {
Expand Down
28 changes: 28 additions & 0 deletions test/query/number.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,31 @@ test('queries entities that are younger or equal a number', () => {
const names = users.map((user) => user.firstName)
expect(names).toEqual(['John', 'Alice'])
})

test('queries entities where property is not contained into the array', () => {
const db = setup()

const users = db.user.findMany({
where: {
age: {
notIn: [16, 24],
},
},
})
const names = users.map((user) => user.firstName)
expect(names).toEqual(['Kate'])
})

test('queries entities where property is contained into the array', () => {
const db = setup()

const users = db.user.findMany({
where: {
age: {
in: [16, 24],
},
},
})
const names = users.map((user) => user.firstName)
expect(names).toEqual(['John', 'Alice'])
})