GraphQL: Querying for a value from another table from a mutation - graphql

Let's say I have a table t1 that is foreign key'd to another table t2. For simplicity, t2 only has columns 'name' and 'id' which are both unique. I store the id in t1.
My problem is I want to write a mutation where I know the name but I don't know the id when I go to store something in t1. Is there a way to query inside of my mutation so that it converts the value in my statement?
Perhaps a plugin I can add to my project?
I end up with something like this where I pass in a known name but I want to store the id
mutation addT1(
$knownT2Name: String!,
) {
createT1 (
input: {
t1: {
id: $component
# Is there a way to convert this to the id inside the query
# Or do I need to query for the id with the name first then pass that in?
t2_id: $knownT2Name
}
}
) {
t1 {
id
t2_id
}
}
}
This is a simple example. The reason I don't want to query for the id with the name is in actuality t1 is foreign key'd to a myriad of other tables with the same situation and I don't want to do 9+ queries just to convert each string to an integer id.
I would much rather be able to do something like this:
mutation addT1(
$knownT2Name: String!,
) {
createT1 (
input: {
t1: {
id: $component
t2_id: t2Byt2(name: $knownT2Name) { id }
}
}
) {
t1 {
id
t2_id
}
}
}
Where t2Byt2(name: $knownT2Name) { id } would be a sub-query that passes the name and gets the id, then stores the id in 't2_id'
I'm looking at a nested mutations plugin for postgraphile (Here's the GitHub)but I haven't had any traction. It's not quite what I'm looking for.

For the simple relationship I believe you want something like: (using the nested mutations plugin). This only works on CREATE. No luck with an UPSERT.
mutation addT1($t1Name: String!, $t2Name: String!) {
createT1(
input: {
T1: {
name: $t1Name,
t2ToT2: { connectByT2: { name: $t2Name } }
}
}
) {
t1 {
t1Id
name
t2ByT2 {
name
}
}
}
}

Related

graphql - Refer to other fields in mutation

I want to create 2 related objects, e.g. 1 Location and 1 Place where Place has a reference to Location like so:
type Location {
id: String
name: String
}
type Place {
id: String
locationId: String
}
Is it possible to do this with 1 mutation request? Currently I'm doing this with 2 separate mutation requests like below:
mutation ($locationName: String!) {
insert_Location(objects: {name: $locationName}) {
returning {
id
}
}
}
//in another request, use the id returned from the request above
mutation ($locationId: String!) {
insert_Place(objects: {locationId: $locationId}) {
returning {
id
}
}
}
I'm aware it's possible to have multiple fields in a mutation so I could create 2 Locations in 1 mutation request like below.
mutation ($locationName: String!) {
location1: insert_Location(objects: {name: $locationName}) {
returning {
id
}
}
location2: insert_Location(objects: {name: $locationName}) {
returning {
id
}
}
}
However if I wanted to do this to create 1 Location and 1 Place, is there a way to retrieve the created Location Id and pass it to the 2nd field to create the Place?
For future reference:
As #Xetera pointed out, because the 2 types have a foreign key relationship you can do a nested insert mutation where hasura would handle setting the foreign key value. In my case it would look something like:
mutation ($locationName: String!) {
insert_Place(
objects: {
Location: {data: {name: $locationName}}, //hasura will create Location and assign the id to Place.locationId
}
) {
returning {
id
}
}
}
Docs here for further reading: https://hasura.io/docs/1.0/graphql/manual/mutations/insert.html#insert-an-object-along-with-its-related-objects-through-relationships

Graphql syntax for single query for same field with different parameters from an array

