Skip to content

moseslucas/api-rails-graphql

Repository files navigation

api-rails-graphql

This is a boilerplate for backend using: rails api only & graphql.

  • Ruby Version: 2.4.2
  • Database: Postgresql

Product Dependencies

 gem 'rails', '~> 5.1.4'
 gem 'pg', '~> 0.18'
 gem 'puma', '~> 3.7'
 gem 'graphql', '~> 1.7.7
 
 # in order to make graphiQL work, these two gems are required
 gem 'sass-rails'
 gem 'uglifier'
 
 # add graphiQL to development
 gem 'graphiql-rails', '~> 1.4.8'

Installing

  1. Clone this repository: git clone git@github.com:moseslucas/api-rails-graphql.git If you're a non-git user download this as zip file

  2. Go to app directory and run: bundle install && rails db:setup

  3. Start the server: rails s

  4. Open browser and navigate to localhost:3000/graphiql

Trying out graphiQL with sample model: Movie

  1. At localhost:3000/graphiql: localhost:3000/graphiql

  2. At the right side of graphiQL there's a Documentation Explorer for all our APIs.

    Check the query: allMovies.

    Its type isMovie and have the following properties:

    id: ID,
    title: String,
    price: Float
    

    allMovies

  3. Make a query call to fetch the allMovies API.

    (In graphql there are Mutations(think of it as POST) & Queries(think of it as GET)

    Make a query call:

    query {
     allMovies
    }
    

    we could also set the properties we want to fetch:

    query {
     allMovies{
    	id, title, price
     }
    }
    

    we could also rename the propery name like javascript objects

    query {
     allMovies{
    	id,
    	title,
    	amount: price
     }
    }
    

    query-allMovies

  4. Make a mutation call to post/create a record to the Movie model using the API createMovie

    (In graphql there are Mutations(think of it as POST) & Queries(think of it as GET)

    Make a mutation call:

    	mutation {
    		createMovie(
    			title: "Star Wars",
    			price: 500
    		)
    	}
    

    we could also set what properties to return after the record has been created:

    	mutation {
    		createMovie(
    			title: "Star Wars",
    			price: 500
    		) { 
    			id,
    			title,
    			amount: price
    		}
    	}
    

    mutation-createMovie

Authors

  • Moses Lucas

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE.md file for details

About

Backend Rails API using Graphql

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published