gpt4 book ai didi

json - 如何在 swift 4 中使用 Graphql 获取 json 值数组?

转载 作者:行者123 更新时间:2023-11-28 05:52:41 24 4
gpt4 key购买 nike

我正在使用 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 查询时 This is graphql Response

 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 值有什么问题?请帮助我!

最佳答案

GraphQL 最好的地方是我们可以使用查询作为模型

因为响应与查询相同,所以我们可以更好地将响应分配给查询类型的变量。

让我用一个例子来解释:-

假设我需要查询我的个人资料数据,

Profile.graphql

query MyProfile{
player {
me {
id
secret
name
email
state
country
timezone
picture
pictureType
audio
rank
}
}
countries{
value
viewValue
}
}

Once we'll build the app, it'll create MyProfileQuery in API.swift. In viewController we can use the response as below-

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")
})
})
}
}

}

Now we have response into the myProfileData variable which we can use as follows -

现在我们可以访问我们在查询中提到的所有值,如下所示

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
//

希望对您有所帮助👍👍👍

关于json - 如何在 swift 4 中使用 Graphql 获取 json 值数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52343079/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com