Skip to content

Commit d18f955

Browse files
committed
✨ Support exists condition
1 parent 4bd6a0a commit d18f955

File tree

3 files changed

+59
-4
lines changed

3 files changed

+59
-4
lines changed

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
- [获取数据](#%E8%8E%B7%E5%8F%96%E6%95%B0%E6%8D%AE)
99
- [查询条件](#%E6%9F%A5%E8%AF%A2%E6%9D%A1%E4%BB%B6)
1010
* [equalTo](#equalto)
11+
* [exists](#exists)
1112
* [范围查询](#%E8%8C%83%E5%9B%B4%E6%9F%A5%E8%AF%A2)
13+
* [数组查询](#%E6%95%B0%E7%BB%84%E6%9F%A5%E8%AF%A2)
14+
* [组合查询](#%E7%BB%84%E5%90%88%E6%9F%A5%E8%AF%A2)
1215
- [关系查询](#%E5%85%B3%E7%B3%BB%E6%9F%A5%E8%AF%A2)
1316
* [Relation](#relation)
1417
* [Pointer](#pointer)
@@ -148,6 +151,18 @@ query {
148151
}
149152
```
150153

154+
### exists
155+
156+
exists 可以用来查询存在或不存在某一字段的对象,例如我们查询存在 title 但不存在 content 的 Todo:
157+
158+
```graphql
159+
query {
160+
Todo(exists: {title: true, content: false}) {
161+
title, content
162+
}
163+
}
164+
```
165+
151166
### 范围查询
152167

153168
```graphql
@@ -180,6 +195,18 @@ query {
180195
- `containedIn` 约束指定列中包含特定元素。
181196
- `containsAll` 约束指定列中包含所有元素。
182197

198+
### 组合查询
199+
200+
你可以将我们前面提到的所有查询条件组合在一起:
201+
202+
```graphql
203+
query {
204+
Todo(exists: {content: true}, ascending: priority, greaterThan: {priority: 5}) {
205+
title, content, priority
206+
}
207+
}
208+
```
209+
183210
## 关系查询
184211

185212
### Relation

schema.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,9 @@ module.exports = function buildSchema({appId, appKey, masterKey}) {
176176
},
177177
containsAll: {
178178
type: createFieldsInputType('containsAll', new GraphQLList(GraphQLID))
179+
},
180+
exists: {
181+
type: createFieldsInputType('exists', GraphQLBoolean)
179182
}
180183
},
181184
resolve: (source, args, {authOptions}, info) => {
@@ -187,17 +190,25 @@ module.exports = function buildSchema({appId, appKey, masterKey}) {
187190
}
188191
});
189192

190-
['equalTo', 'greaterThan', 'greaterThanOrEqualTo', 'lessThan', 'lessThanOrEqualTo',
191-
'containedIn', 'containsAll'].forEach( method => {
193+
['equalTo', 'greaterThan', 'greaterThanOrEqualTo', 'lessThan',
194+
'lessThanOrEqualTo', 'containedIn', 'containsAll'].forEach( method => {
192195
if (_.isObject(args[method])) {
193196
_.forEach(args[method], (value, key) => {
194197
query[method](key, value);
195198
});
196-
} else {
197-
debug(`Ignored argument ${method}`);
198199
}
199200
});
200201

202+
if (_.isObject(args.exists)) {
203+
_.forEach(args.exists, (value, key) => {
204+
if (value) {
205+
query.exists(key);
206+
} else {
207+
query.doesNotExist(key);
208+
}
209+
});
210+
}
211+
201212
if (args.objectId) {
202213
query.equalTo('objectId', args.objectId);
203214
}

test/query.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const {expect} = require('chai');
2+
13
const requestGraphQL = require('./client');
24

35
describe('query', function() {
@@ -98,4 +100,19 @@ describe('query', function() {
98100
});
99101
});
100102
});
103+
104+
it('should work with exists', () => {
105+
return requestGraphQL(`
106+
query {
107+
Todo(exists: {title: true, content: false}) {
108+
title, content
109+
}
110+
}
111+
`).then( res => {
112+
res.body.data.Todo.forEach( ({title, content}) => {
113+
title.should.be.a('string');
114+
expect(content).to.not.exist;
115+
});
116+
});
117+
});
101118
});

0 commit comments

Comments
 (0)