Skip to content

Latest commit

 

History

History
169 lines (120 loc) · 3.43 KB

File metadata and controls

169 lines (120 loc) · 3.43 KB

Ruby GraphQL Server

Ruby Sinatra GraphQL WEBrick Thin Render

Interactive Ruby GraphQL server with queries and mutations

A simple GraphQL server built with Ruby and Sinatra.
Features queries for users, mutations for sending messages, and GraphiQL UI.


Run Locally on Windows

  1. Install dependencies (skip production gems like thin):
bundle install --without production
  1. Run the server:
ruby app.rb
  1. Open in your browser:

http://localhost:4567/

Note: The server can run with either webrick (development) or thin (production).


Endpoints

Path Method Description
/graphql POST Main GraphQL endpoint for queries and mutations
/ GET GraphiQL UI for interactive queries

GraphQL Queries (Examples)

1. Hello query

{
  hello
}

2. Get all users

{
  users {
    id
    name
    email
  }
}

3. Get a single user by ID

{
  user(id: 2) {
    id
    name
    email
  }
}

GraphQL Mutation (Example)

Send a message

mutation {
  sendMessage(message: "Hello world!") {
    message
    timestamp
  }
}

Curl Requests (Windows / Bash examples)

Query Hello

curl -X POST http://localhost:4567/graphql \
  -H "Content-Type: application/json" \
  -d "{\"query\":\"{ hello }\"}"

Query All Users

curl -X POST http://localhost:4567/graphql \
  -H "Content-Type: application/json" \
  -d "{\"query\":\"{ users { id name email } }\"}"

Query Single User

curl -X POST http://localhost:4567/graphql \
  -H "Content-Type: application/json" \
  -d "{\"query\":\"{ user(id: 2) { id name email } }\"}"

Mutation: Send Message

For non-ASCII text (Cyrillic):

curl -X POST http://localhost:4567/graphql \
  -H "Content-Type: application/json" \
  -d "{\"query\":\"mutation { sendMessage(message: \\\"\\u041F\\u0440\\u0438\\u0432\\u0435\\u0442 \\u043C\\u0438\\u0440\\\") { message timestamp } }\"}"

For ASCII text (English):

curl -X POST http://localhost:4567/graphql \
  -H "Content-Type: application/json" \
  -d $'{"query":"mutation { sendMessage(message: \\"Hello world!\\") { message timestamp } }"}'

Features

  • GraphQL Queries: hello, users, user(id: ID)
  • GraphQL Mutation: sendMessage(message: String)
  • Interactive GraphiQL UI at /
  • Runs on Sinatra with webrick (dev) or thin (prod)
  • Fully deployed on Render.com

Technology Stack

  • Ruby 3.2.9 – dynamic programming language
  • Sinatra 2.2 – lightweight web framework
  • Sinatra-contrib – helpers for JSON, reloader, and more
  • GraphQL 2.5 – GraphQL implementation for Ruby
  • WEBrick – development server (Windows default)
  • Thin – production server
  • Render.com – zero-config deployment

Deploy in 10 seconds

Deploy to Render