AWS Amplify AppSync订阅:数据返回空

问题描述

我正在开发我的Amplify应用程序,并且我的订阅可以正常使用:

graphql:

type Item @model(subscriptions: null)
    @auth(rules: [
        {allow: owner},{allow: groups,groups: ["Admin"],operations: [create,update,read,delete] }
  ]) {
  id: ID!
  name: String
  files: String
}


type Subscription {
    itemUpdated(id: ID): Item @aws_subscribe(mutations: ["updateItem"])
}

js:

const handleSubscription = (data) => {
  if (data.value.data.itemUpdated) {
    setItemObj(data.value.data.itemUpdated);
  }
};
useEffect(() => {
  const subscription = API.graphql(
      graphqlOperation(subscriptions.itemUpdated,{
        id,}),).subscribe({
      next: handleSubscription,});
    return () => subscription.unsubscribe();
  },[]);

在handleSubscription方法中,当应用对商品进行突变调用时,返回数据(data.value.data.itemUpdated)将具有正确的数据。

现在,由于我不清楚的原因,当发生突变时,我仍然可以看到订阅事件触发,但是返回数据(data.value.data.itemUpdated)始终为空。

我已尝试按照This Question的建议从graphql模式的auth字段中删除{allow: owner}规则-没用(除了:我仍然很好奇为什么这首先会起作用,但我没有足够的代表对此发表评论。)

在撰写本文时,我的想法是我将尝试创建一个没有{allow: owner}规则的新商品,然后再试一次,如果可行,我将进行报告,但是我的问题将转向问为什么和然后问我如何确保物品仍是所有者专有的?最后,我几乎可以肯定的是,我也有{allow: owner}规则在起作用的情况,但是我可能会误会。

我也尝试过:

  • 已测试过更新不同的Item字段

  • 让扩增cli重建我的graphql js文件

  • 更改了代码,即

    • 删除return () => subscription.unsubscribe();
    • 使输入更加具体API.graphql(graphqlOperation(subscriptions.itemUpdated,{input: { id: id },})(我相信这没关系,但我想尝试。)

我只是不确定这里发生了什么。一切似乎都这么简单,一定是我想念的愚蠢……我知道最终我会弄清楚的,但是我想在这里找人,以防万一。

版本:

  • "aws-amplify": "^3.0.24"
  • "@aws-amplify/ui-react": "^0.2.15"
  • "react": "^16.13.1"
  • amplify-cli: 4.29.0

请让我知道是否遗漏了任何重要信息。预先感谢您的帮助。

解决方法

好吧..就像我想的那样愚蠢。我很浪费时间!

library(shiny)
library(shinyWidgets)
# ui object

ui <- fluidPage(
  titlePanel(p("Spatial app",style = "color:#3474A7")),sidebarLayout(
    sidebarPanel(
      uiOutput("inputp1"),numericInput("num",label = ("value"),value = 1),#Add the output for new pickers
      uiOutput("pickers"),actionButton("button","Update")
    ),mainPanel(
      DTOutput("table")
    )
  )
)

# server()
server <- function(input,output,session) {
  DF1 <- reactiveValues(data=NULL)
  
  dt <- reactive({
    name<-c("John","Jack","Bill")
    value1<-c(2,4,6)
    dt<-data.frame(name,value1)
  })
  
  observe({
    DF1$data <- dt()
  })

  output$inputp1 <- renderUI({
    pickerInput(
      inputId = "p1",label = "Select Column headers",choices = colnames( dt()),multiple = TRUE,options = list(`actions-box` = TRUE)
    )
  })

  observeEvent(input$p1,{
    #Create the new pickers
    output$pickers<-renderUI({
      dt1 <- DF1$data
      div(lapply(input$p1,function(x){
        if (is.numeric(dt1[[x]])) {
          sliderInput(inputId=x,label=x,min=min(dt1[[x]]),max=max(dt1[[x]]),value=c(min(dt1[[x]]),max(dt1[[x]])))
        }else { # if (is.factor(dt1[[x]])) {
          selectInput(
            inputId = x,# The col name of selected column
            label = x,# The col label of selected column
            choices = dt1[,x],# all rows of selected column
            multiple = TRUE
          )
        }

      }))
    })
  })


  dt2 <- eventReactive(input$button,{
    req(input$num)
    dt <- DF1$data ## here you can provide the user input data read inside this observeEvent or recently modified data DF1$data
    dt$value1<-dt$value1*isolate(input$num)

    dt
  })
  observe({DF1$data <- dt2()})
  
  output_table <- reactive({
    req(input$p1,sapply(input$p1,function(x) input[[x]]))
    dt_part <- dt2()
    for (colname in input$p1) {
      if (is.factor(dt_part[[colname]]) && !is.null(input[[colname]])) {
        dt_part <- subset(dt_part,dt_part[[colname]] %in% input[[colname]])
      } else {
        if (!is.null(input[[colname]][[1]])) {
          dt_part <- subset(dt_part,(dt_part[[colname]] >= input[[colname]][[1]]) & dt_part[[colname]] <= input[[colname]][[2]])
        }
      }
    }
    dt_part
  })
  
  output$table<-renderDT({
    output_table()
  })

}

# shinyApp()
shinyApp(ui = ui,server = server)

它是API.graphql( graphqlOperation(subscriptions.itemUpdated,{ id: Id,}),).subscribe({ next: handleSubscription,}); 参数。我之前将id: Id,变量命名为Id,并且js允许将id短接到{name: name}-我必须更改了{ name }变量并直接转到{ {1}},这是订阅的错误语法。

真正的骨头动作,我感到很尴尬。即使在测试过程中,也可以从不良的命名中学到好教训。