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": "[email protected]",
                    "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

BransonGitomeh commented Nov 30, 2016

Authentification

  • username failure
{
  "data": {
    "auth": {
      "hash": null,
      "message": null,
      "errors": [
        {
          "key": "username",
          "message": "Username not found"
        }
      ]
    }
  }
}
  • password failure
{
  "data": {
    "auth": {
      "hash": null,
      "message": null,
      "errors": [
        {
          "key": "password",
          "message": "Invalid password"
        }
      ]
    }
  }
}
  • success login
{
  "data": {
    "auth": {
      "hash": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InRlc3RAc2FiZWsuY28ua2UiLCJpZCI6Ijo6ZmZmZjoxMjcuMC4wLjEiLCJpYXQiOjE0ODA1MTM4MTN9.Kvt7CT5pccsZVL9GqPz7u8A6TZud8rxi2P-85t80rlI",
      "message": "keep this safe!",
      "errors": null
    }
  }
}

@BransonGitomeh
Copy link
Author

BransonGitomeh commented Nov 30, 2016

Authorisation and Data request

  • 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
    }
  }
}
  • variables(id of your organisations account)
{
  "id":"057cc8b0-a95f-11e6-bc3e-371d1cf1f0d6"
}

responses

  • invalid token / token doesn't authorize you to access that org's data, ie...thats not your token 😱
{
  "data": {
    "organisation": {
      "name": null,
      "id": "4218e9e0-b704-11e6-969a-aecfaa4a02b2",
      "location": null,
      "admins": [],
      "contacts": [],
      "groups": [],
      "errors": [
        {
          "key": "auth",
          "message": "authorisation failure"
        }
      ]
    }
  }
}
  • valid token, success 😎
{
  "data": {
    "organisation": {
      "name": "test organisation",
      "id": "057cc8b0-a95f-11e6-bc3e-371d1cf1f0d6",
      "location": "runda",
      "admins": [
        {
          "email": "[email protected]",
          "contact": "0711657108"
        }
      ],
      "contacts": [
        {
          "id": "4e186600-b356-11e6-9e2a-b2d12d3d3107",
          "name": "steve",
          "number": "076567657876"
        },
        {
          "id": "e6b7f970-a960-11e6-8113-75861d48b67b",
          "name": "test",
          "number": "0718657178"
        }
      ],
      "groups": [
        {
          "id": "219bca80-b35c-11e6-93ac-81d3951d072a",
          "name": "new test group",
          "contacts": [
            {
              "id": "219c3fb0-b35c-11e6-a844-1062073c942b",
              "name": "steve",
              "number": "076567657876"
            },
            {
              "id": "219c66c0-b35c-11e6-97e6-a2742e520d4b",
              "name": "test",
              "number": "0711057103"
            }
          ]
        },
        {
          "id": "62df03b0-b35a-11e6-8602-1ddf475788ca",
          "name": "test",
          "contacts": []
        }
      ],
      "errors": null
    }
  }
}

@BransonGitomeh
Copy link
Author

BransonGitomeh commented Nov 30, 2016

Querying specific data

You manipulate the query string to reduce of increase the number of things you want to be returned to you from the request,

query finder($id:String!){
              organisation(id:$id){
                name,
                admins{
                  email,
                  contact
                }
              }
            }

The above query will only get you the name, and an array of admins that are in the organisation. that way you can optimise the network request to fit to only what you need in the context of your application.

@GochoMugo
Copy link

What necessitates/inspires the structure of the queries? Where is it from?

@BransonGitomeh
Copy link
Author

hey @GochoMugo sorry for the late responce, if you have an account in sabek.co.ke all the data that you have access to on the admin interface is available to you via this api, everything you can see and every action that is possible including sensing sms.

this means that if you are interested in making another application for your customers whether it be an android app or anything else, you dont have to build a backend, you can use the information that exposed on this api and store your data here too. for free and you get to resell as you please rather than having to mentain your own backend

@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