【问题标题】:Apollo Vue (using AWS Amplify/AppSync) Graphql query does not pull parent dataApollo Vue(使用 AWS Amplify/AppSync)Graphql 查询不拉取父数据
【发布时间】:2020-07-27 13:58:42
【问题描述】:

我正在使用 AWS AppSync(通过 Apollo Vue)并且我有以下 graphql 模型。

type Service @model
{
  id: ID!
  name: String!
  description: String!
}

type Session @model
{
  id: ID!
  service: Service @connection(keyName: "ServiceSessionConnection")
}

服务表解析器:

#set( $limit = $util.defaultIfNull($context.args.limit, 10) )
#set( $query = {
  "expression": "#connectionAttribute = :connectionAttribute",
  "expressionNames": {
      "#connectionAttribute": "serviceSessionsId"
  },
  "expressionValues": {
      ":connectionAttribute": {
          "S": "$context.source.id"
    }
  }
} )
{
  "version": "2017-02-28",
  "operation": "Query",
  "query":   $util.toJson($query),
  "scanIndexForward":   #if( $context.args.sortDirection )
    #if( $context.args.sortDirection == "ASC" )
true
    #else
false
    #end
  #else
true
  #end,
  "filter":   #if( $context.args.filter )
$util.transform.toDynamoDBFilterExpression($ctx.args.filter)
  #else
null
  #end,
  "limit": $limit,
  "nextToken":   #if( $context.args.nextToken )
$util.toJson($context.args.nextToken)
  #else
null
  #end,
  "index": "gsi-Service.sessions"
}

会话表解析器:

{
  "version": "2017-02-28",
  "operation": "GetItem",
  "key": {
      "id": $util.dynamodb.toDynamoDBJson($util.defaultIfNullOrBlank($ctx.source.sessionServiceId, "___xamznone____"))
  }
}

我正在创建以下记录:

mutation createService {
   createService {
     id: "Id1",
     name: "name",
     description: "desc"
   }
}
mutation createSession {
   createSession {
     id: "Id2",
     serviceSessionsId: "Id1" //note: serviceSessionsId created part of relationship (it is part of the resolved code above!), and I am setting that to parent ID (in this case serviceId value "Id1")
   }
} 

我正在尝试使用父(服务)数据运行以下子表(会话)查询:

query listSessions {
  listSessions {
    items {
      id,
      serviceId,
      service {        //retuns null data
        id,            //retuns null data
        name,          //retuns null data
        description.   //retuns null data
      }
    }
  }
}

我没有得到父(服务对象)数据。由于我是建模的子父@connection(keyName: "ServiceSessionConnection") 关系,我不应该期望查询结果中有父数据吗?我错过了什么吗?查询结果中还需要配置什么关系数据?

【问题讨论】:

    标签: graphql aws-amplify aws-appsync quasar vue-apollo


    【解决方案1】:

    进一步剖析解析器代码,通过在创建子记录 createSession() 时将 serviceSessionsIdsessionServiceId 设置为 Id1 解决了问题

    【讨论】:

      猜你喜欢
      • 2021-02-21
      • 2021-04-24
      • 2020-11-01
      • 2020-08-20
      • 2020-11-23
      • 2021-01-25
      • 2019-09-07
      • 2020-04-17
      • 2018-11-12
      相关资源
      最近更新 更多