Graphql Querying using URLSession

rab eeh
rab eeh
Sep 26, 2019 · 2 min read

Does your app have so much weight because of third party framework for API request? Do you want to reduce your app weight? then this is the solution you looking for.

What is URLSession ?

The URLSession provide an API for downloading data from and uploading data to endpoints indicated by URLs

Why I’m Using UrlSession?

There are lots of third party frameworks available for API requests, but they have lots of other features in the third party framework and we don’t use it in our app , so that third party frameworks increase our app weight. If we use other features in that third party frameworks than its cool.

How easily use urlSession?

Here I’m showing you how easily use GET, POST and PUT API requests

First we have to create a Router class

Router class

Router class is used for initialising URLSession using Codable , then we create a protocol as Routable

URLSession needs URLRequest for getting a response from the server and in the Routable Protocol we are getting info for the URLRequest. Now create APIs.swift class

here we are using the Routable Protocol for getting all details for URLRequest. APIs.swift class is used for API requests. APIs.swift have enum for return Routable Protocol ,that enum contains url, methord, endPoint, header, body and request . We have to create a struct class for parse from our API response named Login_Data.swift for parse POST response

Login_Data.swift is used for parse response from the server. Login_Data struct contain data, the data parse the JSON String and AffectedRaw struct contains affectedRaw and returning. the returning variable is return some value is responce from POST request. Now we have to create another struct class named List_Data.swift to parse values from the GET response. So we have to create variables like graphql response variable.

Login_Data.swift will fetch all values from the response, List_Data stuct contains data, it is array of Lists. List Contains id, List_Created_Date, name and address (these are i added for a example). Now we have to create an NSObject class named APIRequest.swift

So now we can request like APIRequest.shared.getAllLists or APIRequest.shared.postCredentials. postCredential function is used for POST request and parse return response from the server. And getAllLists function is used for GET request and parse the response. If we use this method for API requests so we can reduce our app weight because of no third party framework. The URLSession is not for Graphql query we can use URLSession for REST API request also. We have to change body and header as like REST API request

If you enjoy this quick tip, you can give it a ❤ or share it out. Thanks! :)

Thanks to Balaji Malliswamy and Ashok Kumar

rab eeh

Written by

rab eeh

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade