Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Using JSONB Fields Directly in Functions #521

Open
awgneo opened this issue Nov 20, 2024 · 0 comments
Open

Support Using JSONB Fields Directly in Functions #521

awgneo opened this issue Nov 20, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@awgneo
Copy link

awgneo commented Nov 20, 2024

What version of GraphJin are you using? graphjin version

v3.0.38

Have you tried reproducing the issue with the latest release?

Yes

What is the hardware spec (RAM, OS)?

32GB M1 Max, MacOS Sequoia 15.0

Steps to reproduce the issue (config used to run GraphJin).

I think this might be a simple fix :)

I haven't tried this with tables, yet; however, with the Postgres function:

CREATE FUNCTION agreego.alexfunc (IN obj jsonb)
RETURNS TABLE (output_var text, existed boolean)
LANGUAGE plpgsql
AS $$
BEGIN
    -- Extract the field 'field_name' from the input jsonb and return it
    RETURN QUERY
    SELECT obj->>'field' AS output_var, true AS existed;
END;
$$;

I am unable to run this GraphQL query:

query Alexfunc {
    alexfunc(args: { obj: { field: "Alex" } }) {
        output_var
    }
}

The SQL statement generated is:

"/* action='Alexfunc',controller='graphql',framework='graphjin' */ SELECT jsonb_build_object('alexfunc', __sj_0.json) AS __root FROM ((SELECT true)) AS __root_x LEFT OUTER JOIN LATERAL (SELECT COALESCE(jsonb_agg(__sj_0.json), '[]') AS json FROM (SELECT to_jsonb(__sr_0.*) AS json FROM (SELECT \"alexfunc_0\".\"output_var\" AS \"output_var\" FROM (SELECT \"alexfunc\".\"output_var\" FROM alexfunc(obj => '') AS \"alexfunc\" LIMIT 20) AS \"alexfunc_0\") AS \"__sr_0\") AS \"__sj_0\") AS \"__sj_0\" ON true"

The error I receive is:

{
    "errors": [
        {
            "message": "ERROR: invalid input syntax for type json (SQLSTATE 22P02)"
        }
    ]
}

And you can see alexfunc(obj => '') with nothing being passed in. When attempting with the following:

query Alexfunc {
    alexfunc(args: { obj: "{ \"field\": \"Alex\" }" }) {
        output_var
    }
}

Basically, trying as a string JSON object, the SQL statement generated is:

"/* action='Alexfunc',controller='graphql',framework='graphjin' */ SELECT jsonb_build_object('alexfunc', __sj_0.json) AS __root FROM ((SELECT true)) AS __root_x LEFT OUTER JOIN LATERAL (SELECT COALESCE(jsonb_agg(__sj_0.json), '[]') AS json FROM (SELECT to_jsonb(__sr_0.*) AS json FROM (SELECT \"alexfunc_0\".\"output_var\" AS \"output_var\" FROM (SELECT \"alexfunc\".\"output_var\" FROM alexfunc(obj => '{ \\\"field\\\": \\\"Alex\\\" }') AS \"alexfunc\" LIMIT 20) AS \"alexfunc_0\") AS \"__sr_0\") AS \"__sj_0\") AS \"__sj_0\" ON true"

Where now everything is present, but being double escaped alexfunc(obj => '{ \\\"field\\\": \\\"Alex\\\" }'), I believe, by:

c.squoted(a.Val)

But I think at this point a.Val is already over-escaped and the error is the same.

Expected behaviour and actual result.

I would love it if I could use args: { obj: { field: "Alex" } } and not have to pass in a JSON string to set a JSONB field, but passing a JSON string would be an acceptable backup. I don't need anything as heavy handed as JSONB virtual tables, but the ability to serialize JSONB in and out of GraphQL, and specifically postgres functions, would be awesome.

@awgneo awgneo added the bug Something isn't working label Nov 20, 2024
@awgneo awgneo changed the title Support Using JSONB Directly in Functions Support Using JSONB Fields Directly in Functions Nov 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant