hasura / hasura/graphql-engine

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

Open
#10,016 2 comments 2 reactions 0 assignees View on GitHub
k/bug t/native-dbs
Dominant language
TypeScript
Stars
32.1k
Forks
3k
PR merge metrics
PR metrics pending

Description

### 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
```yaml
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:
```postgesql
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.

```json
{
"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

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the nested Logical Model case through the Native Query metadata request at /v1/metadata, using the supplied PostgreSQL setup and Many to Many template. Trace the validation that reports “Unexpected logical model array field 'children'.” Done means the behavior is made consistent: nested Logical Models work as Native Query return fields, or their unsupported definition is rejected earlier.

Written by the indexing model from the issue text.

Assessment

Tech stack
graphql, postgresql, typescript
Domain
backend-api-design, databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.