Search code examples
javascriptfacebookcallbackfacebook-android-sdk

Facebook graph API: return object from GraphRequest callback


I have a function that successfully makes requests, but am having trouble translating it to the React-Redux data flow because I can't figure out how to return the object that is received in the callback.

What I currently have doesn't work, but I am confident it makes the request successfully because I can see it when I console.log it.

import FBSDK, { AccessToken, GraphRequest, GraphRequestManager } from 'react-native-fbsdk'

export const callback = (error, result) => {
  if (error) {
    console.log(error);
    return error
  } else {
    console.log(result);
    return result;
  }
}

export const graphRequestFor = fields => {
  AccessToken.getCurrentAccessToken()
  .then(token => {
    console.log(token);
    const request = new GraphRequest(
      '/me',
      {
        accessToken: token.accessToken,
        parameters: {
          fields: {
            string: fields
          }
        }
      }, callback
    )
    new GraphRequestManager().addRequest(request).start()
  })
}
export const login = () => {
    graphRequestFor('first_name, last_name')
    .then(results => {
      return results
    }) //undefined
  }

Solution

  • Not an answer to my direct question, but a solution to my problem is to make the api call directly inside the action and dispatching the object from the callback

    import FBSDK, { AccessToken, GraphRequest, GraphRequestManager } from 'react-native-fbsdk'
    
    export const login = params => dispatch => {
      AccessToken.getCurrentAccessToken()
      .then(token => {
        const request = new GraphRequest(
          '/me',
          {
            accessToken: token.accessToken,
            parameters: {
              fields: {
                string: params
              }
            }
          }, (error, result) => {
            if (error) {
              console.log(error);
              return dispatch(receiveCurrentUser(error))
            } else {
              return dispatch(receiveCurrentUser(result))
            }
          }
        )
        new GraphRequestManager().addRequest(request).start()
      })
    }