graphile/pg-aggregates

Not able to use pg-aggregates

Murthy10 opened this issue · 19 comments

I'm trying to use pg-aggregates but unfortunately I was not able to make it work.

First problems occurred during the installation I'm not sure if I do it right.
My approach, installation:

git clone https://github.com/graphile/pg-aggregates.git \
    && cd pg-aggregates \
    && yarn install \
    && yarn build

Execution:

postgraphile --connection postgres://<user>:<password>@<host>:<port>/<db> --schema public --watch --enhance-graphiql --append-plugins <path>/pg-aggregates/dist/index.js

postgrahile runs but I'm not able to execute any aggregation query.

What I tried:

{
  allPayments {
    nodes {
      aggregates{
        sum {
      		amount          
        }
      }
    }
  }
}

Maybe @benjie you can provide an example how to setup and use pg-aggregates.

We've not released this to npm yet (it's still at the proof-of-concept stage); if you want to try it out copy these files into your project and require index.js directly:

https://github.com/graphile/pg-aggregates/tree/master/src

What aggregates are you looking for? You may also wish to track graphile/graphile-engine#465

Thank you for the tip, I will have a look at graphile-engine and follow the updates of this repo.

@Murthy10 The aggregates are added directly to the connection, your query tries to query it on the nodes. If you use a tool like GraphiQL it should help you to construct your query with typeahead and linting.

Try:

{
  allPayments {
      aggregates{
        sum {
      		amount          
        }
      }
  }
}

Is there any way to get this to work with the CLI in the docker image?

There's no instructions on how to do that as it's not officially released yet. You'd do it the same way - add the files to the docker image and then tell PostGraphile where to find them.

I'm having some issue when added files from https://github.com/graphile/pg-aggregates/tree/master/src and required index.td directly.
How I`m doing:

import  AggregatePlugin from "./_common/graphql/plugins/pg-aggregates"
...

const buildServer = async () => {
  const connection = await connectionPromise
  const pgPool = (connection?.driver as any).master
  const { schema: graphileSchema, plugin } = await makeSchemaAndPlugin(
    pgPool,
    schemas, // PostgreSQL schema to use
    {
      dynamicJson: true,
      appendPlugins: [ConnectionFilterPlugin],
      graphiql: true,
      enhanceGraphiql: true,
      watchPg: true,
      jwtSecret: getConfig().PasswordHash,
      // jwtPgTypeIdentifier: 'public.jwt_token',
      pgDefaultRole: 'postgres',
      allowExplain: true
    }
  )
  return new ApolloServer({
    schema: mergeSchemas({
      schemas: [graphileSchema, schema],
    }),
    plugins: [queryComplexityPlugin, AggregatePlugin, plugin],
    context: async ({ event, context: lambdaContext }) => {
      const requestId = lambdaContext.awsRequestId
      const container = Container.of(requestId)

      await connectionPromise

      const authorizerContent = event.requestContext?.authorizer
      const graphQLContext: Partial<OSContext> = {
        uid: authorizerContent?.uid,
        userRole: authorizerContent?.userRole,
        requestId,
        container,
      }
      return graphQLContext
    },
...

I'm sewing two schemas and giving priority to the one generated by the postgraphile. However, when trying to generate the schema I'm taking:

TypeError: Cannot read property 'hook' of undefined

That 'hook' should be a property of 'build' on AddAggregatesPlugin. @benjie would you help me? Am I forgetting to set up something?

Try updating all your graphile-related software, it's possible your dependencies are out of date and don't support this yet.

This is now an official released plugin; usage instructions are in the README 👍

@benjie I am getting error in graphiql -

query{
  allBrands(orderBy: PRICE_DESC){
    aggregates{
      sum{
        price
      }
    }
  }
  allSales(orderBy: QUANTITY_DESC, first: 2){
    totalCount
    nodes{
      quantity
      brandByBrandId{
        brandname
        price
        productByProductId{
          productname
        }
      }
      storeByStoreId{
        storename
      }
    }
    aggregates{
      sum{
        quantity
      }
    }
  }
}

Output

{
  "errors": [
    {
      "message": "Cannot read property '@aggregates' of undefined",
      "locations": [
        {
          "line": 3,
          "column": 5
        }
      ],
      "path": [
        "allBrands",
        "aggregates"
      ]
    }
  ],
  "data": {
    "allBrands": {
      "aggregates": null
    },
    "allSales": {
      "totalCount": 11,
      "nodes": [
        {
          "quantity": 12,
          "brandByBrandId": {
            "brandname": "Brand E",
            "price": "500.50",
            "productByProductId": {
              "productname": "Product C"
            }
          },
          "storeByStoreId": {
            "storename": "Store C"
          }
        },
        {
          "quantity": 9,
          "brandByBrandId": {
            "brandname": "Brand C",
            "price": "300.50",
            "productByProductId": {
              "productname": "Product A"
            }
          },
          "storeByStoreId": {
            "storename": "Store B"
          }
        }
      ],
      "aggregates": {
        "sum": null
      }
    }
  }
}

Requires PostGraphile v4.12.0-alpha.0 or higher.

Yes, I am using -

"dependencies": {
"@graphile-contrib/pg-simplify-inflector": "^6.1.0",
"db-migrate": "^0.11.12",
"db-migrate-pg": "^1.2.2",
"postgraphile": "^4.12.0-alpha.0",
"@graphile/pg-aggregates": "^0.1.0"
}

Interesting; try npx yarn-deduplicate && yarn and then try again. If you're using npm try switching to yarn 😉

@benjie FYI, I dockerized a PostGraphile app by following steps in https://www.graphile.org/postgraphile/running-postgraphile-in-docker/

schema

package

graphql-Dockerfile

docker-compose-yaml

Output in graphiql -

Screen Shot 2021-04-23 at 8 45 04 AM

Please file an issue about these unexpected nulls, sending through the details (e.g. DB schema/data) as text that can be copied/pasted.

Hi @benjie ,
I face this issue "Cannot read property '@Aggregates' of undefined"
My dependencies are:
"@graphile/pg-aggregates": "^0.1.0",
"cluster": "^0.7.7",
"compression": "^1.7.4",
"consul": "^0.40.0",
"cors": "^2.8.5",
"express": "^4.17.1",
"nodemon": "^2.0.4",
"pg": "^8.6.0",
"pm2": "^4.5.4",
"postgraphile": "^4.11.0",
"postgraphile-plugin-connection-filter": "^2.2.1",
"sequelize": "^6.6.2"

I changed the postgraphile version to "4.12.0". Also comes the same error.
Screenshot

Requires PostGraphile v4.12.0-alpha.0 or higher.
-- https://github.com/graphile/pg-aggregates#usage

I don't think you have a new enough version installed. Try yarn add postgraphile@next.

Thanks @benjie for the response.
I have solved the above problem.
I want to grouping "data_configuration" which has the same group a or b . I am attached db details for your reference
oie_HvklHT4PDz1x

And in my graphiql interface how i will group object. My graphiql interface is looks like
Screenshot (1)

You need to request subfields via a selection set on the grouped aggregates field.