Getting @aws_subscribe to work with CDK aws-appsync

0

For the application I'm building Amplify is an overkill. I've been able to manage to build graphQL with appsync and cdk only at this point. Now, I'm running into issue with @aws_subscribe -- it just does not work for me. I'm wondering if the directives like this require Amplify to be aded and initialized? Is there any way to initialize and process graphQL subscriptions with CDK api directly?

My schema.graphql contains certain definitions:

type Message{  
  chatUuid: String!
  uuid: String!
  messageUuid: String!
  text: String! 
  createdAt: AWSDateTime!  
  updatedAt: AWSDateTime!  
}

  sendMessage(
    chatUuid: String!
    uuid: String!
    messageUuid: String!
    text: String! 
  ): Message
   
}

type Subscription {
  onSendMessage(chatUuid: String!): Message    
    @aws_subscribe(mutations: ["sendMessage"])
}

And here is how I'm initializing my graphQL with CDK off of that definition file:

const api = new appsync.GraphqlApi(this, `${deployEnv()}-WiSaw-appsyncApi-cdk`,
      {
        name: `${deployEnv()}-cdk-wisaw-appsync-api`,
        schema: appsync.Schema.fromAsset('graphql/schema.graphql'),
        authorizationConfig: {
          defaultAuthorization: {
            authorizationType: appsync.AuthorizationType.API_KEY,
            apiKeyConfig: {
              expires: cdk.Expiration.after(cdk.Duration.days(365)),
            },
          },
        },
      })

For debugging purposes, tried to attach a resolver to the subscription:

export default async function main(
  event:any
) {

  console.log({event,})

  return {}
}

In the log see the following:

{
  event: {
    arguments: {},
    identity: null,
    source: null,
    request: { headers: [Object], domainName: null },
    prev: null,
    info: {
      selectionSetList: [Array],
      selectionSetGraphQL: '{\n  chatUuid\n  createdAt\n  messageUuid\n  text\n  updatedAt\n  uuid\n}',
      fieldName: 'onSendMessage',
      parentTypeName: 'Subscription',
      variables: {}
    },
    stash: {}
  }
}

At this point, I'm running out of things to try. Any help or direction would be highly appreciated.

  • I know this is definately possible. Am trying to recreate your example but it contains some typos.

已提問 2 年前檢視次數 459 次
1 個回答

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南