How to read results using Apollo Client for iOS

781 views Asked by At

I'm trying to use GraphQL in iOS with Apollo Client. I have the following mutation:

login(user: String!, password: String!): UserType

and the UserType looks like this:

id: ID
user: String!
password: String!
name: String
lastname: String
email: Email!
groups: [GroupType]

In iOS, I have configured aopllo client as the doc says and is working perfectly, but I don't know how to get access to every field in the response. When the login success I want to read the json I receive as response with the UserType fields, so, I'm doing this:

apolloClient.perform(mutation: loginMutation) {
                resultsGQL, error in
...
}

My question is, how can I read every field from resultGQL which belongs to the UserType data defined in my grapql schema?

Regards

1

There are 1 answers

0
Alienbash On

The question is not 100% clear, since it is missing some code your mutation: A GraphQL mutation has to return at least one value, which you have to define. Since i'm not sure about your method

login(user: String!, password: String!): UserType

i am giving you a simple example for updating an existing userProfile with a GraphQL mutation and then returning every field being defined in your schema for userType. Let us assume you have a userType (and therefore know the corresponding userId) and you want to modify the email:

mutation updateUserProfile($id: ID!, $newEmail: String) {
updateUser(id: $id, email: $newEmail) {
    id
    user
    password
    name
    lastName
    email 
   }
}

As you can see, after executing

updateUser(id: $id, email: $newEmail)

all return values are defined inside the following {...} parentheses and are therefore accessible in your callback variable

resultsGQL

That means:

apolloClient.perform(mutation: loginMutation) { resultsGQL, error in
    if let results = resultsGQL?.data {
      // "results" can now access all data from userType
  }
}

Since you defined all entities of your userType schema to be returned from the mutation, you can access them now in your callback variable.