hasura / graphql-engine

Blazing fast, instant realtime GraphQL APIs on your DB with fine grained access control, also trigger webhooks on database events.
https://hasura.io
Apache License 2.0
31.16k stars 2.76k forks source link

Logical Models containing field of a Logical Model type cannot be used as return type of Native Query #10016

Open b-dreissigacker opened 11 months ago

b-dreissigacker commented 11 months ago

Version Information

Server Version: v2.35.1

Environment

OSS

What is the current behaviour?

When I create a Logical Model, I can create a field and type it with another Logical Model. If I then try to use my Logical Model as return type of a Native Query, I get an Unexpected Logical Model error.

What is the expected behaviour?

Either (prefered): I should be able to use a Logical Model as described above as return type of a Native Query, without errors. Or: It should not be able to create a Logical Model and type a field with another Logical Model.

How to reproduce the issue?

  1. Start with an new Hasura instance v2.35.1 connected to a Postgres DB
    version: '3.6'
    services:
    postgres:
    image: postgres:15
    restart: always
    volumes:
    - db_data:/var/lib/postgresql/data
    environment:
      POSTGRES_PASSWORD: postgrespassword
    graphql-engine:
    image: hasura/graphql-engine:v2.35.1
    ports:
    - "7777:8080"
    depends_on:
    - "postgres"
    restart: always
    environment:
      HASURA_GRAPHQL_METADATA_DATABASE_URL: postgres://postgres:postgrespassword@postgres:5432/postgres
      PG_DATABASE_URL: postgres://postgres:postgrespassword@postgres:5432/postgres
      HASURA_GRAPHQL_ENABLE_CONSOLE: "true" # set to "false" to disable console
      HASURA_GRAPHQL_DEV_MODE: "true"
      HASURA_GRAPHQL_ENABLED_LOG_TYPES: startup, http-log, webhook-log, websocket-log, query-log
    volumes:
    db_data:
  2. Connect to the DB with the PG_DATABASE_URL Environment variable
  3. Apply the Many to Many Template from the Template Gallery
  4. Create a Logical Model named child with the fields:
    • aid: integer
    • tag_id: integer
    • title: text
    • author_name: text
  5. Create a Logical Model named parent with the fields:
    • id: integer
    • name: text
    • children: child (the Logical Model from Step 4)
  6. Create a Native Query with the Native Query Statement:
    WITH article_cte AS (
    SELECT a.id AS aid, at.tag_id, a.title, a.author_name FROM _manytomany.article AS a
        JOIN _manytomany.article_tag AS at ON at.article_id = a.id
    )
    SELECT
    id,
    name,
    COALESCE(JSON_AGG(DISTINCT article_cte.*) FILTER ( WHERE _manytomany.tag.id IS NOT NULL ), '[]') as children
    FROM _manytomany.tag, article_cte
    WHERE _manytomany.tag.id = article_cte.tag_id
    GROUP BY
    _manytomany.tag.id,
    _manytomany.tag.name;

Please provide any traces or logs that could help here.

{
  "detail": {
    "http_info": {
      "content_encoding": null,
      "http_version": "HTTP/1.1",
      "ip": "xxx.xxx.xxx.xxx",
      "method": "POST",
      "status": 500,
      "url": "/v1/metadata"
    },
    "operation": {
      "error": {
        "code": "unexpected",
        "error": "Unexpected logical model array field 'children'.",
        "path": "$.args"
      },
      "query": {
        "type": "bulk_atomic"
      },
      "request_id": "486cff3b-48ad-4270-92ae-d5734fbed5c6",
      "request_mode": "error",
      "response_size": 96,
      "uncompressed_response_size": 96,
      "user_vars": {
        "x-hasura-role": "admin"
      }
    },
    "request_id": "486cff3b-48ad-4270-92ae-d5734fbed5c6"
  },
  "level": "error",
  "span_id": "b2ddc0ae2686c4f2",
  "timestamp": "2023-11-24T09:47:36.691+0000",
  "trace_id": "cbd806ba2cc2efbdc46248c8e52f6d80",
  "type": "http-log"
}

Keywords

Hasura, Native Query, Logical Model, Nested, json_agg

FW22 commented 11 months ago

same problem here - would be really cool feature, if this bug would be fixed.

manasag commented 11 months ago

We support LMs inside LMs but not intended for Native Query return types (built for complex data modelling required for MongoDB support). We have plans to support this soon in Hasura V3.