iosgraphqlnsdictionary

Graphql -Data fetching in iOS


Anyone please help me to get the value of "data" from the below API response. It is the kind of GraphQL API response.

["data": {
    signin = {
       result = {
         firstName = evol;
          lastName = adc;
          profileImage = "http:/xxx.com/public/static/default.jpg";
          status = "<null>";
          token = "eyxxx";
         };
      };
  }]

Solution

  • You can get the value of data from the API response as,

     let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments)as? [String : Any]
     print("Json",json!)
    
     let jsonUnwrapped = json.unsafelyUnwrapped
     let jsonData = jsonUnwrapped["data"]as? [String : Any]
      print("data....", jsonData!)
      if let signup = jsonData.unsafelyUnwrapped as? [String : Any]
        {
         let signupDetails = signup["signin"]as? [String : Any]
          if  signupDetails?.count == 0 || signupDetails == nil
            {
              let error = jsonUnwrapped["errors"]
              print("error.....",error! )
              DispatchQueue.main.async {
                 self.view.makeToast("Invalid credentials", duration: 0.5, position: .bottom)
               }
            }
            else
            {
             DispatchQueue.main.async {
               //Your code here...
    
              }
         }