npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fqb

v2.1.1

Published

Facebook Graph API query builder for JavaScript

Downloads

75

Readme

fqb

NPM version Build Status Coverage Status

Facebook Graph API query builder for JavaScript

A query builder that helps you simply make nested requests to Facebook's Graph API for fetching specific data that you want.

Introduction

About Facebook's Graph API, there are three concepts you should know:

  1. Node: A node represents a "real-world thing" on Facebook, such as a user, a photo, a page, a comment.
  2. Edge: An edge is the connection between two or more nodes, such as a page's photos, or a photo's comments.
  3. Field: A Field is a property of the node. such as a user's birthday, or the name of a page.

We follow the same concepts to help you generate request URLs. For example, when you send a request to the Facebook's Graph API, the URL looks like as following:

https://graph.facebook.com/node-id/edge-name?fields=field-name

To generate the same URL with fqb, you'd do the following:

const edge = new FQB().edge('edge-name').fields('field-name')
const fqb = new FQB().node('node-id').fields(edge)

console.log(fqb.asUrl())
// https://graph.facebook.com/node-id?fields=edge-name{field-name}

The output looks a little different, but two URL's are functionally identical with the exception of how the Graph API returns the response data. What makes the URL generated with fqb different is that it is being expressed as a nested request.

Making nested request allows you to effectively nest multiple graph queries into a single call. With fqb, you can make it easy to generate properly formatted nested requests from a fluent, easy-to-read JavaScript interface.

Install

$ npm install --save fqb

Browser

Add a <script> to your index.html:

<script src="/node_modules/fqb/dist/fqb.min.js"></script>

Node.js / Webpack

Import the module to your *.js file:

const FQB = require('fqb')

Usage

A basic example

Below is a basic example that gets the logged in user's id & email (assuming the user granted your app the email permission).

const fqb = new FQB()
  .node('me')
  .fields(['id', 'email'])
  .accessToken('user-access-token')
  .graphVersion('v5.0')

console.log(fqb.asEndpoint())
// /v5.0/me?access_token=user-access-token&fields=id,email

console.log(fqb.asUrl())
// https://graph.facebook.com/v5.0/me?access_token=user-access-token&fields=id,email

Get data across multiple edges

The following example will get the logged in user's name & first 5 photos they are tagged in with just one call to Graph.

const photosEdge = new FQB()
  .edge('photos')
  .fields(['id', 'source'])
  .limit(5)

const fqb = new FQB()
  .node('me')
  .fields(['name', photosEdge])

console.log(fqb.asEndpoint())
// /me?fields=name,photos.limit(5){id,source}

console.log(fqb.asUrl())
// https://graph.facebook.com/me?fields=name,photos.limit(5){id,source}

The following example will get user 1234's name, and first 10 photos they are tagged in. For each photo it gets the first 2 comments and all the likes.

const likesEdge = new FQB()
  .edge('likes')

const commentsEdge = new FQB()
  .edge('comments')
  .fields('message')
  .limit(2)

const photosEdge = new FQB()
  .edge('photos')
  .fields(['id', 'source', commentsEdge, likesEdge])
  .limit(10)

const fqb = new FQB()
  .node('1234')
  .fields(['name', photosEdge])

console.log(fqb.asEndpoint())
// /1234?fields=name,photos.limit(10){id,source,comments.limit(2){message},likes}

console.log(fqb.asUrl())
// https://graph.facebook.com/1234?fields=name,photos.limit(10){id,source,comments.limit(2){message},likes}

Use other methods of Graph API

You can use modifiers to use some methods of Graph API. The methods are such as sort (modifiers({ sort: 'name' })), filtering (The following example), summary (modifiers({ summary: JSON.stringify(['clicks', 'likes']) })) ...

The following example will get photos filtering by ids.

const filterings = [{ field: 'id', operator: 'IN', value: ['1234', '5678'] }]
const modifiers = { filtering: JSON.stringify(filterings) }
const photosEdge = new FQB()
  .edge('photos')
  .fields(['id', 'source'])
  .limit(5)
  .modifiers(modifiers)

const fqb = new FQB()
  .node('me')
  .fields(['name', photosEdge])

console.log(fqb.asEndpoint())
// /me?fields=name,photos.limit(5).filtering([{"field":"id","opertator":"IN","value":["1234","5678"]}]){id,source}

console.log(fqb.asUrl())
// https://graph.facebook.com/me?fields=name,photos.limit(5).filtering([{"field":"id","opertator":"IN","value":["1234","5678"]}]){id,source}

Note

Inspired by FacebookQueryBuilder

Reference

Facebook's Graph API

License

MIT © Chun-Kai Wang