I am looking for something like the below pseudo query:
query users($ids: [String!]) {
"id from $ids": getUser(id){
id
name
dob
}
}
To get a response like:
data: {
'039ccf5c-3070-4368-b790-0884669e759d': {id: '039ccf5c-3070-4368-b790-0884669e759d', name: 'u1', 'dob': 12-12-12'},
'139ccf5c-3070-4368-b790-0884669e759d': {id: '139ccf5c-3070-4368-b790-0884669e759d', name: 'u1', 'dob': 12-12-12'},
}
Is this possible?
You can use aliases to query to the same field multiple times:
query users($id1: String!, $id2: String!) {
user1: getUser(id: $id1) {
...UserFragment
}
user2: getUser(id: $id1) {
...UserFragment
}
}
fragment UserFragment on User {
id
name
dob
}
There are no control structures in GraphQL, so there's no way to loop through a value that is a list. If your list of IDs is of variable length, then you'll have to construct a query similar to the above programmatically, dynamically generating both the variable definitions and the aliased fields.

How to reshape a GraphQL (via Hasura) query response?

I have a CHAT_MESSAGE_FRAGMENT that returns all the message data from my Hasura graphql api.
However, the Gifted Chat react-native component requires the data in a specific structure so I'm attempting to convert it with the query below.
I'm able to alias all the top level data but can't figure out how to add a nested level of data.
I'm guessing it isn't possible but I thought I'd ask in case I'm missing something.
const GIFTED_CHAT_GROUP_MESSAGES_QUERY = gql`
query chatGroupMessages($chatGroupId: Int!) {
chat_message(
where: { to: { id: { _eq: $chatGroupId } } }
) {
_id: id,
# user: {
# _id: from.id, <== How do I add
# name: from.name, <== this secondary level?
# },
text: message,
image: image_url,
createdAt: created_at,
system: message_type,
}
}
${CHAT_MESSAGE_FRAGMENT}
`;
Assuming you already have chat_message.user_id -> users.id foreign key constraint set up, you'll also need to alias the from object in addition aliasing any of its nested fields:
const GIFTED_CHAT_GROUP_MESSAGES_QUERY = gql`
query chatGroupMessages($chatGroupId: Int!) {
chat_message(
where: { to: { id: { _eq: $chatGroupId } } }
) {
_id: id,
from: user: {
_id: id,
name
},
text: message,
image: image_url,
createdAt: created_at,
system: message_type,
}
}
${CHAT_MESSAGE_FRAGMENT}
`;
The secondary level of data is basically nested object queries in Hasura. You can nest any number of queries as long as a relationship has been created.
In this case, assuming the chat_message table has a user_id field, you can establish a foreign key constraint for chat_message.user_id -> users.id, where users is a table with id as primary key.
Once the foreign key constraint is created, Hasura Console automatically suggests relationships. Here user would be an object relationship in chat_message table.
Here's the official docs link for Creating a relationship

How can I insert records in AwsAppSync mutation with proper #connection values?

I have added a resources table to my schema, connecting to a Plants table:
type Resource #model
{
id: ID!
name: String!
Plants: [Plant] #connection(name: "ResourcePlant")
}
Ran amplify push, and all resources were created properly.
Now I wanted to add a Resource, and link it to all Plants properly.
Do you know how is the sintaxe I should use to run the recently created mutation createResource in order to add the items on Plant I want to include to that resource?
I tried to run like this:
mutation CreateResource {
createResource (input: {
name: "Plant",
Plants : {
items :
{ id: "f9a0468e-da74-41d5-8287-1cb6a76b25a5" }
}
}
) {
name,
Plants {
items {
id
}
nextToken
}
}
}
This was the error message:
Validation error of type WrongType: argument 'input' with value
'ObjectValue{objectFields=[ObjectField{name='name',
value=StringValue{value='Plant'}}, ObjectField{name='Plants',
value=ObjectValue{objectFields=[ObjectField{name='items', value=ObjectValue{objectFields=[ObjectField{name='id',
value=StringValue{value='f9a0468e-da74-41d5-8287-1cb6a76b25a5'}}]}}]}}]}'
contains a field not in 'CreateResourceInput': 'Plants' # 'createResource'
How did you define Plant?
And have you checked this example? https://aws-amplify.github.io/docs/cli-toolchain/graphql#connection
Ok, after some headache, I found what was missing in my model. For me so far it has proved to be the best way of doing this relationship...
I have added on my Plant type, on schema definition, a field named plantResourceId (other than the one used for the #connection directive). What I found out was that, by convention, when inserting/updating a record on "Plant" and adding the resource "id" field content of the resource I want to "connect" to that plant, it will automatically be retrieved when "Resources" is queried, for each item - what is better: Out-of-the-box from codegen.
Insert example
mutation CreatePlant {
createPlant(input:{
name: "MyPlant",
plantResourceId: "id-for-connected-resource"
}) {
name,
plantResourceId
}
}
Query example to retrieve items:
query listPlantsOnResource {
listResources(filter: {
name: {
contains: "myfilter"
}
}) {
items {
id
name
Plants
{
items {
id
name
description
}
}
}
}
}
It worked very well!
Thanks all who contributed!

How to run GraphQL filter query on an ARRAY field type in Hasura?

I'm trying to run GraphQL filter query on array field types, for example on a text ARRAY field type.
In the following example scenario:
CREATE TABLE
CREATE TABLE Employee (
firstName text,
lastName text,
tags text[]
);
And we can filter on text array field in one of the following ways:
SELECT STATEMENT with CONDITION on an ARRAY type
SELECT * FROM Employee WHERE tags #> ARRAY['teamplayer']::varchar[]
This works in PostGres and also in Postgraphile implicitly.
On Postgraphile GraphQL, we can query the above table as follows:
Query
{
allEmployees(filter: {tags: {contains: "teamplayer"}}) {
nodes {
firstName
lastName
tags
}
}
}
and the result will be:
Response
{
"data": {
"allEmployees": {
"nodes": [
{
firstName: 'Russell'
lastName: 'Dodds'
tags: ['teamplayer', 'punctual']
},
{
firstName: 'Emma'
lastName: 'Samsin'
tags: ['teamplayer']
}
]
}
}
}
Can someone please give me some references or suggestions on how to achieve similar results on ARRAY field types in Hasura?
I think you couldn't use array directly in the Hasura console. You should use jsonb instead. It's more appropriate as you can use _append, _prepend, _delete_key...
But it's seems you can use Array with hasura. If your schema come from external service, the input for an array should be a literal. For a column of type tags[] the input value has to be a string like: "{teamplayer,punctual}". It's how Array works in postgres.
So your mutation will be:
mutation {
insert_table(objects:{
tags: "{teamplayer,punctual}"
}) {
returning {
tags
}
}
}

Resources