Is the following type of query possible with Graph...
# prisma-whats-new
n
Is the following type of query possible with Graph.cool/GraphQL?
Copy code
return new Promise((resolve, reject) => {
    _this.aggregate([
      {$match: {url}},
      {$group: {
        _id: "$emoji",
        count: {$sum: 1},
        ips: {$push: "$ip"}
      }},
      {$project: {
        _id: 0,
        emoji: "$_id",
        count: 1,
        reacted: {
          $setIsSubset: [[ip], "$ips" ]
        }
      }}
    ], (err, result) => {
      if (err) return reject(err)

      resolve(result)
    })
n
@notrab here's some information on aggregation in GraphQL: https://www.graph.cool/docs/faq/aggregation-in-graphql-eip2ophoof/
n
Thanks, that’s a good start. Quite interested to see if anyone is building complex queries like the above with graphcool
A similar comparison to the data I’m storing is like the Graphcool feedback question at the bottom of the page. I’m guessing you’re viewing the results of that somewhere?
The % of Yes vs. No
It’s that type of query result I’m looking to return.
n
You can combine the
count
with filters and do % or other calculations client side
that's what we currently do for all kind of stuff 😉
but having a baked-in aggregation system will be super powerful
n
May have to come back to this Q when I’ve experimented more 😄
Basically need to get the count of all each type of emoji reacted, also need to return whether the user has voted or not for that emoji type.
n
how many different emojis do you have?
n
5
n
will this number change?
n
Possibly.
I do agree with you that an aggregation system would be sweet.
n
right now you would need to manually write the different count queries for every emoji I guess
n
Doing this on the frontend seems overkill, when the query could do it 😄
n
But there might be another way
I guess the emoji is currently a string field?
n
yah
Current response with my mongo db..
Copy code
[
{
emoji: "sad",
count: 2,
reacted: false
},
{
emoji: "happy",
count: 2,
reacted: false
},
{
emoji: "sad",
count: 0,
reacted: false
},
{
emoji: "love",
count: 0,
reacted: false
},
{
emoji: "poop",
count: 0,
reacted: false
}
]
n
ah so, is this part of a
Reaction
type?
n
reacted
is calculated by the current IP vs a stored IP with that same emoji.
n
yea so again there's no grouping mechanism yet
but you could do something like this
Copy code
query {
  sadCount: _allReactionsMeta(filter: {
    emoji: "sad"
  }) {
    count
  }
  poopCount: _allReactionsMeta(filter: {
    emoji: "poop"
  }) {
    count
  }

}
a bit tedious, but at least something 😉
n
I like it.
n
🙂
n
Would there be a way to return the emoji too?
n
no, but there is some logic anyway when constructing the query what emoji it is, right?
n
I guess I could just rename
happyCount
to
happy
n
yea
n
The final mystery would be checking if the IP is already stored with that reaction & emoji.
Copy code
type Reaction {
  createdAt: DateTime!
  emoji: String!
  id: ID!
  ip: String
  updatedAt: DateTime!
  url: String!
}
n
Well if the data is in your project then we can certainly figure this out. What's the data model look like?
n
I guess there isn’t any scope based validation with graphcool yet?
n
Either we can count them and check if it's 0, or we use
allReactions
and see if the response is empty
what do you mean by that?
n
Reaction
would have validation on the emoji field scoped by IP. So there can be multiple ‘happy’ Reactions, but only by one IP.
Per URL
Graphcool docs for example… You can submit multiple feedback responses per page.
n
if you elevated the IP or emoji to another model, that would be possible already: https://github.com/graphcool/feature-requests/issues/65
you could also encode it yourself
have a string with
${ip}_${emoji}
that's unique
n
True true
n
of course, evil people could figure it out
is this a concern in your context?
n
I’m working on a small side project, mainly to get to grips with GraphQL. I’ve built it using Mongo already but the idea to create a simple embed widget for feedback, with Emoji.
Not too much of a concern
n
🙂
Emoji's are the best! hehe
n
😄
Thanks for your help, are those issues added to any kind of roadmap?
n
I recently tagged all feature requests with different
stage
labels that give you an idea of the current stage 🙂
We're working on a better visual representation for this
n
❤️ 1
n
awesome!