Giter Site home page Giter Site logo

Comments (2)

samuraikun avatar samuraikun commented on June 12, 2024

実行ログ

Gemfilegraphql を追加後に以下を実行

ログ
$ bin/rails generate graphql:install
      create  app/graphql/types
      create  app/graphql/types/.keep
      create  app/graphql/workspace_schema.rb
      create  app/graphql/types/base_object.rb
      create  app/graphql/types/base_argument.rb
      create  app/graphql/types/base_field.rb
      create  app/graphql/types/base_enum.rb
      create  app/graphql/types/base_input_object.rb
      create  app/graphql/types/base_interface.rb
      create  app/graphql/types/base_scalar.rb
      create  app/graphql/types/base_union.rb
      create  app/graphql/types/query_type.rb
add_root_type  query
      create  app/graphql/mutations
      create  app/graphql/mutations/.keep
      create  app/graphql/mutations/base_mutation.rb
      create  app/graphql/types/mutation_type.rb
add_root_type  mutation
      create  app/controllers/graphql_controller.rb
       route  post "/graphql", to: "graphql#execute"
     gemfile  graphiql-rails
       route  graphiql-rails
      create  app/graphql/types/node_type.rb
      insert  app/graphql/types/query_type.rb
      create  app/graphql/types/base_connection.rb
      create  app/graphql/types/base_edge.rb
      insert  app/graphql/types/base_object.rb
      insert  app/graphql/types/base_object.rb
      insert  app/graphql/types/base_union.rb
      insert  app/graphql/types/base_union.rb
      insert  app/graphql/types/base_interface.rb
      insert  app/graphql/types/base_interface.rb
      insert  app/graphql/workspace_schema.rb
Gemfile has been modified, make sure you `bundle install`

$ bundle install
Fetching gem metadata from https://rubygems.org/.........
Resolving dependencies...
Using rake 13.0.6
Using concurrent-ruby 1.1.10
Using minitest 5.16.3
Using builder 3.2.4
Using erubi 1.11.0
Using nio4r 2.5.8
Using websocket-extensions 0.1.5
Using marcel 1.0.2
Using mini_mime 1.1.2
Using digest 3.1.0
Using timeout 0.3.0
Using strscan 3.0.4
Using public_suffix 5.0.0
Using ast 2.4.2
Using backport 1.2.0
Using benchmark 0.2.0
Using racc 1.6.0
Using msgpack 1.5.6
Using crass 1.0.6
Using rack 2.2.4
Using bundler 2.3.22
Using matrix 0.4.2
Using regexp_parser 2.5.0
Using diff-lcs 1.5.0
Using bindex 0.8.1
Using io-console 0.5.11
Using method_source 1.0.0
Using thor 1.2.1
Using zeitwerk 2.6.0
Using graphql 2.0.16
Using jaro_winkler 1.5.4
Using json 2.6.2
Using rexml 3.2.5
Using language_server-protocol 3.17.0.1
Using rb-fsevent 0.11.2
Using parallel 1.22.1
Using rainbow 3.1.1
Using rbs 2.6.0
Using e2mmap 0.1.0
Using unicode-display_width 2.2.0
Using rubyzip 2.3.2
Using websocket 1.2.9
Using tilt 2.0.11
Using webrick 1.7.0
Using sqlite3 1.5.0 (aarch64-linux)
Using i18n 1.12.0
Using tzinfo 2.0.5
Using nokogiri 1.13.8 (aarch64-linux)
Using websocket-driver 0.7.5
Using mail 2.7.1
Using net-protocol 0.1.3
Using addressable 2.8.1
Using parser 3.1.2.1
Using puma 5.6.5
Using rack-test 2.0.2
Using bootsnap 1.13.0
Using sprockets 4.1.1
Using reline 0.3.1
Using kramdown 2.4.0
Using ruby-progressbar 1.11.0
Using activesupport 7.0.3.1
Using loofah 2.18.0
Using ffi 1.15.5
Using net-imap 0.2.3
Using xpath 3.2.0
Using irb 1.4.1
Using kramdown-parser-gfm 1.1.0
Using rbs_rails 0.11.0
Using net-smtp 0.3.1
Using rubocop-ast 1.21.0
Using net-pop 0.1.1
Using terminal-table 3.0.2
Using yard 0.9.28
Using rails-dom-testing 2.0.3
Using rails-html-sanitizer 1.4.3
Using globalid 1.0.0
Using activemodel 7.0.3.1
Using capybara 3.37.1
Using debug 1.6.2
Using rb-inotify 0.10.1
Using rubocop 1.36.0
Using childprocess 4.1.0
Using reverse_markdown 2.1.1
Using actionview 7.0.3.1
Using activejob 7.0.3.1
Using solargraph 0.46.0
Using selenium-webdriver 4.4.0
Using jbuilder 2.11.5
Using listen 3.7.1
Using actionpack 7.0.3.1
Using webdrivers 5.0.0
Using activerecord 7.0.3.1
Using actioncable 7.0.3.1
Using activestorage 7.0.3.1
Using actionmailer 7.0.3.1
Using railties 7.0.3.1
Using sprockets-rails 3.4.2
Using steep 1.1.1
Using actionmailbox 7.0.3.1
Using actiontext 7.0.3.1
Using importmap-rails 1.1.5
Using web-console 4.2.0
Using stimulus-rails 1.1.0
Using turbo-rails 1.1.1
Using rails 7.0.3.1
Fetching graphiql-rails 1.8.0
Installing graphiql-rails 1.8.0
Bundle complete! 20 Gemfile dependencies, 106 gems now installed.
Bundled gems are installed into `./vendor/bundle`

