Online Book Reader

Home Category

Facebook Cookbook - Jay Goldman [112]

By Root 676 0
in one late November night and has grown by leaps and bounds as people have asked for new things, such as snap-backs (i.e., objects returning to their original position when you drop them somewhere they shouldn’t go). Peter has received a few donations to help the cause and may be interested in co-contributors, so drop by his blog if you’d like to be part of the effort.

Chapter 8. Facebook Query Language (FQL)


Let’s say that you’d been puttering around town in a beat-up old jalopy and you pulled up to a traffic light. You dig your jalopy in a very Archie kind of way, even though it’s a little slow sometimes and uses a lot of gas, but it’s your car and it’s all you’ve known. And now let’s say, while you’re sitting there at the light, a brand new Formula 1 race car comes screaming up to a stop right next to you, tires smoking. Sure, your jalopy is nice and all, but think how much faster you could get your shopping done in that baby! Now think of your jalopy as the Facebook API and the sleek, sexy, speedy race car as FQL.

See, here’s the dirty little secret about APIs: they’re all the rage with the kids these days, and they’re a joy to program in, but they’re not particularly efficient when you’re worried about your app scaling to hundreds of thousands of users. If you’re building a Facebook app that you think will really take off and you want to make sure you’re being as future-proof as possible, you should consider using FQL for key queries for the following reasons:

Decreased result sets

When you’re working with the API and you request the friends of a specific user, you get all the info back about everybody and then parse through the results. FQL gives you the ability to limit the fields you want returned and to put more conditions on your queries, thereby reducing the size of the result set and the bandwidth consumed. If you change a key query from the API to FQL and save yourself 10k per request, and you get 100 requests per minute, you’re saving 1 MB of transfer, which is about 1.4 GB per day.

Reduced requests

Some of the API calls require you to make a request, get back the results, parse through them, and then make more requests based on your findings (e.g., to get the names of user’s friends, you get the list of their IDs and then call users.getInfo on them). You can often reduce that down to a single FQL query that uses a subquery to get the list, saving yourself a full roundtrip from the server.

If you already know SQL, FQL is like a natural extension in the same way that FBML naturally proceeds from HTML. If you’re not comfortable with SQL, I recommend picking up a book such as Head First SQL by Lynn Beighley, Learning MySQL by Seyed M.M. “Saied” Tahaghoghi and Hugh E. Williams, or MySQL Cookbook by Paul DuBois. You’ll find all of them, along with everything else O’Reilly publishes, on http://safari.oreilly.com.

We’ll start off with a quick overview of the basics (and particularly how FQL is different than SQL), and then dive right into some practical recipes. This isn’t intended to give you every possible FQL query you might need, but rather to give you a whole bunch of different tools that you can combine to build your own queries. If you’re looking for a specific query for one table and can’t find it, check the other recipes for similar situations.

Playing with FQL


Problem


I’d like to try out some of the FQL queries listed in this chapter, but I don’t want to have to write a whole test page for it.

Solution


Facebook makes three great testing consoles available to developers, which you can find at http://developers.facebook.com/tools.php. There isn’t one specifically for FQL, but if you look in the API Test Console’s Method drop-down (http://developers.facebook.com/tools.php?api), you’ll find an “fql.query” option, which will reveal a query field.

Discussion


Another cool thing you can do in the test console is play with the Response Format drop-down, which lets you toggle between getting your responses in XML, JSON, or Facebook PHP Client. You can also switch between the

Return Main Page Previous Page Next Page

®Online Book Reader