Making Queries with JavaScript

If you read the “Retrieving Advanced Data with FQL” section, you should know just about everything you need to know about creating queries in FQL. It's time to write some code. I show you JavaScript here, but your PHP, Python, Ruby, or C# library should look somewhat similar to this. In JavaScript, you can do this with FB.api() and specify fql.query as the method value and then a query. The following code shows you all you need to do to make the same query I list previously in JavaScript:

FB.api(
{
   method: 'fql.query',
   query: 'SELECT uid, name, pic_square FROM user WHERE uid = me() OR uid IN
               (SELECT uid2 FROM friend WHERE uid1 = me())'
},
function(response) {
   for (var i = 0; i < response.length; i++) {
    console.debug(response[i]);
   }
});

This example uses the method variable because it's an old-style REST API (at the time of this writing, Facebook had not yet converted FQL to Graph API). Specifying the query I use in this example returns a response variable to the function callback at the end. If you loop through the response variable, you can then do things with each element, each element being the logged-in user and her friends. Try it and see what happens!

Combining data sets

The more data you work with, and the more popular your application gets, the more likely you'll want to find ways to streamline the number of API calls you make back to Facebook. Not only does this reduce the time it takes to return data to the user (more calls means longer ...

Get Facebook® Application Development For Dummies® now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.