Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a cop to enforce prepare argument as a Symbol or String #150

Merged
merged 1 commit into from
Dec 21, 2023

Conversation

DougEdey
Copy link
Contributor

I've been working a lot with Sorbet and introduced better support for GraphQL Prepare Methods including adding support for the prepare method to be defined on the current class

Using the upstream example for graphql-ruby

class AddInstrument < GraphQL::Schema::Mutation
    class << self
      def prepare_name(value, context)
        value.capitalize
      end
    end

    null true
    description "Register a new musical instrument in the database"

    argument :name, String, prepare: :prepare_name
    argument :family, Family

    field :instrument, InstrumentType, null: false
    # This is meaningless, but it's to test the conflict with `Hash#entries`
    field :entries, [InstrumentType], null: false
    # Test `extras` injection
    field :ee, String, null: false
    extras [:execution_errors]
    def resolve(name:, family:, execution_errors:)
      instrument = Jazz::Models::Instrument.new(name, family)
      Jazz::Models.data["Instrument"] << instrument
      {instrument: instrument, entries: Jazz::Models.data["Instrument"], ee: execution_errors.class.name}
    end
  end

This cop isn't enabled by default because I don't think everyone wants this, but we've found that removing lambdas makes our codebase much more effective to test, and easier to read, and the combination of sorbet/tapioca makes it even more effective to write & maintain code.

This cop by default will raise offenses when a block argument is provided, and request that a string or symbol is provided. but can be configured to require a string XOR symbol based on EnforcedStyle

@DougEdey
Copy link
Contributor Author

I'm not sure why Rubocop 0.90.0 is failing

Copy link
Owner

@DmitryTsepelev DmitryTsepelev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey hey, LGTM, thank you! Maybe rubocop works wrong because I updated the version in the master recently. I'm going to merge as is and fix right in the master

@DmitryTsepelev DmitryTsepelev merged commit 18e9a4b into DmitryTsepelev:master Dec 21, 2023
7 of 10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants