Skip to content

Instantly share code, notes, and snippets.

@alyssoncm
Created September 23, 2019 20:55
Show Gist options
  • Save alyssoncm/db82f00a8330d88fcce24aaeb7f9da18 to your computer and use it in GitHub Desktop.
Save alyssoncm/db82f00a8330d88fcce24aaeb7f9da18 to your computer and use it in GitHub Desktop.
button swift
Button(action: {
// Perform the SignUpUser mutation, passing the parameters we just got from our TextFields
apollo.perform(mutation: SignUpUserMutation(username: self.username, password: self.password, email: self.email)){ result in
// Let's switch the result so we can separate a successful one from an error
switch result {
// In case of success
case .success(let graphQLResult):
// We try to parse our result
if let objId = graphQLResult.data?.users?.signUp.objectId {
myMessage.alertTitle = "Yay!"
myMessage.alertText = "User signed up!"
self.showingAlert = true
print ("User created with ObjectId: " + objId)
}
// but in case of any GraphQL errors we present that message
else if let errors = graphQLResult.errors {
// GraphQL errors
myMessage.alertTitle = "Oops!"
myMessage.alertText = "We've got a GraphQL error: " + errors.description
self.showingAlert = true
print(errors)
}
// In case of failure, we present that message
case .failure(let error):
// Network or response format errors
myMessage.alertTitle = "Oops!"
myMessage.alertText = "We've got an error: " + error.localizedDescription
self.showingAlert = true
print(error)
}
}
}){
Text("Sign Up!")
.font(.headline)
.foregroundColor(.white)
.padding()
.frame(width: 220, height: 60)
.background(lightBlueColor)
.cornerRadius(15.0)
}
.alert(isPresented: $showingAlert) {
Alert(title: Text(myMessage.alertTitle), message: Text(myMessage.alertText), dismissButton: .default(Text("OK")))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment