Amplify-cli: union and connection types

Created on 5 Oct 2018  路  8Comments  路  Source: aws-amplify/amplify-cli

* Which Category is your question related to? *
GraphQL Transformer

* What AWS Services are you utilizing? *
AWS Appsynce GraphQL Transformer

* Provide additional details e.g. code snippets *
Are union connection types covered and how to properly annotate them?

union MediaMetaData = AudioMetaMedia

# Audio File type meta data
type AudioMetaMedia @model @searchable{
    id: ID!
    # Duration of audio in seconds
    duration: Int!
    # associated media
    media: Media! @connection(name: "MediaAudioMetaMedia")
}

# Content Media type holder
type Media @model {
    id: ID
    # Display name of media
    name: String
    # Display description of media
    description: String
    # Media file stored in S3
    data: S3Object @connection
    # Substitle file associated with media file stored in S3
    subtitle: S3Object @connection
    # Type of assets (e.g. Movie, Audio, ect)
    type: MediaType
    # Meta data associated with media file (e.g. duration for audio)
    mediaMetaData: MediaMetaData @connection(name: "MediaAudioMetaMedia")
}

Is this correct also what happens when you add another type to the union do you keep the same connection name?

feature-request graphql-transformer

All 8 comments

Currently @connection only supports @model object types. I can see why supporting unions would be useful but this will have to prioritized. It would be helpful if you could give more details on your use case and we can start a discussion on how to best generalize this feature request.

We were thinking of having a generic media object which would point to s3object but then having a meta data union that could point to all kinds of different media types meta data. As the meta data for audio is different than video. Honestly there is a way to just change the data structure and walk away from the need for this altogether.

@austinamorusocfc can you elaborate a bit more on your point below:

Honestly there is a way to just change the data structure and walk away from the need for this altogether

We have a Complex Object type for S3 which you'll see some documentation on this soon. Also there is documentation for writing our own GraphQL transformer. Is that what you meant from the above and you'd like to see documentation for S3Object and/or writing your own transformer? Or are you looking for something else as @mikeparisstuff stated to have this as a feature?

@undefobj
I was saying I could remove the union altogether, if it is a requirement to use the amplify template.

However I think the interesting use case here is searching for audio with a certain duration.

And then having the ability to eventually add video type to union and future types of media.

My question was not around the S3Object in this case it was more about handling connections with union types.

Is there any updates on this? I would like the content of a Post to be generic(a union I believe) for example (think Reddit)

union Media =  Video | Image | Link

type Post @model {
   id: ID!
   content: Media! @connection
}

Is this possible currently in any other way if this isn't possible @mikeparisstuff ? I looked at writing a transformer but seems like quite a bit of work and really need to focus on other priorities.

One way to achieve this is adding multiple fields to a Post even though only 1 of them will be used

type Post @model {
    video: Video @connection 
    image: Image @connection
    link: Link @connection 
}

these other fields (image and link) are empty and redundant in a video Post (again think of how Reddit works - you only post one of these things, not a mixture)

The thing I don't quite understand is that in my API.service.ts the mutations look good

mutation CreatePost($input: CreatePostInput!) {
        createPost(input: $input) {
          __typename
          id
          createdAt
          content {
            __typename
            ... on Video {
              id
              file {
                __typename
                bucket
                region
                key
              }
              createdAt
            }
            ... on Image {
              id
              file {
                __typename
                bucket
                region
                key
              }
              createdAt
            }

but the inputs don't...

type CreatePostInput = {
  id?: string | null;
  createdAt?: string | null;
  postUserId: string;
};

why is this? is it a half done feature or something?

what is the point of transforming unions if you can't use them?

lib has been bloody amazing so far - it's been hard work don't get me wrong but this is the first proper road block I've had

help?

@j-r-t Did you ever get an answer to your question above?

Running into a similar use case/scenario myself on wanting to use a Union.

Anyone else have a recommendation?

Same problem here. It will be nice to have union support, especially in a single-table model

Same issue here. @j-r-t @minhazfm @gabrieleravanelli did you find a solution?

Was this page helpful?
0 / 5 - 0 ratings