Giter Club home page Giter Club logo

Comments (4)

hlship avatar hlship commented on May 28, 2024 1

Thanks for the update; been busy here. I'll try to digest what you have posted above.

from lacinia.

hlship avatar hlship commented on May 28, 2024

Could you elaborate on what you are using the variables for? Yes, we could expose how Lacinia internally tracks variables and values, but I prefer to do so carefully, in a way that allows us to change things in the future without breaking backwards compatibility. So if I had some insight into what questions you are trying to answer, that might yield a better, more stable solution.

from lacinia.

dat7e avatar dat7e commented on May 28, 2024

I'm working on a relational database to GraphQL API generator. For poll-based subscription optimization, the compiler has to group similar GraphQL queries into a single big SQL query to execute against the database. This requires knowing which variables are used (for the grouping), and which types they are (for appropriate SQL casts). Below is a sample:

subscription Test($name: String!) {
  Customers(
    where: {
      fullName: { _in: [$name, "Test2"] }
      id: { _neq: "47ae0cc8-9fd6-46b3-bdb8-e3bbc7c5adca" }
    }
  ) {
    id
    fullName
    createdAt
    comm
    updatedAt
    metadata
    addresses(
      where: {
        city: { _in: ["Test3", "Test4"] }
        id: { _neq: "47ae0cc8-9fd6-46b3-bdb8-e3bbc7c5adca" }
      }
    ) {
      street1
      city
      countryCode
    }
  }
}

is currently grouped into this Postgres query:

WITH __subs AS (
  SELECT 
    __subs_arr ->> 0 AS __sub_id, 
    __subs_arr ->> 1 AS name 
  FROM 
    JSON_ARRAY_ELEMENTS(
      CAST('[["43f0282c-f9e9-4644-ac31-9dcdf5095530","jj"],["another sub id","$name value for this sub"]]' AS json)
    ) AS __subs_arr
) 
SELECT 
  __sub_id AS sub_id, 
  __sub_res.result 
FROM 
  __subs 
  LEFT JOIN LATERAL (
    SELECT 
      COALESCE(
        JSONB_AGG(__root_0.*), 
        '[]'
      ) AS result 
    FROM 
      (
        SELECT 
          customer.id, 
          customer.full_name, 
          customer.created_at, 
          customer.comm, 
          customer.updated_at, 
          customer.metadata, 
          (
            SELECT 
              COALESCE(
                JSONB_AGG(__nest_1__0.*), 
                '[]'
              ) 
            FROM 
              (
                SELECT 
                  address.street_1, 
                  address.city, 
                  address.country_code 
                FROM 
                  address 
                WHERE 
                  (
                    city IN ('Test3', 'Test4')
                  ) 
                  AND (id <> '47ae0cc8-9fd6-46b3-bdb8-e3bbc7c5adca') 
                  AND (
                    address.customer_id = customer.id
                  )
              ) AS __nest_1__0
          ) AS addresses 
        FROM 
          customer 
        WHERE 
          (
            full_name IN (__subs.name, 'Test2')
          ) 
          AND (id <> '47ae0cc8-9fd6-46b3-bdb8-e3bbc7c5adca')
      ) AS __root_0
  ) AS __sub_res ON TRUE

I got to this with some hacks (i.e: using variables data from request context, which is without types and might be inaccurate). And this query works but as soon as we introduce some variables of non-primitive types, for example an "id" of UUID type:
Exception in thread "async-dispatch-8" org.postgresql.util.PSQLException: ERROR: operator does not exist: uuid <> text

For the full solution, in the WITH clause, we would need to cast values like so:

WITH __subs AS (
  SELECT 
    __subs_arr ->> 0 AS __sub_id,
    CAST(__subs_arr ->> 1 as uuid) AS id,
    CAST(__subs_arr ->> 2 as text) AS name,
    ...
  FROM 
    ...

from lacinia.

dat7e avatar dat7e commented on May 28, 2024

This seems to be related to #430 in that both try to enrich parsed query context.
I'd like to have your inputs on how to best move forward @hlship

from lacinia.

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.