Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exceptional break point issue - passing token on Header. #112

Closed
alvin-george opened this issue Jul 19, 2017 · 6 comments
Closed

Exceptional break point issue - passing token on Header. #112

alvin-george opened this issue Jul 19, 2017 · 6 comments
Milestone

Comments

@alvin-george
Copy link

alvin-george commented Jul 19, 2017

this is my StudentProfile.graphql file:

query StudentProfile{

me{
... on Student{

profile{
 city
}

}
}
}

I want to make a fetch request to get data from server. After log in, I am supposed to send token as http header with the query to Get a particular student's profile details.

This is my button action:

@IBAction func fetchProfileClicked(_ sender: Any) {
        
        //self.fetchStudentProfileDetails()
        
        
        let configuration: URLSessionConfiguration = .default
        configuration.httpAdditionalHeaders = ["Authorization": "Bearer \(serverToken)"]
        configuration.requestCachePolicy = .reloadIgnoringLocalCacheData // To avoid 412
        
        let url = URL(string: "http://52.88.217.19/graphql")!
        let new_apollo = ApolloClient(networkTransport: HTTPNetworkTransport(url: url, configuration: configuration))
        new_apollo.cacheKeyForObject = { $0[self.currentUsedId!] }
     
        
        new_apollo.fetch(query: StudentProfileQuery()) { (result, error) in
            
            
            if let error = error {
                NSLog("Error while fetching query: \(error.localizedDescription)");
                self.profileDetailsTextView.text =  error.localizedDescription
            }
            guard let result = result else {
                NSLog("No query result");
                self.profileDetailsTextView.text = "No query result"
                return
            }
            
            if let errors = result.errors {
                NSLog("Errors in query result: \(errors)")
                
                self.profileDetailsTextView.text =  String(describing: errors)
                
            }
            
            guard let data = result.data else {
                
                NSLog("No query result data");
                
                return
            }


        }

    }

These are the error I get in Xcode. Any help is appreciated.
screen shot 2017-07-19 at 12 39 27 pm

screen shot 2017-07-19 at 12 39 18 pm

@martijnwalraven
Copy link
Contributor

new_apollo.cacheKeyForObject = { $0[self.currentUsedId!] }

cacheKeyForObject should be a function that returns the cache key for any object that gets passed in. Your code tries to access a field on a passed in object with the value of self.currentUsedId!, but that is not a field of the object. Instead, you'll want to do something like:

new_apollo.cacheKeyForObject = { $0["id"] }

@alvin-george
Copy link
Author

How do we pass token in http header along with graphic query on Apollo.request?

@designatednerd
Copy link
Contributor

@alvinvgeorge This is tied to the issues around #37 - at this point you'd need to configure a new client with the updated header query, or just set up the headers when the client is set up.

We'll be working in that issue around getting that working better. Is there anything else we can help with here?

@designatednerd
Copy link
Contributor

The updated client that allows you to set headers in preflight is now available in 0.11.0 - I'm going to close this issue out, but please feel free to open a new issue if you're still encountering any problems using that!

@designatednerd designatednerd added this to the 0.11.0 milestone Jul 10, 2019
@ghost
Copy link

ghost commented Sep 3, 2019

Does anyone have suggestion for how to pass access token in a header file?
My Apollo version is latest 0.15.0

@designatednerd
Copy link
Contributor

@ShreyanshAlchemy Here's our documentation on advanced client setup, including an example that shows how to add information to a request header using the HTTPNetworkTransportPreflightDelegate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants