Falcor

A JavaScript library for efficient data fetching

README


Falcor

Build Status Coverage Status

2.0


2.0 is the current stable Falcor release. 0.x and 1.x users are
welcome to upgrade.

-   [Breaking changes between 1.x and 2.0](https://github.com/Netflix/falcor/blob/master/MIGRATIONS.md).
-   [Breaking changes between 0.x and 1.x](https://github.com/Netflix/falcor/blob/1.0.0/MIGRATIONS.md).

Roadmap


Issues we're tracking as part of our roadmap are tagged with the
label. They are split into
and
categories, with near, medium and longer term labels to convey a broader sense
of the order in which we plan to approach them.

Getting Started


You can check out a working example server for Netflix-like application right now. Alternately, you
can go through this barebones tutorial in which we use the Falcor Router to
create a Virtual JSON resource. In this tutorial we will use Falcor's express
middleware to serve the Virtual JSON resource on an application server at the
URL /model.json. We will also host a static web page on the same server which
retrieves data from the Virtual JSON resource.

Creating a Virtual JSON Resource


In this example we will use the falcor Router to build a Virtual JSON resource
on an app server and host it at /model.json. The JSON resource will contain
the following contents:

  1. ``` js
  2. {
  3.   "greeting": "Hello World"
  4. }
  5. ```

Normally, Routers retrieve the data for their Virtual JSON resource from backend
datastores or other web services on-demand. However, in this simple tutorial, the
Router will simply return static data for a single key.

First we create a folder for our application server.

  1. ``` sh
  2. $ mkdir falcor-app-server
  3. $ cd falcor-app-server
  4. $ npm init
  5. ```

Now we install the falcor Router.

  1. ``` sh
  2. $ npm install falcor-router --save
  3. ```

Then install express and falcor-express. Support for restify is also available,
as is support for hapi via a [third-party
implementation](https://github.com/Netflix/falcor-hapi).

  1. ``` sh
  2. $ npm install express --save
  3. $ npm install falcor-express --save
  4. ```

Now we create an index.js file with the following contents:

  1. ``` js
  2. // index.js
  3. const falcorExpress = require("falcor-express");
  4. const Router = require("falcor-router");

  5. const express = require("express");
  6. const app = express();

  7. app.use(
  8.     "/model.json",
  9.     falcorExpress.dataSourceRoute(function (req, res) {
  10.         // create a Virtual JSON resource with single key ('greeting')
  11.         return new Router([
  12.             {
  13.                 // match a request for the key 'greeting'
  14.                 route: "greeting",
  15.                 // respond with a PathValue with the value of 'Hello World.'
  16.                 get: () => ({ path: ["greeting"], value: "Hello World" }),
  17.             },
  18.         ]);
  19.     })
  20. );

  21. // serve static files from current directory
  22. app.use(express.static(__dirname + "/"));

  23. app.listen(3000);
  24. ```

Now we run the server, which will listen on port 3000 for requests for
/model.json.

  1. ``` sh
  2. $ node index.js
  3. ```

Retrieving Data from the Virtual JSON resource


Now that we've built a simple virtual JSON document with a single read-only key
greeting, we will create a test web page and retrieve this key from the
server.

Create an index.html file with the following contents:

  1. ``` html
  2. <html>
  3.     <head>
  4.         
  5.         <script src="https://netflix.github.io/falcor/build/falcor.browser.js"></script>
  6.         
  7.         
  8.         <script>
  9.             var model = falcor({
  10.                 source: new falcor.HttpDataSource("/model.json"),
  11.             });
  12.             // retrieve the "greeting" key from the root of the Virtual JSON resource
  13.             model.get("greeting").then(function (response) {
  14.                 document.write(response.json.greeting);
  15.             });
  16.         </script>
  17.     </head>
  18.     <body></body>
  19. </html>
  20. ```

Now visit http://localhost:3000/index.html and you should see the message
retrieved from the server:

  1. ```
  2. Hello World
  3. ```

Steps to publish new version


-   Make pull request with feature/bug fix and tests
-   Merge pull request into master after code review and passing Travis CI checks
-   Run git checkout master to open master branch locally
-   Run git pull to merge latest code, including built dist/ and docs/ by Travis
-   Run npm run dist to build dist/ locally
    -   Ensure the built files are not different from those built by Travis CI, hence creating no change to commit
-   Update CHANGELOG with features/bug fixes to be released in the new version and commit
-   Run npm version patch (or minor, major, etc) to create a new git commit and tag
-   Run git push origin master && git push --tags to push code and tags to github
-   Run npm publish to publish the latest version to NPM

Additional Resources


-   For detailed high-level documentation explaining the Model, the Router, and JSON
    Graph check out the Falcor website.


-   For a working example of a Router, check out the

-   For questions and discussion, use [Stack
    Overflow](https://stackoverflow.com/questions/tagged/falcor).