Skip to content

Instantly share code, notes, and snippets.

@BransonGitomeh
Last active December 11, 2016 20:44
Show Gist options
  • Save BransonGitomeh/9d8adebd8036572aec7aabc11d0ffeca to your computer and use it in GitHub Desktop.
Save BransonGitomeh/9d8adebd8036572aec7aabc11d0ffeca to your computer and use it in GitHub Desktop.

Sabek.Graph Api

The Graph API demonstrated will provide you with all the data that is also available via the admin console at sabek.co.ke

The following code snippet illustrates how to use sabek's graph api in your application, to login to your account and get an authorisation token and log in and request for your data

const async = require("async")
const request = require('request');
var token;
const assert = require("assert")

async.series([
    function getAuthKeys(next) {
        var query =
            `query getToken($username:String!,$password:String!){
              auth(username:$username, password:$password){
                hash,
                message
              }
            }`;

        request.post({
            url: 'http://localhost:9000',
            form: {
                query: query,
                variables: JSON.stringify({
                    "username": "test@sabek.co.ke",
                    "password": "testdd"
                })
            }
        }, function(err, response, body) {
            assert.ifError(err)
            if (!err && response.statusCode == 200) {
                // contains the token
                var response = JSON.parse(body)
                console.log(response.data.auth.hash)
                token = response.data.auth.hash
            }
            next()
        })
    },
    function getData(next) {
        var query =
            `query finder($id:String!){
              organisation(id:$id){
                name,
                id,
                location,
                admins{
                  email,
                  contact
                },
                contacts{
                  id,
                  name,
                  number
                },
                groups{
                  id,
                  name,
                  contacts{
                    id,
                    name,
                    number
                  }
                },
                errors{
                  key,
                  message
                }
              }
            }`;

        request.post({
            url: 'http://localhost:9000',
            form: {
                token: token,
                query: query,
                variables: JSON.stringify({
                    "id": "057cc8b0-a95f-11e6-bc3e-371d1cf1f0d6"
                })
            }
        }, function(err, response, body) {
            if (!err && response.statusCode == 200) {
                console.log(body)
            }
            next()
        })
    }
], (err) => {
    console.log("complete")
})
@BransonGitomeh
Copy link
Author

also, what inspires the structure the queries is the data you need as the person who is querying, so if in a certain section of the app you need a certain set of data, you query only for that, this can be explained more on facebooks graphql.org site, and its main focus is to give the power to query to the client, ie if you want to implement preloading future pages with data. even before your user triggers it, you can fetch all the related data in a single request, so when the user gets to that section of your application that needs that data he will not need to wait for a network request

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment