I'm using Apollo iOS to fetch GraphQL queries. I want to move the apollo.fetch()
query closures to a separate function in a class. This class will contain a static reference to the apollo client as well as functions for performing GraphQL mutations and queries.
I'm trying the following:
static func fetchQueryResults() -> CountriesQuery.Data.Country?{
var myResult: CountriesQuery.Data.Country?
myResult = nil
apollo.fetch(query: countriesQuery) { (result, error) in
print(result?.data)
myResult = result?.data //this line causes error
}
return myResult
}
Whenever I add the line myResult = result?.data
I get the error Generic parameter 'Query' could not be inferred.
However, when the line is commented out it works fine, but obviously the function is pointless. Eventually I would like to generalize this function so I could pass the query into it, but how do I get the data out of this basic closure?
In essence the question is, how do I "wrap" a closure in a function?
The point of this function is to be able to get the number of rows for the table view section in the function:
override func tableView(_ tableView:UITableView, numberOfRowsInSection section: Int -> Int{
return fetchQueryResults.count
}
However, the view loads before this function runs. I think this is because the apollo.fetch()
is running asynchronously?