-
Notifications
You must be signed in to change notification settings - Fork 208
Expand file tree
/
Copy pathGraphQLDemoView.swift
More file actions
160 lines (142 loc) · 5.61 KB
/
Copy pathGraphQLDemoView.swift
File metadata and controls
160 lines (142 loc) · 5.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//
// GraphQLDemoView.swift
// Example
//
// Created by Matheus Gois (GraphQL Demo) on 17/07/26.
//
import SwiftUI
/// Fires real GraphQL POSTs against a public endpoint so the GraphQL
/// Operation Inspector is testable from the Example app.
struct GraphQLDemoView: View {
@State private var responseText = ""
@State private var isLoading = false
@State private var statusCode: Int?
@State private var errorMessage: String?
private let endpoint = URL(string: "https://countries.trevorblades.com/")!
var body: some View {
ScrollView {
VStack(alignment: .leading, spacing: 16) {
Text("GraphQL Operation Inspector")
.font(.title2.bold())
Text("Fires real GraphQL POST requests against countries.trevorblades.com. "
+ "Open DebugSwift → Network → tap the capture to see the GraphQL section "
+ "(operation name/type, variables, data/errors split).")
.foregroundColor(.secondary)
Button(action: runQuery) {
Label("Run query (GetCountries)", systemImage: "play.circle.fill")
.frame(maxWidth: .infinity)
.padding(.vertical, 8)
.background(Color.accentColor.opacity(0.15))
.cornerRadius(8)
}
.disabled(isLoading)
Button(action: runMutationShapedQuery) {
Label("Run mutation-shaped query", systemImage: "play.circle")
.frame(maxWidth: .infinity)
.padding(.vertical, 8)
.background(Color(.secondarySystemBackground))
.cornerRadius(8)
}
.disabled(isLoading)
Button(action: runAnonymousQuery) {
Label("Run anonymous query", systemImage: "play")
.frame(maxWidth: .infinity)
.padding(.vertical, 8)
.background(Color(.secondarySystemBackground))
.cornerRadius(8)
}
.disabled(isLoading)
if isLoading {
ProgressView("Sending…")
}
if let statusCode {
Text("Status: \(statusCode)")
.font(.subheadline)
.foregroundColor(statusCode == 200 ? .green : .red)
}
if let errorMessage {
Text(errorMessage)
.foregroundColor(.red)
.font(.caption)
}
if !responseText.isEmpty {
Text("Response")
.font(.headline)
Text(responseText)
.font(.body)
.padding(8)
.background(Color(.secondarySystemBackground))
.cornerRadius(8)
}
}
.padding()
}
.navigationTitle("GraphQL Demo")
.navigationBarTitleDisplayMode(.inline)
}
private func runQuery() {
let query = """
query GetCountries($code: ID!) {
country(code: $code) {
name
native
capital
emoji
}
}
"""
let variables: [String: Any] = ["code": "BR"]
send(query: query, variables: variables)
}
private func runMutationShapedQuery() {
// A query that *looks* like a mutation in its keyword — the inspector
// should classify it as `.mutation` based on the leading keyword.
let query = """
mutation SetFlag {
__typename
}
"""
send(query: query, variables: nil)
}
private func runAnonymousQuery() {
// No operation name — inspector should return nil for the name.
let query = "{ __typename }"
send(query: query, variables: nil)
}
private func send(query: String, variables: [String: Any]?) {
isLoading = true
errorMessage = nil
responseText = ""
statusCode = nil
var request = URLRequest(url: endpoint)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("application/json", forHTTPHeaderField: "Accept")
var body: [String: Any] = ["query": query]
if let variables { body["variables"] = variables }
request.httpBody = try? JSONSerialization.data(withJSONObject: body)
URLSession.shared.dataTask(with: request) { data, response, error in
DispatchQueue.main.async {
isLoading = false
if let error {
errorMessage = error.localizedDescription
return
}
if let http = response as? HTTPURLResponse {
statusCode = http.statusCode
}
if let data,
let json = try? JSONSerialization.jsonObject(with: data),
let pretty = try? JSONSerialization.data(
withJSONObject: json,
options: [.prettyPrinted, .sortedKeys]
),
let text = String(data: pretty, encoding: .utf8) {
responseText = text
} else if let data, let text = String(data: data, encoding: .utf8) {
responseText = text
}
}
}.resume()
}
}