使用graphql-ruby的技巧

反省的障碍

通常情况下,执行下面的自省操作就可以列出GraphQL的模式信息。

{
  __schema {
   	queryType {
   	  name
   	  description
   	}
  }
}

为了使模式结构不容易被攻击,在生产环境中最好不要执行(尽管可以从查询或变更中推测出来)…

在Rails中使用graphql-ruby时,可以使用以下代码块进行阻塞。

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

指示的写法

当需要主要注释和使用SDL(Schema Definition Language)表示规范时使用。

请给出以下网页的中文翻译: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!
}
bannerAds