question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

feat: Ability to use "where" clause inside `insert` mutation to link existing data

See original GitHub issue

_Note from Hasura team: issue closed in favour of https://github.com/hasura/graphql-engine/issues/1573_

I think this is a feature request.

Given the following schema:

CREATE TABLE "Person" (
  "id" uuid PRIMARY KEY DEFAULT gen_random_uuid()
);

CREATE TABLE "EmailAddress" (
  "personId" uuid NOT NULL REFERENCES "Person" ("id") ON DELETE CASCADE,
  "emailAddress" citext PRIMARY KEY
);

CREATE TABLE "Agreement" (
  "id" uuid PRIMARY KEY DEFAULT gen_random_uuid(),
  "text" citext NOT NULL
);

CREATE TABLE "PersonAgreement" (
  "personId" uuid NOT NULL REFERENCES "Person" ("id") ON DELETE CASCADE,
  "agreementId" uuid NOT NULL REFERENCES "Agreement" ("id") ON DELETE CASCADE,
  "value" boolean NOT NULL
);

A person can be uniquely found using an email address. I’d like to insert a new PersonAgreement into the database given an agreementId, emailAddress, and PersonAgreement value.

Using sql, the email address could be used to get the related personId which could be combined with the agreementId and value to insert a new PersonAgreement.

At the moment, it does not appear to be possible to execute this query within a transaction using Hasura. The problem is that the only way to associate the Person with the new PersonAgreement is to insert a new Person along with the PersonAgreement. Except in this case I don’t wish to create a new Person, I wish to find an existing person or have the mutation fail.

Example of the query I’d like to perform:

mutation AddPersonAgreement(
  $emailAddress: String!,
  $agreementId: uuid!,
  $value: Boolean!
) {
  insert_PersonAgreement(
    objects: {
      value: $value,
      agreementId: $agreementId,
      person: {
        where: { # <-- new API here
          emailAddresses: { emailAddress: { _eq: $emailAddress } }
        }
      }
    }
  ) {
    affected_rows
  }
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:6
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

11reactions
rikinskcommented, Nov 20, 2019

Related to #3366

This could be resolved if it were possible to add nested objects via the update mutation.

Something roughly along the lines of:

mutation insertUserEvent($email: String, $eventId: Int) {
  update_users (
    where: { email: {_eq: $email}}
    _add: {
      event: {
        eventId: $eventId
      }
    }
  ) {
    affected_rows
  }
}

This is a feature being worked on and should be released soon.

1reaction
jorrollcommented, Sep 28, 2019

Yes, this query might translate to:

INSERT INTO "PersonAgreement" ("value", "agreementId", "personId")
VALUES (
  $1, 
  $2, 
  SELECT "id" FROM "Person"
    JOIN "EmailAddress" ON ("EmailAddress"."personId" = "Person"."id")
    WHERE "EmailAddress"."emailAddress" = $3
);

I’m not sure if this SQL is correct, but hopefully the idea is clear.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Postgres: Insert mutation | Hasura GraphQL Docs
Insert an object into Postgres using a mutation.
Read more >
INSERT INTO SELECT statement overview and examples
This query performs the following tasks: It first Selects records from a table ( Select statement)
Read more >
Four Success Stories in Gene Therapy - Nature
Researchers have linked the disease to mutations or deletions in any one of 27 genes associated with retinal development and function.
Read more >
cobas EGFR Mutation Test v2 - Accessdata.fda.gov
Activating mutations in the gene encoding EGFR occur primarily in NSCLC, ... Safety Data Sheets (SDS) are available upon request from your local...
Read more >
INSERT (Transact-SQL) - SQL Server - Microsoft Learn
Is the name of the linked server on which the table or view is ... The OUTPUT INTO clause is not supported in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found