graphqlgraphql-jshasuragraphiql

Hasura: How to group same variables in columns and count them?


I am trying to create a query where data from table analytics value per column will be grouped and counted, I have zero knowledge in grouping a value of columns and counting them and doing this on my end is really hard.

So I have an example query here: So I have an example query here:

query getAnalytics($from: timestamptz, $to: timestamptz, $user_id: String) {
  data:analytics(where: { created_at: {_gte: $from, _lte: $to}, user_id: {_eq: $user_id}}) { id user_id visitor_id type os device country browser created_at }
}

it get a sample data:

{
  "data": {
    "data": [
      {
        "id": 9,
        "user_id": "user_2Jy9jmL8OIpyb1DpTGYrrtjS77P",
        "visitor_id": "null",
        "type": "visit",
        "os": "Windows 10",
        "device": "Desktop",
        "country": "Japan",
        "browser": "Chrome",
        "created_at": "2023-01-07T04:36:38.086468+00:00"
      },
      {
        "id": 10,
        "user_id": "user_2Jy9jmL8OIpyb1DpTGYrrtjS77P",
        "visitor_id": "null",
        "type": "visit",
        "os": "Windows 10",
        "device": "Desktop",
        "country": "Japan",
        "browser": "Chrome",
        "created_at": "2023-01-07T04:36:38.489139+00:00"
      },
      {
        "id": 11,
        "user_id": "user_2Jy9jmL8OIpyb1DpTGYrrtjS77P",
        "visitor_id": "null",
        "type": "visit",
        "os": "Windows 10",
        "device": "Desktop",
        "country": "Japan",
        "browser": "Chrome",
        "created_at": "2023-01-07T04:43:53.903306+00:00"
      },
      {
        "id": 12,
        "user_id": "user_2Jy9jmL8OIpyb1DpTGYrrtjS77P",
        "visitor_id": "null",
        "type": "visit",
        "os": "Windows 10",
        "device": "Desktop",
        "country": "Japan",
        "browser": "Chrome",
        "created_at": "2023-01-07T04:43:54.325736+00:00"
      },
      {
        "id": 13,
        "user_id": "user_2Jy9jmL8OIpyb1DpTGYrrtjS77P",
        "visitor_id": "null",
        "type": "visit",
        "os": "Windows 10",
        "device": "Desktop",
        "country": "Japan",
        "browser": "Chrome",
        "created_at": "2023-01-07T04:45:17.899966+00:00"
      },
    ]
  }
}

Sorry, I wasn't able to provide a tested code because most of my structure didn't work and the only thing that I know in counting things up is aggregate with condition and count or join, But things grouping dynamically with columns value and counting them up, I have zero idea.

Is it possible to group the value of columns and count them, you know for analytics purposes. sample output structure that I want to happen:

{
    "user_id": "user_2Jy9jmL8OIpyb1DpTGYrrtjS77P",
    "visitor_id": {
        "null": 41,
        "user_2JzAMaOW8j01rpsl98McXwBlCkv": 30
    }
    "type": {
        "visit": 71,
        "shares": 0,
        "clicks": 0,
    },
    "os": {
        "Windows 10": 28,
        "iOS 13.2.3": 36,
        "null": 7,
    },
    "device": { 
        "Desktop": 60,
        "Mobile": 10,
        "null": 7,
    },
    "country": { 
        "Japan": 60,
        "null": 7,
    },
    "browser": {
        "Chrome": 28,
        "Mobile Safari": 36,
        "null": 7,
    }
}

I am clueless and cant find in docs or other article the thing/related to things I want to happen


Solution

  • GraphQL is a query language for APIs (https://graphql.org/).

    GraphQL it is not SQL and have not a group method. For each query we have output object format. In your case it is (approximately)

    type Analytic {
      id: ID!
      user_id: String
      visitor_id: String
      type: String
      os: String
      device: String
      country: String
      browser: String
      created_at: DateTime
    }
    

    Result of your query it is List of Analytic. Combining schema-defined types must already be done on the client. Or ask to add an object with analytics to the scheme in the format you need.