DEV Community

Ruby on Rails GraphQL API Tutorial: From 'rails new' to First Query

Isa Levine on September 16, 2019

This week, I've been working on a takehome technical challenge asking me to deep-dive into GraphQL and create a simple API. I've never worked with ...
Collapse
 
qz135636665908 profile image
Jose-Xu

Hi Isa, thanks for the great post. I was following it step by step, however for some reason I am running into an "ActionController::RoutingError (No route matches [GET] "/graphql")" error. Do you know which step might causing the issues? Thank you!

Collapse
 
isalevine profile image
Isa Levine

Hi Jose-Xu! At which step are you seeing this error? I'd guess it's probably from trying to access localhost:3000/graphql in your browser? If so, I believe the issue is that the /graphql route only accepts a POST request.

Let me know which part you're getting that error at, and I'll see what I can do to help! :)

Collapse
 
qz135636665908 profile image
Jose-Xu

Hi Isa, thanks for your reply, and sorry for the late reply, since I do not get any notifications. And yes the problem is exactly like what you said it appears as I trying to access localhost:3000/graphql. and it seems like it is getting a GET request. However in the route.rb I have wrote post "/graphql", to: "graphql#execute". Do you know if there are any other places that I might have made a mistake? Much appreciate!

Thread Thread
 
isalevine profile image
Isa Levine • Edited

Hi Jose-Xu, no problem at all on the late reply! :)

It might help to break down the expected behavior. When you say you are trying to access localhost:3000/graphql, do you mean:

  • you are trying to visit there in a browser and see something, or
  • you are trying to send a POST request with a GraphQL Query for a body?

The only way to access the endpoint at localhost:3000/graphql is by sending a POST request (in this tutorial's case, through Insomnia) and have the requests body be formatted as a GraphQL Query. So, trying to access localhost:3000/graphql from a browser will always return an error because it's never expecting a GET request.

On the other hand, if your issue is that you're getting errors with sending a POST request with a GraphQL Query body, then the issue is probably with the request body itself! Are you using Insomnia (or something similar, like Postman) to send the POST request? And if so, can you send a screenshot so we can look over it? :)

Thread Thread
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
qz135636665908 profile image
Jose-Xu

Hi Isa, I actually found my issues, as I typed something wrong in my query_type.rb file. Thanks for made it clear that I should not have expect localhost:3000/graphql to work in the browser!

Thread Thread
 
isalevine profile image
Isa Levine

Glad to hear it Jose-Xu, and well done!

I was recreating the API step-by-step, and thought the issue might be that the GraphQL gem's rails generate graphql:install wasn't auto-creating all the files it needed to--and that would've been a WAY BIGGER PROBLEM! Very happy it was something less drastic than that. :)

Thread Thread
 
qz135636665908 profile image
Jose-Xu

thank you so much for help again!!!!

Collapse
 
otangvinhduong profile image
oTangVinhDuong • Edited

Hi I have the same error as Jose-Xu, Don't know when you made it, have you tried again?
dev.to/qz135636665908/comment/oo7f
quote:
"Hi Isa, I actually found my issues, as I typed something wrong in my query_type.rb file. Thanks for made it clear that I should not have expect localhost:3000/graphql to work in the browser!"

I don't know what error he made in query_type.rb file but I checked it very well and I got it right!
can you check this problem ? thanks

Collapse
 
isalevine profile image
Isa Levine

Hi oTangVinhDuong! If I'm not mistaken, I believe Jose-Xu's problem was trying to access localhost:3000/graphql in a browser.

Since GraphQL relies on sending POST requests only, you will need to use a tool like Insomnia, Postman, or graphiql to view the query results.

To double-check everything is working, I just used Insomnia to send a POST request to localhost:3000/graphql/ using a GraphQL body with the following query:

query {
    allOrders {
        id
        description
        total      
        successfulPayments {
            id
            amount
            status
        }
    }
}

I also confirmed that the query_type.rb file I used just now matches what is above--this is the code I just ran:

module Types
  class QueryType < Types::BaseObject
    field :all_orders, [Types::OrderType], null: false

    def all_orders
      Order.all
    end
  end
end

Let me know if you're still having issues, and I'll try to help as best I can! :)

Collapse
 
pabloc profile image
PabloC

Great post. Thank you so much.