iOS
Using GraphQL Apollo iOS Client in a Swift Project
22 min
introduction in this section you will learn how to install the apollo ios client on your swift project and query data from back4app using it prerequisites to complete this quickstart, you need xcode an app created at back4app follow the new parse app tutorial to learn how to create a parse app at back4app a class with some data stored (so you can retrieve it) 1 getting apollo client into your xcode project the easiest way to integrate the apollo ios client is by using cocoapods https //cocoapods org/ in order to integrate it, follow these steps create your xcode project and in the same folder of your xcodeproj file, create a new file named podfile edit the podfile file and add the following code, changing the string yourprojectnamehere to your project’s name \# uncomment the next line to define a global platform for your project platform \ ios, '12 0' target 'yourprojectnamehere' do \# comment the next line if you're not using swift and don't want to use dynamic frameworks use frameworks! \# pods for conferenceplanner pod 'apollo' end save the file and open a terminal go to that folder and type when the installation finishes, you should have a new file with the format xcworkspace open that file with xcode 2 retrieve your schemas you need a file name schema json containing the schemas for your graphql endpoint there are two ways for you to retrieve your full schema using the back4app graphql console using apollo we will discuss both choose the one you like the best 2 1 retrieve your schemas with the back4app graphql console go to your graphql console for the app you want to retrieve the schema from and on, the right hand under the schema tab, click the download button 2 2 retrieve your schemas with apollo if you prefer to use apollo, first you have to install the desktop version by typing then, run the following command replacing the values for the headers with your appid and masterkey this will generate aschema json file as output 3 add your schema json file to the project add the schema json file that you downloaded or retrieved to your project in the root directory this is the same folder where your appdelegate swift file is located 4 create your graphql files you now can create your graphql files with your files and mutations those file must have the graphql extension and cointain at least one query or mutation in order for apollo to crate the swift code from a useful convention is to colocate queries, mutations or fragments with the swift code that uses them by creating \<name> graphql next to \<name> swift if you don’t have pre existing graphql files in your file tree, create a very simple query and add it to a graphql file in your file tree so that when you run the code generation build step, it actually finds something if you don’t, you’ll get the error no operations or fragments found to generate code for here is a simple query that as an example, that returns parse users 1 query findallusers{ 2 objects{ 3 find user{ 4 count 5 results{ 6 username 7 } 8 } 9 } 10 } add that file to your target directory at the same level your schema json file is 5 add a code generation build step you can invoke apollo as part of the xcode build process, which will retrieve and update the schema json file automatically so your classes will always reflect any changes you make by calling the check and run apollo cli sh wrapper script the wrapper checks if the version of apollo installed on your system is compatible with the framework version in your project if you don’t check that, you could potentially generate code that is incompatible with the runtime code in the framework the steps are on your application target’s build phases settings tab, click the + icon and choose new run script phase in the created run script, change its name to generate apollo graphql api drag this new run script just above compile sources in your list of build phases so that it executes before your code is compiled add the following to the run script if you are using xcode 11 beta, add this script instead 6 build and add your api file to the target build your project and a file named api swift should be created on your target’s directory drag the generated api swift file to your target and make sure to uncheck the “copy files if needed” checkbox make sure you checked all the targets the api file needs to be included in 7 configure the client now on your viewcontroller swift, create your apollo configuration and change your appid and clientkey values 1 let apollo apolloclient = { 2 let configuration = urlsessionconfiguration default 3 configuration httpadditionalheaders = \[ 4 "x parse application id" "yourappidhere", 5 "x parse client key" "yourclientkeyhere" 6 ] 7 8 let url = url(string "https //parseapi back4app com/graphql")! 9 10 return apolloclient( 11 networktransport httpnetworktransport( 12 url url, 13 configuration configuration 14 ) 15 ) 16 }() 8 configure the client on your viewdidload, call your graphql query from the apollo client 1 apollo fetch(query findallusersquery()) { result in 2 guard let data = try? result get() data else { return } 3 print(data objects? finduser results\[0] username) 4 } if you have any users in your user class, the first one (index 0) should be retrieved optional step get syntax highlighting you can have graphql syntax highlighting on xcode if you wish to achieve that clone the xcode apollo repository https //github com/apollostack/xcode apollo to your computer close xcode if it is currently running you may need to create these folders inside of /library/developer/xcode 4\ copy graphql ideplugin to /library/developer/xcode/plug ins 5\ copy graphql xclangspec to /library/developer/xcode/specifications you may receive a warning the first time you start up xcode after installing these add ons once you agree to load the plugin, you will no longer see this warning