aws-amplify / amplify-js

A declarative JavaScript library for application development using cloud services.
https://docs.amplify.aws/lib/q/platform/js
Apache License 2.0
9.44k stars 2.13k forks source link

AWS Appsync Optimistic Response and Models that have relationships using @connection #4090

Open amirmishani opened 5 years ago

amirmishani commented 5 years ago

Which Category is your question related to? API

What AWS Services are you utilizing? AWS Appsync SDK

Provide additional details e.g. code snippets How do you get AWS Appsync Optimistic Response to work when your models have relationships using @connection?

The generated mutation input fields don't take "jobs" as input but the optimistic response requires it to work. I get this warning that says: Missing field jobs in { "__typename": "User"...}

My User model uses @connection to connect users with jobs.

type User
  @model
  @key(fields: ["tenantId", "userId"])
{
  tenantId: String!
  userId: ID!
  ...
  jobs: [JobAssignee] @connection(name: "UserJobs", keyField: "userId")
}

My react component setup:

export default compose(
  graphql(ListUsers, {
    options: {
      fetchPolicy: 'cache-and-network',
      variables: {
        limit: 100
      }
    },
    props: (props: any) => ({
      users: props.data.listUsers ? props.data.listUsers.items : [],
      data: props.data
    })
  }),
  graphqlMutation(NewUser, ListUsers, 'User', 'userId'),
  graphqlMutation(EditUser, ListUsers, 'User', 'userId')
)(Users);
manueliglesias commented 5 years ago

Hi @amirmishani

The graphqlMutation helper doesn't work on types with connections at the moment. For now you'll have to write your own apollo update functions, I am sorry I don't have a better suggestion at the moment.

I am labeling this as a feature request.

amirmishani commented 5 years ago

@manueliglesias Thanks for the info. After I opened the issue I did end up writing my own apollo update functions and it's working perfectly. Obviously the boilerplate is a bit much but it's worth getting the offline capabilities.