LoginSignup
3
0

More than 1 year has passed since last update.

graphql-rubyを利用する際のtips

Last updated at Posted at 2022-12-19

イントロスペクションのブロック

通常時は、下記のようなイントロスペクションを実行するとGraphQLのスキーマ情報が一覧できてしまう

{
  __schema {
   	queryType {
   	  name
   	  description
   	}
  }
}

スキーマ構造をされにくくするために、本番環境では実行できない方が好ましい
(queryやmutationから推測はできてしまうが...)

graphql-rubyをrailsで使うときは以下の記述でブロックできる

class GraphqlTestSchema < GraphQL::Schema
  disable_introspection_entry_points if Rails.env.production?
	...
end

directiveの書き方

主に注釈など、SDL(スキーマ定義言語)で仕様を表現したいときに使う

https://graphql-ruby.org/type_definitions/directives.html#schema-directives


# directiveの型を作成する
class Directives::Permission < GraphQL::Schema::Directive
  argument :level, String
  # どのtypeで使うのか定義
  locations FIELD, OBJECT
end

class Types::TestType < Types::BaseObject
  field :test_field, String, null: false

  # TestというObjectタイプに付与する例
  directive Directives::Permission, level: "manager"
end

SDLで確認する

irb(main):003:0> printer = GraphQL::Schema::Printer.new(GraphqlTestSchema)
irb(main):003:0> puts printer.print_schema

directive @permission(level: String!) on FIELD | OBJECT                                                                     
                                                                         
(中略)

type Test @permission(level: "manager") {
  testField: String!
}
3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0