title: aws appsync subscriptions won't fire
body: <p>I've been struggling with this for some time now, so I could use some help.</p>
  <p>I can't get onCreate subscription for type <code>HealthPlan</code> working in my react-native app.</p>
  <p>Based on <a href="https://github.com/dabit3/conference-app-in-a-box/blob/master/src/Discussion.js" rel="nofollow noreferrer">Nader's example</a>:</p>
  <p><strong>schema.graphql</strong></p>
  <pre><code>type HealthPlan {
      id: ID!
      name: String!
      type: String!
      owner: String
  }
  
  input CreateHealthPlanInput {
      name: String!
      type: String!
  }
  
  type Mutation {
      ....
      createHealthPlan(input: CreateHealthPlanInput!): HealthPlan
      ...
  }
  
  type Subscription {
      ...
      onCreateHealthPlan(owner: String!): HealthPlan
          @aws_subscribe(mutations: [&quot;createHealthPlan&quot;])
      ...
  }
  </code></pre>
  <p><strong>mutations.js</strong></p>
  <pre><code>export const createHealthPlan = /* GraphQL */ `
    mutation CreateHealthPlan($input: CreateHealthPlanInput!) {
      createHealthPlan(input: $input) {
        id
        name
        type
        owner
      }
    }
  `;
  </code></pre>
  <p><strong>subscriptions.js</strong></p>
  <pre><code>export const onCreateHealthPlan = /* GraphQL */ `
    subscription OnCreateHealthPlan($owner: String!) {
      onCreateHealthPlan(owner: $owner) {
        id
        name
        type
        owner
      }
    }
  `;
  </code></pre>
  <p><strong>Main.js</strong></p>
  <pre><code>import { API, graphqlOperation } from 'aws-amplify';
  import { onCreateHealthPlan } from './subscriptions.js';
  
  const sub = &quot;cognitoSubField&quot;;   // AMAZON_COGNITO_USER_POOLS auth
  
  API.graphql(
            graphqlOperation(onCreateHealthPlan, {
              owner: sub,
            }),
          ).subscribe({
            next: response =&gt; {
              console.log('next data received', response);
            },
            error: err =&gt; {
              console.log('subscription error', err.message);
            },
            complete: () =&gt; {
              console.log('finished');
            },
  })
  </code></pre>
  <p><strong>Mutation.createHealthPlan.req.vtl</strong></p>
  <pre><code>## [Start] Determine request authentication mode **
  #if( $util.isNullOrEmpty($authMode) &amp;&amp; !$util.isNull($ctx.identity) &amp;&amp; !$util.isNull($ctx.identity.sub) &amp;&amp; !$util.isNull($ctx.identity.issuer) &amp;&amp; !$util.isNull($ctx.identity.username) &amp;&amp; !$util.isNull($ctx.identity.claims) &amp;&amp; !$util.isNull($ctx.identity.sourceIp) &amp;&amp; !$util.isNull($ctx.identity.defaultAuthStrategy) )
    #set( $authMode = &quot;userPools&quot; )
  #end
  ## [End] Determine request authentication mode **
  ## [Start] Check authMode and execute owner/group checks **
  #if( $authMode == &quot;userPools&quot; )
    ## No Static Group Authorization Rules **
  
  
    ## No Dynamic Group Authorization Rules **
  
  
    ## [Start] Owner Authorization Checks **
    #set( $isOwnerAuthorized = false )
    ## Authorization rule: { allow: owner, ownerField: &quot;owner&quot;, identityClaim: &quot;cognito:username&quot; } **
    #set( $allowedOwners0 = $util.defaultIfNull($ctx.args.input.owner, null) )
    #set( $identityValue = $util.defaultIfNull($ctx.identity.claims.get(&quot;username&quot;), $util.defaultIfNull($ctx.identity.claims.get(&quot;cognito:username&quot;), &quot;___xamznone____&quot;)) )
    #if( $util.isList($allowedOwners0) )
      #foreach( $allowedOwner in $allowedOwners0 )
        #if( $allowedOwner == $identityValue )
          #set( $isOwnerAuthorized = true )
        #end
      #end
    #end
    #if( $util.isString($allowedOwners0) )
      #if( $allowedOwners0 == $identityValue )
        #set( $isOwnerAuthorized = true )
      #end
    #end
    #if( $util.isNull($allowedOwners0) &amp;&amp; (! $ctx.args.input.containsKey(&quot;owner&quot;)) )
      $util.qr($ctx.args.input.put(&quot;owner&quot;, $identityValue))
      #set( $isOwnerAuthorized = true )
    #end
    ## [End] Owner Authorization Checks **
  
  
    ## [Start] Throw if unauthorized **
    #if( !($isStaticGroupAuthorized == true || $isDynamicGroupAuthorized == true || $isOwnerAuthorized == true) )
      $util.unauthorized()
    #end
    ## [End] Throw if unauthorized **
  #end
  ## [End] Check authMode and execute owner/group checks **
  
  
  ## [Start] Prepare DynamoDB PutItem Request. **
  $util.qr($context.args.input.put(&quot;createdAt&quot;, $util.defaultIfNull($ctx.args.input.createdAt, $util.time.nowISO8601())))
  $util.qr($context.args.input.put(&quot;updatedAt&quot;, $util.defaultIfNull($ctx.args.input.updatedAt, $util.time.nowISO8601())))
  $util.qr($context.args.input.put(&quot;__typename&quot;, &quot;HealthPlan&quot;))
  #set( $condition = {
    &quot;expression&quot;: &quot;attribute_not_exists(#id)&quot;,
    &quot;expressionNames&quot;: {
        &quot;#id&quot;: &quot;id&quot;
    }
  } )
  #if( $context.args.condition )
    #set( $condition.expressionValues = {} )
    #set( $conditionFilterExpressions = $util.parseJson($util.transform.toDynamoDBConditionExpression($context.args.condition)) )
    $util.qr($condition.put(&quot;expression&quot;, &quot;($condition.expression) AND $conditionFilterExpressions.expression&quot;))
    $util.qr($condition.expressionNames.putAll($conditionFilterExpressions.expressionNames))
    $util.qr($condition.expressionValues.putAll($conditionFilterExpressions.expressionValues))
  #end
  #if( $condition.expressionValues &amp;&amp; $condition.expressionValues.size() == 0 )
    #set( $condition = {
    &quot;expression&quot;: $condition.expression,
    &quot;expressionNames&quot;: $condition.expressionNames
  } )
  #end
  {
    &quot;version&quot;: &quot;2017-02-28&quot;,
    &quot;operation&quot;: &quot;PutItem&quot;,
    &quot;key&quot;: #if( $modelObjectKey ) $util.toJson($modelObjectKey) #else {
    &quot;id&quot;:   $util.dynamodb.toDynamoDBJson($util.defaultIfNullOrBlank($ctx.args.input.id, $util.autoId()))
  } #end,
    &quot;attributeValues&quot;: $util.dynamodb.toMapValuesJson($context.args.input),
    &quot;condition&quot;: $util.toJson($condition)
  }
  ## [End] Prepare DynamoDB PutItem Request. **
  </code></pre>
  <p><strong>Mutation.createHealthPlan.res.vtl</strong></p>
  <pre><code>$util.toJson($ctx.result)
  </code></pre>
  <p><strong>Subscription.onCreateHealthPlan.req.vtl</strong></p>
  <pre><code>{
      &quot;version&quot;: &quot;2018-05-29&quot;,
      &quot;payload&quot;: {}
  }
  </code></pre>
  <p><strong>Subscription.onCreateHealthPlan.res.vtl</strong></p>
  <pre><code>## [Start] Determine request authentication mode **
  #if( $util.isNullOrEmpty($authMode) &amp;&amp; !$util.isNull($ctx.identity) &amp;&amp; !$util.isNull($ctx.identity.sub) &amp;&amp; !$util.isNull($ctx.identity.issuer) &amp;&amp; !$util.isNull($ctx.identity.username) &amp;&amp; !$util.isNull($ctx.identity.claims) &amp;&amp; !$util.isNull($ctx.identity.sourceIp) &amp;&amp; !$util.isNull($ctx.identity.defaultAuthStrategy) )
    #set( $authMode = &quot;userPools&quot; )
  #end
  ## [End] Determine request authentication mode **
  ## [Start] Check authMode and execute owner/group checks **
  #if( $authMode == &quot;userPools&quot; )
    ## No Static Group Authorization Rules **
  
  
    ## [Start] Owner Authorization Checks **
    #set( $isOwnerAuthorized = false )
    ## Authorization rule: { allow: owner, ownerField: &quot;owner&quot;, identityClaim: &quot;cognito:username&quot; } **
    #set( $allowedOwners0 = $util.defaultIfNull($ctx.args.owner, null) )
    #set( $identityValue = $util.defaultIfNull($ctx.identity.claims.get(&quot;username&quot;),
                          $util.defaultIfNull($ctx.identity.claims.get(&quot;cognito:username&quot;), &quot;___xamznone____&quot;)) )
    #if( $util.isList($allowedOwners0) )
      #foreach( $allowedOwner in $allowedOwners0 )
        #if( $allowedOwner == $identityValue )
          #set( $isOwnerAuthorized = true )
        #end
      #end
    #end
    #if( $util.isString($allowedOwners0) )
      #if( $allowedOwners0 == $identityValue )
        #set( $isOwnerAuthorized = true )
      #end
    #end
    ## [End] Owner Authorization Checks **
  
  
    ## [Start] Throw if unauthorized **
    #if( !($isStaticGroupAuthorized == true || $isOwnerAuthorized == true) )
      $util.unauthorized()
    #end
    ## [End] Throw if unauthorized **
  #end
  ## [End] Check authMode and execute owner/group checks **
  
  $util.toJson(null)
  </code></pre>
  <p>Every time the code runs, I get:</p>
  <p><code>subscription error</code></p>
  <p>logged to the console.</p>
  <p>Analyzing the console output, I found the following info:</p>
  <pre><code>LOG  [DEBUG] 39:51.456 AWSAppSyncRealTimeProvider - subscription message from AWS AppSyncRealTime: {&quot;type&quot;:&quot;connection_ack&quot;,&quot;payload&quot;:{&quot;connectionTimeoutMs&quot;:300000}}
  LOG  [DEBUG] 39:51.457 AWSAppSyncRealTimeProvider - Notifying connection successful
  LOG  [DEBUG] 39:51.459 AWSAppSyncRealTimeProvider - subscription message from AWS AppSync RealTime: {&quot;type&quot;:&quot;ka&quot;}
  LOG  [DEBUG] 39:51.459 AWSAppSyncRealTimeProvider {&quot;id&quot;: &quot;&quot;, &quot;observer&quot;: null, &quot;query&quot;: &quot;&quot;, &quot;variables&quot;: {}}
  LOG  [DEBUG] 39:51.620 AWSAppSyncRealTimeProvider - subscription message from AWS AppSync RealTime: {&quot;id&quot;:&quot;9f72da89-0d1d-4aa8-8457-78bb1832478b&quot;,&quot;type&quot;:&quot;error&quot;,&quot;payload&quot;:{&quot;errors&quot;:[{&quot;errorType&quot;:&quot;MappingTemplate&quot;,&quot;message&quot;:&quot;Value for field '$[operation]' not found.&quot;}]}}
  LOG  [DEBUG] 39:51.620 AWSAppSyncRealTimeProvider {&quot;id&quot;: &quot;guid&quot;, &quot;observer&quot;: {&quot;_subscription&quot;: {&quot;_cleanup&quot;: [Function anonymous], &quot;_observer&quot;: [Object], &quot;_queue&quot;: undefined, &quot;_state&quot;: &quot;ready&quot;}}, &quot;query&quot;: &quot;subscription OnCreateHealthPlan($owner: String!) {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
      onCreateHealthPlan(owner: $owner) {
          id
          name
          type
          owner                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
      }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     }&quot;, &quot;variables&quot;: {&quot;owner&quot;: &quot;cognitoSubField&quot;}}
  LOG  [DEBUG] 39:51.622 AWSAppSyncRealTimeProvider - Error while unsubscribing Error: Subscription never connected
  LOG  [DEBUG] 39:52.624 AWSAppSyncRealTimeProvider - closing WebSocket...
  </code></pre>
  <p>It seems that this is the real error:
  <code>Value for field '$[operation]' not found</code></p>
  <p>Any ideas of why this is happening? </p>