Given I have this:
class MyFieldType < GraphQL::Schema::Field
end
How does this get used?
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:
field(...) configuration method will use that class to create instances .fields methodIn general, the customization works like this:
#initialize in your field class to accept some new keywords If you have custom metadata, you can use it to:
#authorized? might look at some custom configuration stored in instance variables.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.
Most helpful comment
It's only useful when it's attached to some kind of "field owner" using the
field_classconfiguration method. "Field owners" are Object types, Interface types, and Mutations (anything that extendsGraphQL::Schema::Member::HasFieldsis a field owner). For example:After the field class is hooked up to an owner like that, it starts becoming useful, and here's why:
field(...)configuration method will use that class to create instances.fieldsmethodIn general, the customization works like this:
#initializein your field class to accept some new keywordsIf you have custom metadata, you can use it to:
#authorized?might look at some custom configuration stored in instance variables.graphqlschema files that include@areaOfResponsibility(team: ...)for each object type, and that data is configured with custom instance variablesDoes that help?