from graphql-ruby-hands-on.

samuraikun avatar samuraikun commented on June 12, 2024

Userモデルを追加してGraphQLスキーマを作ってみる

2e19e6b

作成したUserテーブル

create_table "users", force: :cascade do |t|
    t.string "email", null: false
    t.string "password_digest", null: false
    t.string "first_name", null: false
    t.string "last_name", null: false
    t.string "username", null: false
    t.string "postal_code"
    t.string "address"
    t.string "phone"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["username"], name: "index_users_on_username", unique: true
end

作成したUserモデルに対するGraphQLスキーマを作成

$ bin/rails g graphql:object User
      create  app/graphql/types/user_type.rb

DBスキーマと同じフィールドでスキーマが作成されることを確認

app/graphql/types/user_type.rb

# frozen_string_literal: true

module Types
  class UserType < Types::BaseObject
    field :id, ID, null: false
    field :email, String, null: false
    field :password_digest, String, null: false
    field :first_name, String, null: false
    field :last_name, String, null: false
    field :username, String, null: false
    field :postal_code, String
    field :address, String
    field :phone, String
    field :created_at, GraphQL::Types::ISO8601DateTime, null: false
    field :updated_at, GraphQL::Types::ISO8601DateTime, null: false
  end
end

query_type.rb にUserのQueryを追加

事前にUserのレコードを作成

User.create!(email: "[email protected]", password: "passworD@123", first_name: "Mike", last_name: "Ross", usern
ame: "mike123")

query_type.rb

module Types
  class QueryType < Types::BaseObject
    # Add `node(id: ID!) and `nodes(ids: [ID!]!)`
    include GraphQL::Types::Relay::HasNodeField
    include GraphQL::Types::Relay::HasNodesField

    # Add root-level fields here.
    # They will be entry points for queries on your schema.

    field :user, Types::UserType, null: true do
      description "Find User by ID"
      argument :id, ID, required: true
    end
    def user(id:)
      User.find(id)
    end
    def users(page: nil, items: nil)
      User.all
    end
  end
end

http://localhost:3000/graphiql にアクセスして Userを取得できるかどうか検証

{
  user(id: 1) {
    email
    firstName
    lastName
    username
  }
}

スクリーンショット 2023-01-08 14 41 06

MutationsでUser作成

mutation_type.rb に以下を追加

module Types
  class MutationType < Types::BaseObject
    field :create_user, Types::UserType, null: false do
      argument :email, String, required: true
      argument :password, String, required: true
      argument :first_name, String, required: true
      argument :last_name, String, required: true
      argument :username, String, required: true
      argument :postal_code, String, required: false
      argument :address, String, required: false
      argument :phone, String, required: false
    end
    def create_user(email:, password:, first_name:, last_name:, username:, postal_code: nil, address: nil, phone: nil)
      User.create!(
        email: email,
        password: password,
        first_name: first_name,
        last_name: last_name,
        username: username,
        postal_code: postal_code,
        address: address,
        phone: phone
      )
    end
  end
end

スクリーンショット 2023-01-08 14 45 22

ここまでの疑問・感想

  • query_type.rb, mutation_type.rb 内で、ActiveRecordによるDBクエリを実行する処理をどんどん追加すると肥大化するので、何かしら構造化して整理する必要がありそう

from graphql-ruby-hands-on.

Related Issues (7)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.