-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpinQueries.js
More file actions
125 lines (120 loc) · 3.19 KB
/
pinQueries.js
File metadata and controls
125 lines (120 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/* For understanding how text matching in Sanity GROG works: refer https://www.sanity.io/docs/query-cheat-sheet#170b92d4caa2
Basically our 1st query returns all pins whose title/category/about starts with the "searchTerm" word.
But we don't want each and every field, so we can project the fields we want (This property is similar to GQL and advantage over REST).
In our Case, the fields of each pin we want are: [For Docs refer: https://www.sanity.io/docs/how-queries-work]
1. _id , 2. image [image is a special Sanity type which has an asset reference field and form that we just want the url field hence the given syntax used]. -> is dereferencing operator which allows to populate the fields of reference otherwise only reference id is returned (in our case, we wanted url from asset),
3. destination, 4.postedBy(since it is ref, we dereferenced the required fields from it),
5. save (since it is an array,we add [] after the name and from that we want only postedBy and key which is unique for array element generated by Sanity, hence _ before it).
*/
// Get Pins whose category/title/about field start with given searchTerm
export const searchPinsQuery = searchTerm => {
const query = `*[_type=="pin" && title match "${searchTerm}*" || category match "${searchTerm}*" || about match "${searchTerm}*"]{
_id,
image{
asset->{
url
}
},
destination,
postedBy->{
_id,
userName,
avatar
},
save[]{
_key,
postedBy->{
_id,
userName,
avatar
},
},
}`;
return query;
};
// Get all Pins
export const feedQuery = `*[_type == "pin"] | order(_createdAt desc) {
_id,
image{
asset-> {
url
}
},
destination,
postedBy->{
_id,
userName,
avatar
},
save[]{
_key,
postedBy->{
_id,
userName,
avatar
},
},
} `;
// Get Pin by id
export const getPinByIdQuery = pinId => {
const query = `*[_type=="pin" && _id=="${pinId}"]{
image{
asset->{
url
}
},
_id,
title,
about,
category,
destination,
postedBy->{
_id,
userName,
avatar
},
save[]{
postedBy->{
_id,
userName,
avatar
},
},
comments[]{
comment,
_key,
postedBy->{
_id,
userName,
avatar
},
}
}`;
return query;
};
// Get Pins similar to given Pin (similarity based on Category)
export const getPinSuggestionsQuery = pin => {
const query = `*[_type=="pin" && category=="${pin.category}" && _id!="${pin._id}"]{
image{
asset->{
url
}
},
_id,
destination,
postedBy->{
_id,
userName,
avatar
},
save[]{
_key,
postedBy->{
_id,
userName,
avatar
},
},
}`;
return query;
};