Graphql-ruby: How to use GraphQL::Schema::Field?

Created on 3 Oct 2018  路  4Comments  路  Source: rmosolgo/graphql-ruby

Given I have this:

class MyFieldType < GraphQL::Schema::Field
end

How does this get used?

Most helpful comment

It's only useful when it's attached to some kind of "field owner" using the field_class configuration method. "Field owners" are Object types, Interface types, and Mutations (anything that extends GraphQL::Schema::Member::HasFields is a field owner). For example:

class BaseObject < GraphQL::Schema::Object 
  field_class MyFieldType 
end 

After the field class is hooked up to an owner like that, it starts becoming useful, and here's why:

  • The field(...) configuration method will use that class to create instances
  • The instances are accessible via the owner's .fields method

In general, the customization works like this:

  • Extend #initialize in your field class to accept some new keywords
  • Use those keywords to customize your application, for example:

    • Create shorthand keywords for common configurations

    • Store custom metadata on a field-by-field basis

If you have custom metadata, you can use it to:

  • provide runtime behavior, for example, #authorized? might look at some custom configuration stored in instance variables
  • support tooling, for example, at GitHub, we dump .graphql schema files that include @areaOfResponsibility(team: ...) for each object type, and that data is configured with custom instance variables

Does that help?

All 4 comments

It's only useful when it's attached to some kind of "field owner" using the field_class configuration method. "Field owners" are Object types, Interface types, and Mutations (anything that extends GraphQL::Schema::Member::HasFields is a field owner). For example:

class BaseObject < GraphQL::Schema::Object 
  field_class MyFieldType 
end 

After the field class is hooked up to an owner like that, it starts becoming useful, and here's why:

  • The field(...) configuration method will use that class to create instances
  • The instances are accessible via the owner's .fields method

In general, the customization works like this:

  • Extend #initialize in your field class to accept some new keywords
  • Use those keywords to customize your application, for example:

    • Create shorthand keywords for common configurations

    • Store custom metadata on a field-by-field basis

If you have custom metadata, you can use it to:

  • provide runtime behavior, for example, #authorized? might look at some custom configuration stored in instance variables
  • support tooling, for example, at GitHub, we dump .graphql schema files that include @areaOfResponsibility(team: ...) for each object type, and that data is configured with custom instance variables

Does that help?

I am 75% there. Perhaps an example would help? The missing part is how does it work with field(...), perhaps I don't understand the inter-workings of that method.

First of all, here is an example of a custom field that overrides (or alternative phrased: places some defaults) in field definitions:
http://graphql-ruby.org/fields/introduction.html#field-parameter-default-values

Having that custom field class, you can now configure a type to use that class whenever you call field:

class ObjectType < BaseObject
  # VALID
  field_class CustomField

  field :id, ID
  field :name, String

  # INVALID
  # custom_field :name
  # CustomField.new :name
end

You can see the code to verify for yourself that the field method will use the class you specified in field_class to create a new object (from_options eventually calls #new in the end).
https://github.com/rmosolgo/graphql-ruby/blob/master/lib/graphql/schema/member/has_fields.rb#L51-L52

If you just need to specify a default set of options for some fields of a type and not for all of them, take a look at the items_field example here:
http://graphql-ruby.org/fields/resolvers.html#first-ask-yourself-

Hi, I'm closing this issue because it looks like a good example has been provided and there's no more activity on it.

If there are any other queries, feel free to reopen it.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

KevinColemanInc picture KevinColemanInc  路  3Comments

rmosolgo picture rmosolgo  路  4Comments

pareeohnos picture pareeohnos  路  3Comments

amozoss picture amozoss  路  3Comments

jtippett picture jtippett  路  3Comments