Skip to content

Latest commit

 

History

13 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Graph API

This is a simple api that you can use to store data and get it based on id or retrieve them all for rendering a graph bar.

The sample structure is in data.json in root directory and here you may see the result.

Table of Contents

Built with

For development purposes I didn't use any framework but only two 3rd party dependencies.

I attempted to develop a micro framework with common functionality such as routing, template rendering also features like login and registration.

My goal is to simulate how a modern day framework works and only then develop the project top of the core application.

Development Environment

Third-Party Dependencies

Getting Started

These instructions will guide you to up and running the project on your local machine.

Prerequisite

  • Composer
  • Apache(or Nginx)
  • MySQL

Installation

In root directory you will find graph.sql. First import it to MySQL database manually.

Then update parameters in app/config/config.yml.dist and save this file as config.yml.

dataSource:
    database:
        host: localhost
        name:
        user:
        password:

graphQL:
    endpoint: http://localhost:8000/graph

Now you can run composer install to install dependencies.

Running

You can start the server with composer run-script serve. Below you may find route list.

Method Path Info
GET /graph bar graph data
GET /graphql explorer
POST /graphql endpoint

GraphQL Examples

REST is a well-known specification but APIs based on it have some disadvantages when it comes to resolve multiple requests.

GraphQL on the other hand brings a new way that allows you to get exactly the data you’re looking for in one request.

Following examples show how queries and mutations can be ran via explorer or from command line.

Basic Query

This query is asking for all fields on tick object.

explorer:

Scheme

{
  tick(id: 1) {
    id
    title
    name
    data
    borderColor
    borderColorOpacity
    backgroundColor
    backgroundColorOpacity
    borderWidth
  }
}

cmd:

$ curl -X POST \
-H "Content-Type: application/json" \
-d '{ "query": "{ tick(id: 1) { id title name data borderColor borderColorOpacity backgroundColor backgroundColorOpacity borderWidth } }" }' \
http://localhost/graphql

Response:

{
  "data": {
    "tick": {
      "id": "1",
      "title": "votes",
      "name": "red",
      "data": 12,
      "borderColor": "#ff6384",
      "borderColorOpacity": "1",
      "backgroundColor": "#ff6384",
      "backgroundColorOpacity": "0.2",
      "borderWidth": 1
    }
  }
}

Mutation

addTick is asking to create a new tick object. Result will be the id.

explorer:

Scheme

mutation AddTick(
    $title: String!,
    $name: String!,
    $data: Int!,
    $borderColor: String!,
    $borderColorOpacity: String!,
    $backgroundColor: String!,
    $backgroundColorOpacity: String!,
    $borderWidth: Int!
) {
  addTick(
    title: $title,
    name: $name,
    data: $data,
    borderColor: $borderColor,
    borderColorOpacity: $borderColorOpacity,
    backgroundColor: $backgroundColor,
    backgroundColorOpacity: $backgroundColorOpacity,
    borderWidth: $borderWidth
  )
}

variables:

{
  "title": "attendance",
  "name": "orange",
  "data": 18,
  "borderColor": "#fffaa4",
  "borderColorOpacity": "0.2",
  "backgroundColor": "#ff9f40",
  "backgroundColorOpacity": "1",
  "borderWidth": 1
}

cmd:

$ curl -X POST http://localhost/graphql \
  -H "Content-Type: application/json" \
  -d '{ "query": "mutation AddTick($title: String!, $name: String!, $data: Int!, $borderColor: String!, $borderColorOpacity: String!, $backgroundColor: String!, $backgroundColorOpacity: String!, $borderWidth: Int!) { addTick(title: $title, name: $name, data: $data, borderColor: $borderColor, borderColorOpacity: $borderColorOpacity, backgroundColor: $backgroundColor, backgroundColorOpacity: $backgroundColorOpacity, borderWidth: $borderWidth) }", 
  "variables": { "title": "attendance", "name": "orange", "data": 18, "borderColor": "#fffaa4", "borderColorOpacity": "0.2", "backgroundColor": "#ff9f40", "backgroundColorOpacity": "1", "borderWidth": 1 } }'

Response:

{
  "data": {
    "addTick": 12
  }
}

Resources

About

A simple API for storing and retrieving data to render graphical representations

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages