Amplify-cli: The @connection directive doesn't link data

Created on 18 Apr 2019  路  5Comments  路  Source: aws-amplify/amplify-cli

Note: If your question is regarding the AWS Amplify Console service, please log it in the
official AWS Amplify Console forum

* Which Category is your question related to? *
Amplify schema.graphql types and directives

* What AWS Services are you utilizing? *
Amplify

* Provide additional details e.g. code snippets *

type Author @model {
    id: ID!
    name: String
    book: Book @connection(name: "AuthorBook")
}

type Book @model {
    id: ID!
    title: String
    author: Author @connection(name: "AuthorBook")
}

I'm trying to get clarification on the @connection directive, because it is not working as expected and the documentation doesn't detail this exact use case.

With the basic schema above, I'm hoping to link the book and author together. My end goal is that I can query a book, and get the author data, and vice versa.

Following these steps:
1) Mutation to create a new Author (only filling in the name field)
2) Mutation to create a new Book (filling in the title, and passing in the Author ID)

Desired behavior: when I query Author, it shows me the associated book. Currently is shows null.
(When I query the book, that of course shows me the Author since I manually linked it together)

So to solve this problem, I can manually do an updateAuthor mutation, and pass it in the book ID.
However, I was under the impression that using a named connection like this would automatically link them together, without requiring an additional update mutation.

Am I misunderstanding, or have I structured my schema incorrectly?

graphql-transformer pending-response question

Most helpful comment

@ElliottBabb The @connection directive does not as of writing support bi-directional 1-1 connections so you must update the reference id in both objects. It is possible to use BatchPutItem or Pipeline resolvers to have a single mutation that does both but this is not implemented. In the case of one-to-many @connections you only need to update the objects on the many side.

The same is true for:

mutation createAuthorAndBook{
    createAuthor(input:{
        name: "Dave"
        book: {
            title: "How To Code 101"
        })
    }
}

You could use a pipeline resolver to implement a mutation that creates the author, creates and book, and associates them. You can use a similar approach as is outlined here for writing custom resolvers that target ElasticSearch. https://aws-amplify.github.io/docs/cli/graphql#add-a-custom-resolver-that-targets-an-aws-lambda-function

, as of writing, creates a DynamoDB GSI, adds resolvers to the connected fields (i.e. Book.author and Author.book

All 5 comments

Also, is it possible to have a mutation that creates both a book and an author at the same time?

mutation createAuthorAndBook{
    createAuthor(input:{
        name: "Dave"
        book: {
            title: "How To Code 101"
        })
    }
}

Which would result in them being linked/connected, so querying for "How To Code 101" would also have the author info

@jordanranz Any advice on this issue?

@ElliottBabb The @connection directive does not as of writing support bi-directional 1-1 connections so you must update the reference id in both objects. It is possible to use BatchPutItem or Pipeline resolvers to have a single mutation that does both but this is not implemented. In the case of one-to-many @connections you only need to update the objects on the many side.

The same is true for:

mutation createAuthorAndBook{
    createAuthor(input:{
        name: "Dave"
        book: {
            title: "How To Code 101"
        })
    }
}

You could use a pipeline resolver to implement a mutation that creates the author, creates and book, and associates them. You can use a similar approach as is outlined here for writing custom resolvers that target ElasticSearch. https://aws-amplify.github.io/docs/cli/graphql#add-a-custom-resolver-that-targets-an-aws-lambda-function

, as of writing, creates a DynamoDB GSI, adds resolvers to the connected fields (i.e. Book.author and Author.book

@mikeparisstuff
Affirmative - thank you for the response.
Would love to see the 1-1 bi-directional connection feature eventually. Our project has some intertwined data and that would drastically reduce the number of mutations we have to call just to initiate it. I'm currently at seven mutations within a lambda to create and update/connect everything.

Cheers

@mikeparisstuff Thank you this information was super helpful.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mwarger picture mwarger  路  3Comments

onlybakam picture onlybakam  路  3Comments

jeanpaulcozzatti picture jeanpaulcozzatti  路  3Comments

ffxsam picture ffxsam  路  3Comments

ReidWeb picture ReidWeb  路  3Comments