The Facebook API craziness

The Facebook platform is the cornerstone of SocialCaddy. As most developers we tend to nag about its APIs and especially the sparse documenation. This time it was no exception. But what has happened?

A few weeks ago ago Facebook announced the Graph. Basically the whole architecture of Facebook is now based on the Graph. Whatever you like on Facebook or beyond it is a node on the Graph and you connect to it. The Open Graph protocol bring the Semantic Web aka Web 3 to our browsers today.

Besides the Graph API Facebook introduced a new permissions policy, new authentication methods (OAuth and a new JS SDK) and it dropped support for some older functionality but without actually announcing it.

First of all we found that you have to upgrade your permissions and if you use Facebook Connect you have to migrate to the new dialog. Go to the Developer app -> Edit Settings -> Migrations -> New Data Permissions.

That was the first step! Remember to study the new permissions.

Now let 's get to the point. Before the Graph we used to select our users' friends work history, education history, activities, etc in a single FQL call like this:

SELECT work_history, education_history, activities, books, movies FROM user WHERE uid IN(some_ids)

Then we updates some of those fields... and... there were no updates! We thought it was some kind of caching but the relationship_status for example was updated! 

Then we decided to give a look at the FQL User table and a lot of the fields were missing but the FQL query was executed but fetching the old values! We could fetch the work_history and education_history doing a simple Graph API call ex.

http://graph.facebook.com/me (or some uid)

That call fetched the missing fields. In the doc page we read that some fields were missing for example relationship_status but it was there in the JSON we got from the Graph API call.

OK, that worked but how could we get all our users' friends work histories in a single call? It seemed we had to query the Graph n times where n is the number of a user 's friends count. That was terrible! Our FQL did not fetch the data and the docs had no sign of the fields we used to ask for. We were desperate but after some careful reading behind the lines our saw that: 

any of its fields which are also fields in the corresponding Graph API version

What does this mean? It means that our old FQL query is still valid (partially) in this format:

SELECT work, education FROM user WHERE uid IN(some_ids)

That made our day! The fields were not lost and in a single request we got all the data we wanted! But what about the books, activities, movies and so on? Well these data is no longer a text field in the user 's profile. They are now connections on the graph so whatever goes there is actually a page you like! Do you want to get all of those in FQL? Just do this

SELECT name,type FROM page WHERE page_id IN (SELECT target_id  FROM connection WHERE source_id=657809015 and target_type="Page")

This will return all the pages your user likes and of course you can filter by type such as MUSICIAN, LOCAL_BUSINESS and so on. The Graph version is much simpler but FQL gives you the power for batch select, advanced filtering and so on.

In conclusion do not think of the Graph API as something different to FQL. Basically they will bring you the same data in different format and using a different way to ask for them. FQL is indeed, as Facebook says, advanced API but it gives you the power to scale more easily and filter your data.

Do you have any similar experiences with the Facebook APIs? Please share your story with us.

Panos