graphql-engine icon indicating copy to clipboard operation
graphql-engine copied to clipboard

Single object lookup by unique key

Open karibertils opened this issue 8 years ago • 19 comments

It would be useful if we could lookup single objects by their unique fields other than the primary key.

user_by_email(email: "[email protected]") { id email }

For composed unique fields it could be something along the lines of

order_by_user_id_and_invoice_id(user_id: 1, invoice_id: 1) { id email }

I presume we could automatically get this functionality based on the unique indexes in postgresql. It is a bit nicer than with the where clause when we know there will only be a single item.

karibertils avatar Sep 23 '18 01:09 karibertils

We should also generate the corresponding mutations.

0x777 avatar Sep 25 '18 06:09 0x777

Is this Rails's ActiveRecord pattern ?

revskill10 avatar Sep 27 '18 16:09 revskill10

This would make things cleaner. Whatever design decision should take into consideration views/functions/etc

andrewalex avatar Nov 16 '19 20:11 andrewalex

what about allowing returning single items for custom functions

if I want to create a function that returns the current logged user, right now it will return an array instead of a single item

tafelito avatar Mar 10 '20 20:03 tafelito

Want to echo a desire for this, as it's a bit awkward to be returning arrays that are known to only return 0-1 records. I think just allowing custom functions to be manually flagged as 'singular' in the metadata would be ideal. For a concrete use case, this is the kind of function I'm using currently:

CREATE OR REPLACE FUNCTION "accountByUsername"(search text)
RETURNS SETOF accounts AS $$
    SELECT * FROM accounts WHERE lower(username) = lower(search)
$$ LANGUAGE sql STABLE;

Where there's a unique index on lower(username).

c-spencer avatar May 27 '20 18:05 c-spencer

@tirumaraiselvan @0x777 any updates on this? This isn't a major blocker to anything since the workaround is straightforward but it would make a lot of my code far cleaner and more concise.

maxcan avatar Jun 30 '20 18:06 maxcan

Would be nice if we had a query_by_uk exactly like query_by_pk but base on the unique keys, this way we don't need to use the search queries that return an array.

gsnunes avatar Oct 22 '20 01:10 gsnunes

@tirumaraiselvan @0x777 Do you any updates on this? Any chance we can see this in the 2.1 Release.

amnaveenriaz avatar Mar 02 '21 05:03 amnaveenriaz

This would greatly help

joanrodriguez avatar Mar 09 '21 18:03 joanrodriguez

any updates on this?

alherd-by avatar Feb 02 '22 11:02 alherd-by

Is there any update?

furkan-vibely avatar Mar 09 '22 13:03 furkan-vibely

I would also like to have a cleaner solution

SimEli avatar Apr 04 '22 08:04 SimEli

 👀

smeevil avatar Apr 20 '22 13:04 smeevil

Want to echo a desire for this, as it's a bit awkward to be returning arrays that are known to only return 0-1 records. I think just allowing custom functions to be manually flagged as 'singular' in the metadata would be ideal. For a concrete use case, this is the kind of function I'm using currently:

CREATE OR REPLACE FUNCTION "accountByUsername"(search text)
RETURNS SETOF accounts AS $$
    SELECT * FROM accounts WHERE lower(username) = lower(search)
$$ LANGUAGE sql STABLE;

Where there's a unique index on lower(username).

Is there a custom way to autogenerate these for multiple tables from a user-side in hasura console?

BrianHung avatar Jun 11 '22 21:06 BrianHung

👀

mbrimmer83 avatar Dec 24 '22 23:12 mbrimmer83

This issue is nearly blocking me from selling Hasura to my organization. We have synthetic UUIDs or integers as PK for almost all of our tables, and unique key constraints which are really used to lookup and refer to entities. A get_by_uk is a no-brainer necessity for our GraphQL apis, as using the where syntax is terrible for this use-case. Our hand-made GraphQL resolvers can't be replaced by Hasura without this feature.

dwoske avatar Mar 09 '23 16:03 dwoske

Hi @dwoske I've reached to you via email regarding this.

a-chocolate-bear avatar Mar 15 '23 04:03 a-chocolate-bear

Thanks everyone for your comments and patience on this issue. We have been closely listening into all the feedback and requests from the community, and have been working on a re-imagined, re-architectured Hasura, that tackles many of these from ground up. We are pleased to announce that we are launching V3-Alpha of Hasura (Data Delivery Network) on our next Community call on Nov 30. The new metadata structure in V3 is designed to be highly flexible and declarative, to support the perfect GraphQL schema you would like to achieve, while leveraging the database specific features as much as possible. In V3, you can customise and decide what key you would like to use for lookups (and not fixed to the primary key). We would request to join this community call to learn more about Hasura V3. Post launch, we will update this issue with relevant details.

manasag avatar Nov 23 '23 16:11 manasag

This is possible in Hasura V3 by defining the appropriate selectUniques configuration in your metadata. Using the example from the original post.

GraphQL order_by_user_id_and_invoice_id(user_id: 1, invoice_id: 1) { id email }

The model would look something like this...

kind: Model
version: v1
definition:
  ... # Other model info
  graphql:
    selectUniques:
      - queryRootField: user_by_email
        uniqueIdentifier:
          - email
      - queryRootField: order_by_user_id_and_invoice_id
        uniqueIdentifier:
          - user_id
          - invoice_id

Look forward to hearing your feedback. More information can be found in the V3 documentation here. V3 data domain modeling docs

dameleney avatar Dec 06 '23 00:12 dameleney