This is based on the the homework assignment from the Udemy Basics of GraphQL with Ruby (second and third assignments) in the "Basics of GraphQL in Ruby on Rails" course on Udemy.
It's been modified in the following ways:
- RVM style gemset added:
graphql_blog
. - rspec-rails, factory-bot-rails, faker and awesome_print gems installed.
- Model specs added for User, Comment, Post and Session.
- New classes created with some filler specs to get you started.
Fork this repo to your personal github and then clone that to your local environment.
- Click on the "Fork" link on this repo's page (top right) in Github.
- Click "Clone or Download" green button and copy the content in the box there.
- In terminal go to your workspace and clone it via
$ git clone [email protected]:YOUR_GITHUB_HANDLE/graphql_rspecs.git
. - Using terminal enter the directory locally:
$ cd graphql_rspec_starter
. - If it prompts you to install a new Ruby version follow the directions.
- Install the bundler gem into your empty gemset:
$ gem install bundler
. - Run bunlder to install current gems into gemset:
$ bundle
- Instantiate the database:
$ rails db:create
- Run current migrations:
$ rails db:migrate
At this point you should be able to run tests by doing $ rspec
and they should all pass or show as pending.
Please read the GraphQL's Testing Document. How we plan on handling GraphQL server side testing differently than this article:
- If the functionality saves to the database (mutations for example) then put these classes in
/app/services
like we do now. - If it's business logic that does not save to the database that we should still test, put these into classes into the
/app/graphql/resolvers
folder. - Corresponding spec files should go in
/spec/graphql/resolvers
and/spec/services/
.
In Rubymine at the bottom, look for "6: TODO" and click on that. It will show you to places where you should move
business logic to various classes in /graphql/resolvers
and /services/user_services
.
- app/graphql/types/mutation_type.rb line 18
- app/graphql/types/query_type.rb lines 33 and 40
- app/graphql/types/user_type.rb line 38
After you've moved business logic into classes and have green tests on them it's still possible that the interaction between the classes and GraphQL was broken in the process. To make sure everything on the GraphQL side still functions properly:
- Run
$ rails db:seed
in terminal. - Start your Rails server. In terminal you can do this through
$ rails s
. - Use GraphiQL to test the methods you previously changed to make sure they still work.
{
user(id: 1) {
id
address
}
}
{
login(email:"[email protected]", password:"Password!")
}
In console, verify that a session was created:
> user = User.find_by(email: '[email protected]')
> user.sessions.count > 0
Before running, edit HTTP Headers in GraphiQL:
- Header name:
Authorization
- Header value:
ba21c26ff0522ff1c5b437d35802651bc69e8930
{
logout
}
In console, verify that the session was destroyed:
> user = User.find_by(email: '[email protected]')
> user.sessions.count == 0
Set Vars:
{
"user": {
"id": "1",
"firstName": "Jane",
"lastName": "NewBoss"
}
}
Run mutation to update the user:
mutation updateUser($user: UserInputType!) {
updateUser(user:$user)
}
Then verify that the firstName and lastName were updated properly to "Jane" and "NewBoss":
{
user(id: 1) {
firstName
lastName
}
}
After you have completed the assignment, send a link of your repo to Roger and Ben for review.