Calling FQL with the Facebook / Flex API

FQL is the query language used in Facebook to access tables and relationships. All the top level calls available within facebook APIs ( such as getFriends() ) are simply wrappers around FQL calls. In this post, I will show you how to use the newly released Adobe Flex / Facebook API to invoke FQL commands.

A simple FQL shell

Intro to FQL

After working with the Facebook API for a while, you’ll become slightly frustrated by the number of calls you need to make in order to get detailed information found across different tables.

Let’s say your requirement is: “Get all the names, sex and profile pictures of the user’s friends”.

Using API objects, you would need to first call a getFriends() command to get all the friends of the user. Then you would have to call getUserInfo() to get the details for each user.

Wouldn’t it be nice if you could just call a magic function that will do all this processing on the server and just return the data that you need?

Fortunately, the Facebook API has a very powerful query language called FQL. FQL allows you to get the data your application needs in one call, and makes your code much simpler.

Instead of the above, I could call the following command:

    SELECT name, sex, pic_square FROM user 
    WHERE uid IN ( SELECT uid2 FROM friend WHERE uid1= loggedInUser )

Which will return exactly the fields I need.

Using FQL with the Facebook API

I’ve included a complete example of a FQL shell here:

You would simply need to replace the “xxx” in the API Key and secret with one from your facebook application.

Here are the juicy bits:

private function invokeFQL( query : String, callback : Function ) : void {
    var call : FacebookCall = new FqlQuery( query ) );
    call.addEventListener(FacebookEvent.COMPLETE, callback );

To call FQL, you simply need to create a FqlQuery() object with the query that you need and add a callback. My invokeFQL function passes a callback function, so I can reuse this function like so:

invokeFQL( selectFriendsQuery, selectFriendsCallback )
invokeFQL( selectAlbumsQuery, selectAlbumsCallback )

The callback function looks something like this:

   private function fqlCallBack( e : FacebookEvent ) : void {
      if( !e.success ){ "Error: " + e.error.reason );
     } else {
        result.text =;

This is just like your HTTPService response function. However, since your data does not conform to a standard GetFriendsData object, I recommend just keeping these as XML and use the e4x functionality available in Flex to handle these requests.

Here is an example of a more elaborate callback function would look like

   [Bindable] private var profilePictures : XMLListCollection;
   [Bindable] private var friends : XMLListCollection;    

   private function fqlCallBack( e : FacebookEvent ) : void {
       if( !e.success ){ "Error: " + e.error.reason );
      } else {
        var results : XML = new XML( );
        // extract profile pictures
	namespace fb = "";
 	use namespace fb;
        profilePictures = new XMLListCollection( results..pic_square );        // extract users
        friends = new XMLListCollection( results..user );

You would then bind these items to as your dataproviders like so:

     <mx:List id="myphotos" dataProvider="{profilePictures}" itemRenderer="PhotoRenderer"/>

To access individual nodes, you would either call the child element directly, like, or use the dataField or labelFunction functionalities within Flex components to retrieve this data. See Adobe’s Using XML data tutorials for more info.

Tips on Learning and Using FQL

  • The Facebook Developer Tools include a console for you to test your FQL commands. Go and select fql.query as your method.
  • The FQL equivalent of each API call is outlined in the API documentation of each call. For example, friends.get‘s FQL call is SELECT uid2 FROM friend WHERE uid1=loggedInUid . Instead of trying to come up with these commands from scratch, build from existing calls and optimize.
  • Don’t skim on the documentation. FQL is very well detailed on the Facebook site, I always go back to both the tables reference and the API calls reference to construct the API calls
  • Again E4X, the XML format available to Flex applications is perfect for these types of unstructured data.

Happy hacking. Remember to visit – Search Your Style.


7 thoughts on “Calling FQL with the Facebook / Flex API

  1. Pingback: links for 2009-09-10 at adam hoyle presents suckmypixel

  2. Pingback: Optimize your facebook applications by utilizing FQL instead of API calls

  3. Nik

    Just found this thanks!
    Got a Flash IDE version?
    I don’t use Flex so I’ll see if I can hack one out of this kind post.
    Unless you have one 😛

  4. Ruben

    Nice tutotial indeed,
    But I’m wondering if there’s some possibilities to use this way to connect to Facebook to make some interactions like “add post in a profile”,”change status to profiles”.
    I did it with GraphApi using Facebook.api(/me) and stuff like this.
    But this need to use a .js file or calling from .as code.The way to connect to facebook, that you wrote in here, is easier than I used. So it’d be so usefull if i can use to create some interaction beside FQL queries.

    Is there some ways to do that?


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s