aws-amplify / amplify-cli

The AWS Amplify CLI is a toolchain for simplifying serverless web and mobile development.
Apache License 2.0
2.81k stars 819 forks source link

Mutations not triggering subscriptions #4462

Closed CodySwannGT closed 4 years ago

CodySwannGT commented 4 years ago

Describe the bug Using the Amplify GraphQL Client to subscribe to a subscription and call a mutations does not result in a socket notification via the subscription

Amplify CLI Version 4.21.1

To Reproduce

const createTest = /* GraphQL */ '
    mutation CreateTest($input: CreateTestInput!) {
      createTest(input: $input) {
        __typename
        id
      }
    }
'

useEffecct(() => {
API.graphql(graphqlOperation(createTest, { input: { id: (new Date()).toISOString(), name: (new Date()).toISOString() }}))
        .then(console.log) <==== successfully called
        .catch(console.log)
}, [])

useEffect(() => {
    API.graphql(graphqlOperation(`subscription OnCreateTest {
        onCreateTest {
        __typename
        id
      }
    }`))
    .subscribe({
      next: (todoData) => { /// <==== this is NEVER called
        console.log("todoData", todoData);
      },
      error: console.log, /// <==== this is NEVER called
    })
}, [])

Expected behavior Calling the mutation should result in the subscripting firing

Desktop (please complete the following information):

Additional context

type Test 
  @model(subscriptions: { level: public })  
{
  id: ID!
  name: String!
}

const awsmobile = {
    "aws_project_region": "us-east-1",
    "aws_cognito_identity_pool_id": "xxxxxxx",
    "aws_cognito_region": "us-east-1",
    "aws_user_pools_id": "xxxxxxx",
    "aws_user_pools_web_client_id": "xxxxxxx",
    "oauth": {},
    "aws_appsync_graphqlEndpoint": "https://xxxxxxx.appsync-api.us-east-1.amazonaws.com/graphql",
    "aws_appsync_region": "us-east-1",
    "aws_appsync_authenticationType": "AMAZON_COGNITO_USER_POOLS",
    "aws_cloud_logic_custom": [
        {
            "name": "AdminQueries",
            "endpoint": "https://xxxxxxx.execute-api.us-east-1.amazonaws.com/staging",
            "region": "us-east-1"
        }
    ],
    "aws_mobile_analytics_app_id": "xxxxxxx",
    "aws_mobile_analytics_app_region": "us-east-1",
    "aws_user_files_s3_bucket": "xxxxxxx",
    "aws_user_files_s3_bucket_region": "us-east-1"
};

export default awsmobile;

import awsmobile from './aws-exports';

Amplify.configure(awsmobile);

Amplify folder

SwaySway commented 4 years ago

Hello @CodySwannGT does the subscription trigger from the AppSync Console?

CodySwannGT commented 4 years ago

@SwaySway I think you're asking, if I run the mutation in the AppSync console, does the subscription trigger from the app?

If so, no, it does not.

If you're asking if I can create a subscription from the AppSync query console, I got a message that subscriptions weren't allowed when I tried that.

UnleashedMind commented 4 years ago

@CodySwannGT From my experience using the AppSync service, after call .subscribe(..) on an API, it take a couple of seconds for the subscription to fully setup, in my tests, I usually use setTimeout to wait for 2 to 5 seconds before I send the mutations, and the subscription works as expected. In your project the mutation and subscription are both sent by the useEffecct(.) method, it's very likely that the AppSync service is not given enough time to fully setup the subscription for your project. Try to setup the subscription first and then wait for a few seconds before you start to send the mutations (consider also use other react life cycle events to achieve this?)

BTW, you can test subscriptions on AppSync query console.

CodySwannGT commented 4 years ago

@UnleashedMind What am I doing wrong?

UnleashedMind commented 4 years ago

As I pointed out above, you need to first setup the subscription, then wait for a couple of seconds, then send the mutations. Could you verify that in your code the mutation is sent AFTER the subscibe(.) method is called?

And when you use the AppSync Console, you need to open two consoles to test subscriptions. On one console, run the subscription. Then open another console, go to the Queries Console of the same API, and run mutation. After the mutation is successfully executed, you will see the subscription data in the first console.

CodySwannGT commented 4 years ago

@UnleashedMind sorry for the miscommunication. I saw your first response and my reply was simply related to the AppSync Console.

That said, I tried (and recorded) doing as you said and I still got nice dice

I also can confirm the mutation is sent AFTER the subscribe(.) method is called, but that seems like a moot point if the subscription is not even working from console

CodySwannGT commented 4 years ago

@UnleashedMind Anything else I can provide? Pretty desperate for a solution on this one.

renebrandel commented 4 years ago

@CodySwannGT - can we get on a call together? We're trying to get to the bottom of this but just require more hands-on time. Can you DM me on Twitter?

UnleashedMind commented 4 years ago

@CodySwannGT I used the exact auth setup (email or phone) and I still cannot reproduce your issue.

Could you try to create another project from ground up with the schema and auth type? See if subscription works. Could you send us the AppSync API region, name, ID (you can find it in the Settings) in email? so we can ask the AppSync team to help to troubleshoot.

CodySwannGT commented 4 years ago

Yep. As discussed, we're successfully using subscriptions on other projects.

I'm not sure when I'll have time to setup a brand new project, but I can certainly send you the AppSync info.

UnleashedMind commented 4 years ago

@CodySwannGT The AppSync service had an issue, subscription does not work if the schema contains union or interface, that issue has just been fixed yesterday. Is there interface or union in the schema of this project? Could you try again and see if the issue is still there.

CodySwannGT commented 4 years ago

Sorry for the delay. Literally just tried it from console and got nothing.

I also spun up a brand new app in a different account and it worked fine.

Unfortunately, this is a pretty robust stack, so tearing it down isn't it an option

UnleashedMind commented 4 years ago

The question still stand: Is there interface or union in the schema of this project? Could you send me the exact time (including the time zone) when you sent the subscription and the mutations? (You can test it again if you don't remember the exact time you tested last time). So I can contact the AppSync team with the information and they can track the history to dig to the root of the issue.

CodySwannGT commented 4 years ago

No. I posted by entire amplify folder, including the graphql schema. No interfaces or unions.

I"m afk but I will grab the time next time I test it.

renebrandel commented 4 years ago

Hi @CodySwannGT - based on the investigation, the issue seems to be in AppSync. The best way to get engagement on this issue is to create a customer support ticket through the AWS Management Console. Closing this as the issue is not in the Amplify CLI repo.

CodySwannGT commented 4 years ago

Uh. Sure. Done: https://www.dropbox.com/s/io7e91f9fwofgiz/Screenshot%202020-07-07%2008.45.36.png?dl=0

What's your advice for anyone who isn't pay for support?

github-actions[bot] commented 3 years ago

This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels for those types of questions.