Blook

published: 3/13/2019last updated: 4/10/2019

Query(GET)

*Goal of this chapter

  • Implement Query(GET)

Okay, In this chapter we will try to implement Query.

Query is equivalent to Rest GET request.

First just copy and paste the code, then I will recap what we did!

Okay, Let's begin!

This is inital query_type.rb

module Types class QueryType < Types::BaseObject # Add root-level fields here. # They will be entry points for queries on your schema. # TODO: remove me field :test_field, String, null: false, description: "An example field added by the generator" def test_field "Hello World!" end end end

Add new query field posts

field :posts, [PostType], null: true def posts
Post.all end

So the query_type.rb is

module Types class QueryType < Types::BaseObject # Add root-level fields here. # They will be entry points for queries on your schema. # TODO: remove me field :test_field, String, null: false, description: "An example field added by the generator" def test_field "Hello World!" end field :posts, [PostType], null: true def posts Post.all end end end

This means

  • field posts is array of PostType and It is ok to return nullable value.

  • posts method returns Post.all

Next, We need to define PostType

https://graphql-ruby.org/getting_started#getting-started

rails g graphql:object Post title:String body:String
Running via Spring preloader in process 62001 create app/graphql/types/post_type.rb

This generates the below code

module Types class PostType < Types::BaseObject field :title, String, null: true field :body, String, null: true end end

Declare types

https://graphql-ruby.org/getting_started#declare-types

Imitate official guide

module Types class PostType < Types::BaseObject description "A blog post" field :id, ID, null: false field :title, String, null: false field :body, String, null: true end end

Query from graphiql

http://api.localhost:3000/graphiql

Okay let's try to query by using graphiql

It works!!!

Reverse Recap

Here we will recap the flow!!!

1. query posts from graphiql

Check the terminal log!

2. POST "/graphql" is fired.

3. It fires GraphqlController#execute action.

Started POST "/graphql" for 127.0.0.1 at 2019-03-12 15:09:45 +0800 Processing by GraphqlController#execute as */* Parameters: {"query"=>"query {\n posts {\n id\n title\n body\n }\n}", "variables"=>nil, "subdomain"=>"api", "graphql"=>{"query"=>"query {\n posts {\n id\n title\n body\n }\n}", "variables"=>nil}} Post Load (0.9ms) SELECT "posts".* FROM "posts" ↳ app/controllers/graphql_controller.rb:10 Completed 200 OK in 40ms (Views: 0.3ms | ActiveRecord: 11.1ms)

4. In def execute action, query is executed.

class GraphqlController < ApplicationController def execute variables = ensure_hash(params[:variables]) query = params[:query] operation_name = params[:operationName] context = { # Query context goes here, for example: # current_user: current_user, } result = RailsApiGraphqlCrudTutoSchema.execute(query, variables: variables, context: context, operation_name: operation_name) render json: result rescue => e raise e unless Rails.env.development? handle_error_in_development e end ... end

RailsApiGraphqlCrudTutoSchema is defined in...

class RailsApiGraphqlCrudTutoSchema < GraphQL::Schema mutation(Types::MutationType) query(Types::QueryType) end

Types::QueryType is defined in...

module Types class QueryType < Types::BaseObject # Add root-level fields here. # They will be entry points for queries on your schema. # TODO: remove me field :test_field, String, null: false, description: "An example field added by the generator" def test_field "Hello World!" end field :posts, [PostType], null: true def posts Post.all end end end

PostType is defined in...

module Types class PostType < Types::BaseObject description "A blog post" field :id, ID, null: false field :title, String, null: false field :body, String, null: true end end

So, finally we get the json result!

{
"data": {
"posts": [
{
"id": "1",
"title": "Death Be Not Proud",
"body": "Sapiente dignissimos veniam. Et eius velit. Quam dolorem id."
}
]
}
}