Coder Social home page Coder Social logo

Comments (17)

danielrearden avatar danielrearden commented on May 18, 2024 1

I actually started working on a graphql-codegen plugin already. My intent is to replace the CLI entirely with it. The plugin could optionally generate the resolvers using the existing typescript-resolvers plugin as well to make the whole DX better.

from sqlmancer.

lishine avatar lishine commented on May 18, 2024

I adapted my project to the 0.2.1 release
And it works great, aside from the count.
I use @many, @paginate, typescript generation

It could me mentioned in the docs, how to integrate types:

client.ts:
import Knex from 'knex'
import { createSqlmancerClient } from 'sqlmancer'
import { SqlmancerClient } from './generated'

const knex = Knex({
    client: 'pg',
    connection: process.env.PG_CONNECTION_STRING,
})
export const client: SqlmancerClient = createSqlmancerClient('./src/sqlmancer/schema.ts', knex)
npx sqlmancer generate ./src/sqlmancer/schema.ts ./src/sqlmancer/generated.ts
context.d.ts:
import { Request, Response } from 'express'
import { SqlmancerClient } from '../sqlmancer/generated'

export type Context = { sql: any; req: Request; res: Response; client: SqlmancerClient }
server.ts:
import { makeSqlmancerSchema } from 'sqlmancer'

import { resolvers } from './resolvers'
import { schema } from '~/sqlmancer/schema'
import { client } from '~/sqlmancer/client'

    const server = new ApolloServer({
        schema: makeSqlmancerSchema({
            typeDefs: schema,
            resolvers: resolvers,
        }),
        context: ({ req, res }) => {
            return {
                sql,
                client,
                req,
                res,
            }
        },
        debug: false,
    })

I still use one schema file because of code-gen.
I can try to write code-gen plugin ?

from sqlmancer.

danielrearden avatar danielrearden commented on May 18, 2024

The original intent behind the count aggregate field was to answer questions like "how many orders have a status of DELIVERED?"

query {
  orders(where: { status: { equal: "DELIVERED" } }) {
    aggregate {
      count
    }
  }
}

It sounds like we need to add a separate totalCount field, probably next to hasMore instead of inside the aggregate object, that would return the count without the limit applied.

from sqlmancer.

lishine avatar lishine commented on May 18, 2024

So I understand the intend that functions that are inside aggregate to operate with all the arguments applied including limit and offset. And it is useful in case that results are not requested. Because if results are requested, it could be just calculated by the requester.
You intend to put totalCount outside aggregate. And it would not be affected by offset and limit. BTW hasMore is affected by offset. And these specs are implicit, is it good enough?
Furthermore, would there be need to calculate totalAvg/totalMin/totalMax ?
Considering the above, hasura's API seems reasonable. All can be done in one go. Just the where: $where need to be specified in each.

query {
   orders(limit, where)
   aggregate_orders(limit, where)
   aggregate_orders(nolimit, where)
}

from sqlmancer.

lishine avatar lishine commented on May 18, 2024

Maybe like this

query {
  orders(where: { status: { equal: "DELIVERED" } }) {
    aggregate {
      count
    }
    aggregateTotal {
      count
    }
  }
}

from sqlmancer.

lishine avatar lishine commented on May 18, 2024

Or to be explicit:

query {
  orders {
    results(where: $where, offset: $offset, limit: $limit)
    hasMore(where: $where, offset: $offset)
    aggregate(where: $where, offset: $offset, limit: $limit ) {
      count
    }
    aggregateTotal(where: $where) {
      count
    }
  }
}

from sqlmancer.

danielrearden avatar danielrearden commented on May 18, 2024

totalCount would really be more a convenience. It's already possible to do something like

query($where: OrderWhere!, $limit: Int!) {
  orders(where: $where, limit: $limit) {
    results {
      id
    }
  }
  allOrders: orders(where: $where) {
    aggregate {
      count
    }
  }
}

Definitely need to at least document the usage better.

By the way, I forgot to mention, you can simplify this:

client.models.Order.paginate().count().selectAll().resolveInfo(info).execute()

to just

client.models.Order.paginate().resolveInfo(info).execute()

resolveInfo will append count and select and whatever other methods to the builder as needed depending on what fields are actually requested

from sqlmancer.

lishine avatar lishine commented on May 18, 2024

Thanks.

The above

query($where: OrderWhere!, $limit: Int!) {
  orders(where: $where, limit: $limit) {
    results {
      id
    }
  }
  allOrders: orders(where: $where) {
    aggregate {
      count
    }
  }
}

Indeed I do it now - as ordersCount. But it has to be added to the scheme.

from sqlmancer.

lishine avatar lishine commented on May 18, 2024

And btw date now returns a string - cool!

from sqlmancer.

lishine avatar lishine commented on May 18, 2024

Indeed I do it now - as ordersCount. But it has to be added to the scheme.

My mistake, you mean allOrders is an alias. Cool idea, then all good, no need for totalCount and such

from sqlmancer.

lishine avatar lishine commented on May 18, 2024

So essentially this issue can be closed

from sqlmancer.

danielrearden avatar danielrearden commented on May 18, 2024

I'm still wondering if adding totalCount might make sense. It's not uncommon to see that field exposed in, for example, Relay connections.

from sqlmancer.

lishine avatar lishine commented on May 18, 2024

Each table will use this. At least, totalCount will simplify a bit the query.
But if it is related to Relay, it can be done together with #46

from sqlmancer.

lishine avatar lishine commented on May 18, 2024

It can be right now partly compatible with relay :))
hasMore -> pageInfo { hasNextPage }
results -> edges

from sqlmancer.

danielrearden avatar danielrearden commented on May 18, 2024

Implemented with 32fe4be

from sqlmancer.

danielrearden avatar danielrearden commented on May 18, 2024

@lishine a graphql-codegen plugin is now available.

from sqlmancer.

lishine avatar lishine commented on May 18, 2024

Fantastic! I will test it

from sqlmancer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.