Aristat / rails-graphql-example-app

Rails Graphql API example

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Rails Graphql API

Getting started

Install rvm/rbenv, ruby, rails

Clone repository

git clone git@github.com:Aristat/rails-graphql-example-app.git

Start

bin/rails db:create
bin/rails db:migrate
bin/rails db:seed

bundle exec rails s -p 3000

localhost:3000/graphql

Code structure

.
├── fixtures                            // fixtures for specs
├── db                                  // migrations and seeds for DB
└── app
    ├── graphql                         // graphql logic
    └── controllers/graphql_controller  // graphql action with jwt auth

Generate token for auth

bin/rails c
JWTWrapper.generate_user_token(User.last)

this token set in headers -

{
  "Authorization": "bearer example_token_from_console"
}

Queries/Mutations

query userProfile {
  userProfile {
    id
    name
  }
}
mutation updateUser($userInput: UserInput) {
  updateUser(userInput: $userInput) {
    ok
    errors {
      message
      type
    }
  }
}
query productBlock($productId: ID!) {
  product(id: $productId) {
    name
    typeObject
    itemsCount
    productItems {
      nodes {
        color
        price
      }
    }
  }
}

Testing

RAILS_ENV=test rspec spec/

Programs for testing

https://github.com/prisma/graphql-playground - for easy query writing

About

Rails Graphql API example


Languages

Language:Ruby 86.8%Language:HTML 9.9%Language:JavaScript 2.1%Language:CSS 1.2%Language:Shell 0.0%