This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A tiny RSpec helper | |
# | |
# spec/support/helpers/let_env.rb | |
module LetEnv | |
def let_env(name) | |
around do |example| | |
@original_environment_vars ||= {} | |
@original_environment_vars[name] ||= ENV[name.to_s] | |
ENV[name.to_s] = yield |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/graphql/my_rails_app_schema.rb | |
class MyRailsAppSchema < GraphQL::Schema | |
mutation(Types::MutationType) | |
query(Types::QueryType) | |
end | |
# app/graphql/types/query_type.rb | |
module Types | |
class QueryType < Types::BaseObject | |
# Add root-level fields here. They will be entry points for queries on your schema. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/routes.rb | |
if Rails.env.development? | |
mount GraphiQL::Rails::Engine, at: "/graphiql", graphql_path: "/graphql" | |
end | |
post "/graphql", to: "graphql#execute" | |
# app/controllers/graphql_controller.rb | |
class GraphqlController < ApplicationController | |
def execute |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/graphql/mutations/update_post.rb | |
module Mutations | |
class UpdatePost < GraphQL::Schema::Mutation | |
argument :post_id, ID, required: true | |
argument :description, String, required: true | |
description "Create a new post in a team" | |
field :post, Types::UpdatedPostType, null: true | |
field :errors, [String], null: false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/queries/create_comment_mutator.rb | |
class CreateCommentMutator < ApplicationQuery | |
property :post_id | |
property :comment, validates: { length: { minimum: 1, maximum: 500 } } | |
validate :post_belongs_to_team | |
def post_belongs_to_team | |
return if post.present? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/graphql/mutations/create_comment.rb | |
module Mutations | |
class CreateComment < GraphQL::Schema::Mutation | |
argument :post_id, ID, required: true | |
argument :comment, String, required: true | |
description "Create a comment on a post" | |
field :id, ID, null: true | |
field :errors, [String], null: false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/graphql/types/query_type.rb | |
module Types | |
class QueryType < Types::BaseObject | |
field :user, Types::UserType, null: false do | |
argument :id, ID, required: true | |
end | |
def user | |
resolver = UserResolver.new(context, args) | |
resolver.user |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/queries/users_resolver.rb | |
class UsersResolver < ApplicationQuery | |
def users | |
scope = if current_user.admin? | |
User.all | |
else | |
current_user.team.users | |
end | |
# Avoid N+1 when accessing the avatar, with a shortcut for `.includes(avatar_attachment: :blob)`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/graphql/types/query_type.rb | |
module Types | |
class QueryType < Types::BaseObject | |
field :users, [Types::UserType], null: false | |
def users | |
resolver = UsersResolver.new(context) | |
resolver.users | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/queries/application_query.rb | |
class ApplicationQuery | |
include ActiveModel::Model | |
# Set up accessors with validations in a single step. | |
def self.property(name, options = {}) | |
attr_accessor name | |
validates(name, options[:validates]) if options.key?(:validates) | |
end | |
NewerOlder