How to properly append to an array using Dgraph GraphQL API?

256 views Asked by At

I'm trying to spin up Dgraph, but appears as though to add a node to an array of nodes using the GraphQL api requires an unnecessary amount of work if I understand it correctly:

I have the following simplified schema:

type User @secret(field: "password") {
    account: String! @id
    email: String! @search(by: [hash])
    extension: String! @search(by: [hash])
    phone: String! @search(by: [hash])
    hasCreated: [Transaction]! @hasInverse(field: from)
    hasReceived: [Transaction]! @hasInverse(field: to)
}

type Transaction {
    id: ID!
    type: TransactionType! @search(by:[terms])
    amount: Float! 
    assetCode: String! @search(by:[terms])
    to: User!
    from: User!
    initiatedAt: DateTime! @search(by:[hash])
    completedAt: DateTime @search(by:[hash])
    status: Status!
}

To me, it appears as though to add a node to the User's hasCreated or hasReceived fields would require me to pull the entire array, append a new Transaction to the array and then use an updateUser mutation to complete the update. But the updateUser mutation would require me to get all the Transactions and all Users attached to these transactions and so on.

Example trying to retrieve an entire user object:

query {
                getUser(id:"%s"){
                    account
                    email
                    extension
                    phone
                    hasCreated {
                        id
                        type
                        amount
                        assetCode
                        to {
                            ...
                        }

                    }
                    hasReceived {
                        id
                        type
                        amount
                        assetCode
                        to {
                            ...
                        }

                    }
                    }
                }
            }

Is there another way to append to arrays or update objects using the GraphQL api without having to retrieve whole objects?

0

There are 0 answers