3

I am new to GraphQl and learning it. Currently I have single database Table - student_courses as shown below:

student_id|   student_name   | course_code | course_name
1              ABC                S-101       DataStructures
1              ABC                S-150       NLP
1              ABC                S-250       Machine learning
2              PQR                S-101       DataStructures
3              XYZ                S-101       DataStructures
3              XYZ                S-150       NLP

I have mapped the model to single GraphQL object. So I am getting GraphQL API response as individual json objects for each row in table.

I wanted to understand how to group the results of this table by student_id, student_name and get results in below format:

student_id, student_name, {course_code : course_name}

For eg: 1, "ABC", {"S-101":"DataStructures", "S-150":"NLP", "S-250":"Machine learning"}

My current GraphQL query -

{
  student_courses() {
    data {
      student_id
      student_name
      course_code
      course_name
    }
  }
}
skool99
  • 780
  • 1
  • 16
  • 35
  • not query responsibility - resolver role – xadm Mar 15 '21 at 12:26
  • Ok, it will be great if you can point me to some good reference implementation on GraphQL resolver to implement this. Thanks! – skool99 Mar 15 '21 at 13:39
  • IDK ruby, follow some tutorials about ORMs and graphql server in ruby/rails – xadm Mar 15 '21 at 14:14
  • 1
    Here's an answer that's worth a look. Clear explanation, useful examples and a code sandbox, not to mention a very kind response to someone wanting to learn. https://github.com/gatsbyjs/gatsby/issues/27163#issuecomment-701222898 – Kay V Jun 02 '22 at 13:57

1 Answers1

4

It is hard to answer with the little you say about your database and about your backend: graphql is not intelligent about the data.

Some reminders first:

To serve a GraphQL api you need a "graphql backend" you may be using a framework like prisma, apollo server, or if your database is a PostgreSql I recommend postgraphile.

These framework receive and parse Graphql queries, and "resolve" every sub part of a query with a function called a "resolver". They may also help you connect to the database and generate part of the code that you need to get the data.

For example if you have a query like this:

      average {
        points
      }

You need a resolver to get you the "average" field, and another resolver to get the "points" sub field of the "average" object.

Back to your question

Grouping results would be something that you code in your resolver, the graphql engine would not "know" how to ask your database to make a join or a group query.

On postgraphile you can use the pg-aggregates plugin that can do this for you (because it is specialized on binding itself with postgresql database).

You would be able to resolve Graphql queries like this.

query GroupedAggregatesByDerivative {
  allMatchStats {
    byDay: groupedAggregates(groupBy: [CREATED_AT_TRUNCATED_TO_DAY]) {
      keys # The timestamp truncated to the beginning of the day
      average {
        points
      }
    }
    byHour: groupedAggregates(groupBy: [CREATED_AT_TRUNCATED_TO_HOUR]) {
      keys # The timestamp truncated to the beginning of the hour
      average {
        points
      }
    }
  }
}

In practise that will mean that your resolver will take a parameter called "groupBy" and depending on its value return different data.

Remark: on the Graphql schema side the allMatchStats query will have to be modified so that it takes the byDay field as input and its output will not be a "match" but another custom defined type that contains the average field for example. (note that on the case above the postgraphile plugin, generates all this for you)

Dharman
  • 30,962
  • 25
  • 85
  • 135
mpsido
  • 167
  • 4