Execute Apollo GraphQL Query in iOS

Chandan
3 min readMar 6, 2022

--

Executing GraphQL Query.

Hello Everybody,
This article is very helpful for you.

PART-1

If you completed these five points, then you can continue it…
I want to complete these 5 points.

  1. I already add Apollo-iOS in my project β†’ Article
  2. I already add BrightFutures in my project β†’ Article
  3. I already add GraphQL Schema file in my project β†’ Article
  4. I already Auto-generate API.Swift file β†’ Article
  5. I already created Advance Apollo Client in my project β†’ Article

in this example we use this end point to fetch query.

GraphQL Endpoint β†’ https://countries.trevorblades.com

First you should check your query is working or not in your GraphQL Playgroud.

Yehhh! My Query is working good.

If your query is working good then copy your query and paste in your Query.graphql file in your projecrt.

Get Country Query
Query pasted in query.graphql file

NOTE: β†’ If in your side this query is not comming colorfull then please don't worry πŸ˜‰. I β€˜ll explain later ok, no worries πŸ˜‰.

Ok after pasting Query in query.graphql file press Command + B or Build your project.

your XCode will Generate API for getCountry Query.

Well done, here your part 1 is completed, Take rest if you want take Tea or coffee. and come back again.

Part -2

Now you should create a file that name is YOUR_PROJECT_NAMEAPI.swift

You can set deferent name of that file according to your project.

For Example.

File Name Example.

And in this LearningGraphQLAPI.swift file import three things files and also create a class in this file and class name is LearningGraphQLAPI.

creating class and importing some library.

And then in the class create a new method called getCountryDeatils().πŸ‘‡πŸΌ

Creating GetCountryDetails()

And in video πŸ‘†πŸΌ I forgot to create static shared object of LearningGraphQLAPI in this class see line no. 15 πŸ‘‡πŸΌ.

Pseudo code of GetCountryDetailsQuery()

I hope in your side there are no errors.

Now Open Your view controller file and write this code. πŸ‘‡πŸΌ watch video.

Fetch data

OUTPUT β†’

NOTE β†’ I hope everything is working fine for you. and if you getting some or any kind of error then please comment πŸ‘‡πŸΌ.

or you can PING me β†’ trot.stalls.0l@icloud.com

--

--