A simple bare-bones Rails app that was demoed at the 2018-03-27 Ann Arbor Ruby meetup for the purposes of getting started with the GraphQL Ruby gem.
Going commit-by-commit demonstrates how to install dependencies and scaffold
the GraphQL API for your own app. To run this app, having Docker is the easiest
route (e.g., Docker for Mac). With
docker-compose
you can get the app running via:
git clone https://github.com/andrewsardone/intro-graphql-talk-ruby
cd intro-graphql-talk-ruby
docker-compose build web
docker-compose run web bin/setup
docker-compose up
Visit the GraphiQL API explorer at https://localhost:3000/graphiql.
Inspired by How to GraphQL's Ruby tutorial, the app is intended to be a Pinboard-style bookmark manager. There isn't any browser user interface, but solely an exposed GraphQL API that's explorable via GraphiQL:
The model layer consists of bookmarks and tags:
shell> bin/rails console
pry(main)> show-models
ApplicationRecord
Table doesn't exist
Bookmark
id: integer
name: string
url: string
created_at: datetime
updated_at: datetime
has_many :taggings
has_many :tags (through :taggings)
Tag
id: integer
name: string
created_at: datetime
updated_at: datetime
has_many :bookmarks (through :taggings)
has_many :taggings
Tagging
id: integer
bookmark_id: integer
tag_id: integer
created_at: datetime
updated_at: datetime
belongs_to :bookmark
belongs_to :tag
And our GraphQL API exposes those types:
# A pinnable bookmark, i.e., a named URL
type Bookmark {
id: ID!
name: String!
tags: [Tag]
url: String!
}
# Autogenerated input type of CreateBookmark
input CreateBookmarkInput {
# A unique identifier for the client performing the mutation.
clientMutationId: String
name: String!
url: String!
tags: [String!]
}
# Autogenerated return type of CreateBookmark
type CreateBookmarkPayload {
bookmark: Bookmark
# A unique identifier for the client performing the mutation.
clientMutationId: String
}
type Mutation {
createBookmark(input: CreateBookmarkInput!): CreateBookmarkPayload
# An example field added by the generator
testField: String
}
type Query {
# A root collection of all Bookmarks
bookmarks: [Bookmark]!
# An example field added by the generator
testField: String
}
type Tag {
bookmarks: [Bookmark]
id: ID!
name: String!
}