Skip to content

Commit 679d4d8

Browse files
authored
Merge pull request #113 from vantreeseba/add_neq_filter
Add neq filter
2 parents 61e1033 + 4579e4c commit 679d4d8

File tree

4 files changed

+55
-1
lines changed

4 files changed

+55
-1
lines changed

README.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,17 @@ type Post {
122122
type PostFilter {
123123
q: String
124124
id: ID
125+
id_neq: ID
125126
title: String
127+
title_neq: String
126128
views: Int
127129
views_lt: Int
128130
views_lte: Int
129131
views_gt: Int
130132
views_gte: Int
133+
views_neq: Int
131134
user_id: ID
135+
user_id_neq: ID
132136
}
133137
type ListMetadata {
134138
count: Int!
@@ -140,7 +144,7 @@ By convention, json-graphql-server expects all entities to have an `id` field th
140144

141145
For every field named `*_id`, json-graphql-server creates a two-way relationship, to let you fetch related entities from both sides. For instance, the presence of the `user_id` field in the `posts` entity leads to the ability to fetch the related `User` for a `Post` - and the related `Posts` for a `User`.
142146

143-
The `all*` queries accept parameters to let you sort, paginate, and filter the list of results. You can filter by any field, not just the primary key. For instance, you can get the posts written by user `123`. Json-graphql-server also adds a full-text query field named `q`, and created range filter fields for numeric and date fields. The detail of all available filters can be seen in the generated `*Filter` type.
147+
The `all*` queries accept parameters to let you sort, paginate, and filter the list of results. You can filter by any field, not just the primary key. For instance, you can get the posts written by user `123`. Json-graphql-server also adds a full-text query field named `q`, and created range filter fields for numeric and date fields. All types (excluding booleans and arrays) get a not equal filter. The detail of all available filters can be seen in the generated `*Filter` type.
144148

145149
## GraphQL Usage
146150

@@ -361,6 +365,32 @@ Here is how you can use the queries and mutations generated for your data, using
361365
</pre>
362366
</td>
363367
</tr>
368+
<tr>
369+
<td>
370+
<pre>
371+
// all fields (except boolean and array) get not equal filters
372+
// -lt, _lte, -gt, and _gte
373+
{
374+
allPosts(filter: { title_neq: "Lorem Ipsum" }) {
375+
title
376+
views
377+
}
378+
}
379+
</pre>
380+
</td>
381+
<td>
382+
<pre>
383+
{
384+
"data": {
385+
"allPosts": [
386+
{ "title": "Some Other Title", views: 254 },
387+
]
388+
}
389+
}
390+
</pre>
391+
</td>
392+
</tr>
393+
364394
<tr>
365395
<td>
366396
<pre>

src/introspection/getFilterTypesFromData.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
GraphQLBoolean,
23
GraphQLInputObjectType,
34
GraphQLString,
45
GraphQLInt,
@@ -29,6 +30,10 @@ const getRangeFiltersFromEntities = (entities) => {
2930
fields[`${fieldName}_gt`] = { type: fieldType };
3031
fields[`${fieldName}_gte`] = { type: fieldType };
3132
}
33+
34+
if (fieldType != GraphQLBoolean && fieldType != GraphQLList) {
35+
fields[`${fieldName}_neq`] = { type: fieldType };
36+
}
3237
return fields;
3338
}, {});
3439
};

src/resolver/Query/applyFilters.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ export default (entityData = [], filter = {}) => {
77
Object.keys(filter)
88
.filter((key) => key !== 'q')
99
.forEach((key) => {
10+
if (key.indexOf('_neq') !== -1) {
11+
// not equal to
12+
const realKey = key.replace(/(_neq)$/, '');
13+
items = items.filter((d) => d[realKey] != filter[key]);
14+
return;
15+
}
1016
if (key.indexOf('_lte') !== -1) {
1117
// less than or equal
1218
const realKey = key.replace(/(_lte)$/, '');

src/resolver/Query/applyFilters.spec.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,19 @@ test('filters by value on each field using the related filter', () => {
9595
]);
9696
});
9797

98+
test('filters by not equals given fields', () => {
99+
expect(applyFilters(data, { id_neq: 2 })).toEqual([
100+
{
101+
id: 1,
102+
title: 'Lorem Ipsum',
103+
user_id: 123,
104+
views: 254,
105+
tags: ['foo', 'bar'],
106+
},
107+
{ id: 3, title: 'Sic Dolor amet', user_id: 123, views: 76, tags: [] },
108+
]);
109+
});
110+
98111
test('filters by value range on each integer field using the related filters', () => {
99112
expect(applyFilters(data, { views_lt: 76 })).toEqual([
100113
{

0 commit comments

Comments
 (0)