Coder Social home page Coder Social logo

littleb's Introduction

Littleb

Requirements

The app is built with Ruby 2.3.7 and Rails 5.2.1. It uses Postgres and GraphQL.

Note that there is a presumption that postgres is running on your machine and is accessible to your system user without password (see config/database.yml otherwise).

Set Up

$ cd littleb
$ bundle install
$ rake db:create
$ rake db:migrate
$ rake db:seed # will populate the default 'bits' list
$ rails s

The application is then available via GraphiQL interface at http://localhost:3000/graphiql

The app is also deployed on Heroku, but GraphiQL is not available there (see curl references in query notes below).

Run Tests

$ rails test

General Notes

I have chosen to implement this API exercise using GraphQL.

As a result I didn't get as far as I would have liked. In particular, bits and materials are passed as lists of names (comma-separated string) because I couldn't get an Array type working as expected.

The following functions have been implemented:

  • query: bits
  • query: materials
  • query: users
  • query: inventions
  • mutation: createMaterial (name[string])
  • mutation: createUser (username[string], email[string])
  • mutation: createInvention (title[string], description[string], list of bits and materials as comma-separated string, and user id)
  • mutation: updateInvention (see above)
  • mutation: deleteInvention (id[int])

Presently, the queries return all objects of the given type (no filtering).

Examples

Note that GraphiQL will work locally, but not on Heroku, so I've included the curl commands to use with Heroku.

All of these are just examples. You can construct the queries and mutations as you like, given the confines of the GraphQL types.

Get a list of bits:

query{
  bits{
    id
    name
  }
}
$ curl 'https://young-depths-78204.herokuapp.com/graphql' -H 'Content-Type: application/json'  -d '{"query":"{  bits{  id name } }"}'

Get a list of materials:

query{
  materials{
    id
    name
  }
}
$ curl 'https://young-depths-78204.herokuapp.com/graphql' -H 'Content-Type: application/json'  -d '{"query":"{  materials{  id name } }"}'

Add a material:

mutation{
  createMaterial(name: "stuff-and-stuff"){
    id
    name
  }
}
$ curl 'https://young-depths-78204.herokuapp.com/graphql' -H 'Content-Type: application/json'  -d '{"query":"mutation{ createMaterial(name: \"scissors\"){  id name } }"}'

Add a user:

mutation{
  createUser(username: "foobar", email: "[email protected]"){
    id
    username
    email
  }
}
$ curl 'https://young-depths-78204.herokuapp.com/graphql' -H 'Content-Type: application/json'  -d '{"query":"mutation{ createUser(username: \"edward\", email: \"[email protected]\"){  id username email } }"}'

Get users:

query{
  users{
    id
    username
    email
  }
}
$ curl 'https://young-depths-78204.herokuapp.com/graphql' -H 'Content-Type: application/json'  -d '{"query":"{  users{  id username email } }"}'

Add an invention (requires at least one bit):

mutation{
  createInvention(
    title: "Whacky One",
    description: "Really whacky thing that I made!",
    bits_list: "bargraph, timeout",
    materials_list: "stuff-and-stuff",
    user_id: 1,
    ){
    id
    title
    description
    bits{
      name
    }
    materials{
      name
    }
    user{
      username
    }
  }
}
$ curl 'https://young-depths-78204.herokuapp.com/graphql' -H 'Content-Type: application/json'  -d '{"query":"mutation{ createInvention(title: \"My Great Invention\", description: \"This is my greatest invention!\", bits_list: \"bargraph, timeout\"){  id title description bits{ name } } }"}'

Get inventions (note users can list inventions per user):

query{
  inventions{
    id
    title
    description
    bits{
      name
    }
    materials{
      name
    }
    user{
      username
      inventions{
        title
      }
    }
  }
}
$ curl 'https://young-depths-78204.herokuapp.com/graphql' -H 'Content-Type: application/json'  -d '{"query":"{  inventions{ id title description user{ username } materials{ name } bits{  name } } }"}'

Update an invention:

mutation{
  updateInvention(
    id: 1,
    title: "Updated Title",
    description: "Updated Description",
    bits_list: "timeout",
    materials_list: "stuff-and-stuff",
    user_id: 1,
    ){
    id
    title
    description
    bits{
      name
    }
    materials{
      name
    }
    user{
      username
    }
  }
}
$ curl 'https://young-depths-78204.herokuapp.com/graphql' -H 'Content-Type: application/json'  -d '{"query":"mutation{ updateInvention(id: 1, title: \"My New Title\", description: \"My new description\", bits_list: \"timeout\"){  id title description bits{ name } } }"}'

Delete an invention:

mutation{
  deleteInvention(
    id: 1,
  ){
    id
    title
  }
}
$ curl 'https://young-depths-78204.herokuapp.com/graphql' -H 'Content-Type: application/json'  -d '{"query":"mutation{ deleteInvention(id: 1){  id title description bits{ name } } }"}'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.