Code Monkey home page Code Monkey logo

Comments (3)

benjie avatar benjie commented on July 18, 2024 1

We need to add it to https://postgraphile.org/postgraphile/next/node-id/
and cross-link it to https://postgraphile.org/postgraphile/next/smart-tags/#arg0variant-arg1variant-
and I could have sworn that I'd written it in the docs somewhere but I can't find it (please PR!) but essentially you need to:

  1. create the function so that it accepts a table record type rather than uuid (this is a temporary requirement we hope to lift at some point, but we'll need another way of indicating the expected type)
  2. override autodetection if it's a stable function (as we might detect it as a computed column function) to turn it back into what it should be: @behavior -queryField -typeField -mutationField +queryField. No action necessary for yours since it's volatile
  3. change the "variant" of the relevant 0-indexed argument to nodeId: @arg0variant nodeId\n@arg1variant nodeId

So for you:

CREATE
OR REPLACE function dansdata.add_organization_member (
  organization dansdata.organizations,
  individual dansdata.individuals,
  title TEXT
) returns dansdata.organization_members AS $$
  INSERT INTO dansdata.organization_members("organization_id", "individual_id", "title") VALUES (organization.id, individual.id, title) RETURNING *;
$$ language sql volatile strict security invoker;
comment on function dansdata.add_organization_member is
  E'@arg0variant nodeId\n@arg1variant nodeId\n@behavior -typeField +mutationField';

from crystal.

benjie avatar benjie commented on July 18, 2024 1

I'm well aware of the limitations, and this is a planned feature. The reason we don't support it on UUID currently is because we have no way of indicating what type that UUID is meant to be, other than to use a record type. Likely a better solution is to use something like @arg0variant nodeId:Organization but implementing that is not something that has been visited yet. But since it is solvable in the current system (albeit slightly awkward) it's on the backburner to address once V5.0.0 is released.

from crystal.

FelixZY avatar FelixZY commented on July 18, 2024

@benjie, thanks to your description, I was able to implement the desired behavior. However, given the current requirement of using a table record type, I found myself having to deal with two instances of "ugly" DX. For me, these are further motivating the need to support @arg0variant nodeId on non-table record types, such as uuid, in the future as well.

1. id duplication

CREATE OR REPLACE function dansdata.add_organization_member (
  organization_id dansdata.organizations,
  member_id dansdata.profiles,
  title TEXT = NULL
) returns dansdata.organization_members AS $$
  SELECT dansdata.add_organization_member (organization_id.id, member_id.id, title);
$$ language sql volatile called ON NULL input security invoker;

As you can see, I now have to use <type>_id.id syntax to access my id if I want to expose organizationId: <id> in the GraphQL api.

Of course, I could just

CREATE OR REPLACE function dansdata.add_organization_member (
  organization dansdata.organizations,
  member dansdata.profiles,
  title TEXT = NULL
)

but then the GraphQL member will be named organization instead of organizationId (I'm guessing I could work around this with inflection but that's still a bit too much work IMO).

2. It's harder to use the function internally

I can no longer simply do this in raw SQL:

SELECT dansdata.add_organization_member ('68d3ca81-52ba-484d-80b6-ca0af6213c1d', 'f0f98228-e7fe-418c-b71e-995638862ee7');

instead, I've been forced to add a wrapper specifically for postgraphile which I would preferably avoid:

CREATE OR REPLACE function dansdata.add_organization_member (
  organization_id UUID,
  member_id UUID,
  title TEXT = NULL
) returns dansdata.organization_members AS $$
  INSERT INTO
    dansdata.organization_members ("organization_id", "member_id", "title")
  VALUES
    (organization_id, member_id, title)
  RETURNING
    *;
$$ language sql volatile called ON NULL input security invoker;

comment ON function dansdata.add_organization_member IS $$
  @behavior -*
$$;

CREATE OR REPLACE function dansdata.postgraphile_add_organization_member (
  organization_id dansdata.organizations,
  member_id dansdata.profiles,
  title TEXT = NULL
) returns dansdata.organization_members AS $$
  SELECT dansdata.add_organization_member (organization_id.id, member_id.id, title);
$$ language sql volatile called ON NULL input security invoker;

comment ON function dansdata.postgraphile_add_organization_member IS $$
  @name addOrganizationMember
  @arg0variant nodeId
  @arg1variant nodeId
$$;

from crystal.

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.