json - 如何在 swift 4 中使用 Graphql 获取 json 值数组?
问题描述
我正在使用 graphql 使用 Apollo 获取 API 值。我已成功下载 schema.json 并从 grpahql 获取值。但我无法获取 json 值的值数组。
这是我的示例回复:
{
"data": {
"team_Dashboard": {
"activeMission": "Food Mission",
"currentMissionChallenges": [
{
"id": "123",
"title": "Challenge",
"estTime": "5",
"location": "Anywhere",
"maxPts": "30",
"status": "Not yet started"
},
{
"id": "1234",
"title": " II Challenge",
"estTime": "5",
"location": "Anywhere",
"maxPts": "70",
"status": "Not yet started"
}
]
}
}
}
Graphql 查询:
query teamDashboard($teamId: ID!) {
team_Dashboard(teamId: $teamId) {
activeMission
currentMissionChallenges
}
}
Graphql 模式响应:
missionDeadLine: String
currentMissionChallenges: [JSON]
当我在我的 Graphql 查询中添加 currentMissionChallenges([JSON]) 时,会从服务器获取错误响应。但是当我从 Graphql 查询中删除 currentMissionChallenges 时,会从服务器获取成功响应和值。
问题是 currentMissionChallenges 是 [JSON] 格式。当我更改我的 graphql 查询时,这是 graphql 响应
query teamDashboard($teamId: ID!) {
team_Dashboard(teamId: $teamId) {
activeMission
currentMissionChallenges {
id
title
estTime
location
maxPts
status
}
}
}
dashBord.grpahql 中显示以下错误
Field "currentMissionChallenges" must not have a selection since type "[JSON]" has no subfields.
如何从 graphql 获取 json 数组值。获取 Json 值有什么问题?请帮我!
解决方案
The best thing about GraphQL is we can use the query as model
因为响应与查询相同,所以我们可以更好地将响应分配给查询类型的变量。
让我用一个例子来解释:-
假设如果我需要查询我的个人资料数据,
Profile.graphql
query MyProfile{
player {
me {
id
secret
name
email
state
country
timezone
picture
pictureType
audio
rank
}
}
countries{
value
viewValue
}
}
一旦我们构建应用程序,它将在 API.swift 中创建 MyProfileQuery。在 viewController 中,我们可以使用如下响应 -
var myProfileData: MyProfileQuery.Data.Player.Me? // Declaring the valiable of player Type
ApolloClientManager.sharedInstance
.fetchQuery(MyProfileQuery(), showLoader: true,
viewController: self) { (response) in // Fetching response using Apollo Client Manager
if let allData = response {
if let profiledata = allData.player?.me {
self.myProfileData = profiledata // Assigning response into the variable declared
self.myEdittingProfileData = profiledata
self.updateUI()
}
if let countryData = allData.countries {
self.allCountrydata = countryData
self.getPickerDataForState(comppletion: {
self.openTimeZonePicker(completion: {
print("got timeZone Data")
})
})
}
}
}
现在我们对myProfileData变量有响应,我们可以使用如下 -
现在我们可以访问我们在查询中提到的所有值,如下所示
print("player id is- \(myProfileData?.id)")
print("player name is- \(myProfileData?.name)")
print("player email is- \(myProfileData?.email)")
print("player state is- \(myProfileData?.state)")
print("player rank is- \(myProfileData?.rank)")
print("player pictureType is- \(myProfileData?.pictureType)")
// player id is- 10
// player name is- jordan
// player email is- jordan@domain.com
// player state is- Ohio
// player rank is- 101
// player pictureType is- custome
//
希望这会帮助你
推荐阅读
- javascript - 酵素的浅层是渲染孩子的孩子?
- vue.js - 子到父事件发出
- vue.js - 将 vanilla-lazyload 与 vuejs 一起使用
- python - 使用多种字符串格式解析挑战
- php - 如何将属性传递回父类构造函数方法
- python-3.x - 使用 Pipenv 环境创建的 Virtualenv 始终可以访问站点包
- java - 使用 Docker 从 Java 调用 .NET Core
- java - Saxon XQueryCompiler的优化
- python - 尝试在 python 3.7 中读取 netcdf 文件时出现未知文件格式错误
- vba - 从日期中删除时间戳标记