项目作者: Tonkpils

项目描述 :
Creating a GraphQL Rails, React, Relay application
高级语言:
项目地址: git://github.com/Tonkpils/GRRR.git
创建时间: 2017-06-03T23:03:18Z
项目社区:https://github.com/Tonkpils/GRRR

开源协议:

下载


How to setup an app with GraphQL, Rails, React, Relay

Generate a new app

  1. run rails new APP_NAME --webpack=react --database=postgresql --skip-test --skip-coffee

    • —skip-test, —database, and —skip-coffee is optional
  2. add react-rails to Gemfile
  3. add graphql to Gemfile
  4. run bundle install
  5. run rails g graphql:install
  6. run rails g react:install
  7. run yarn add react-relay@^1.0.0
  8. run yarn add relay-compiler@^1.0.0 babel-plugin-relay@^1.0.1 --dev
  9. add <%= javascript_pack_tag 'application' %> to app/views/layouts/application.html.erb
  10. add <%= stylesheet_pack_tag 'application' %> to app/views/layouts/application.html.erb
  11. move app/javascript/packs/hello_react.jsx to app/javascript/components/hello_react.jsx
  12. modify the generated GraphQL schema file which should be under app/graphql/app_name_schema.rb

    1. AppNameSchema = GraphQL::Schema.define do
    2. query(Types::QueryType)
    3. end
    4. module RelaySchemaHelpers
    5. # Schema.json location
    6. SCHEMA_DIR = Rails.root.join('app/javascript/packs/')
    7. SCHEMA_PATH = File.join(SCHEMA_DIR, 'schema.json')
    8. def execute_introspection_query
    9. # Cache the query result
    10. Rails.cache.fetch checksum do
    11. AppNameSchema.execute GraphQL::Introspection::INTROSPECTION_QUERY
    12. end
    13. end
    14. def checksum
    15. files = Dir['app/graphql/**/*.rb'].reject { |f| File.directory?(f) }
    16. content = files.map { |f| File.read(f) }.join
    17. Digest::SHA256.hexdigest(content).to_s
    18. end
    19. def dump_schema
    20. # Generate the schema on start/reload
    21. FileUtils.mkdir_p SCHEMA_DIR
    22. result = JSON.pretty_generate(AppNameSchema.execute_introspection_query)
    23. unless File.exist?(SCHEMA_PATH) && File.read(SCHEMA_PATH) == result
    24. File.write(SCHEMA_PATH, result)
    25. end
    26. end
    27. end
    28. AppNameSchema.extend RelaySchemaHelpers
  13. Add the following to .babelrc

    1. [
    2. "relay",
    3. {
    4. "schema": "app/javascript/packs/schema.json",
    5. "debug": true
    6. }
    7. ],
  14. add __generated__/ to .gitignore

  15. create a Procfile with the following contents

    1. web: bundle exec puma -t 5:5 -p ${PORT:-3000} -e ${RACK_ENV:-development}
    2. relay: yarn run relay -- --watch
    3. webpacker: ./bin/webpack-dev-server
  16. add the following as a script in package.json

    1. "relay": "relay-compiler --src ./app/javascript --schema ./app/javascript/packs/schema.json"
  17. create a controller action and in the view you can now render your hello_react component
    1. <%= react_component 'hello_react' %>