Skip to content

feat: Type and Query complexity callbacks#273

Merged
zachdaniel merged 3 commits intoash-project:mainfrom
team-alembic:feat/custom-complexity
Feb 11, 2025
Merged

feat: Type and Query complexity callbacks#273
zachdaniel merged 3 commits intoash-project:mainfrom
team-alembic:feat/custom-complexity

Conversation

@mbuhot
Copy link
Copy Markdown
Contributor

@mbuhot mbuhot commented Feb 10, 2025

Resolves #270

A custom complexity callback can be provided for a type to be used when that type is resolved through a relationship.

A query-level complexity callback applies when the resource has a top-level query in the schema.

Contributor checklist

  • Bug fixes include regression tests
  • Features include unit/acceptance tests

A custom complexity callback can be provided for a type to be used when that type is resolved through a relationship.

A query-level complexity callback applies when the resource has a top-level query in the schema.
default: []
],
complexity: [
type: :mod_arg,
Copy link
Copy Markdown
Contributor

@zachdaniel zachdaniel Feb 10, 2025

Choose a reason for hiding this comment

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

Suggested change
type: :mod_arg,
type: :mfa,

I think we should make these MFAs.

Copy link
Copy Markdown
Contributor

@zachdaniel zachdaniel left a comment

Choose a reason for hiding this comment

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

Looks great! I think we should make these MFAs instead of just mod/fun pairs (hardcoded args get prepended to our arguments). That allows better reuse, i.e complexity {MyApp.Complexity, :complexity, [__MODULE__, :very_complex]} etc.

@mbuhot
Copy link
Copy Markdown
Contributor Author

mbuhot commented Feb 11, 2025

@zachdaniel I'm having some trouble forwarding the MFA through to Absinthe:

 2) test custom complexity calculation (AshGraphql.ReadTest)
     test/read_test.exs:424
     ** (Absinthe.AnalysisError) Invalid value returned from complexity analyzer.

     Analyzing field:

       sponsoredComments

     Defined at:

       /Users/michaelbuhot/src/ash-project/ash_graphql/lib/resource/resource.ex:4338

     Got value:

         {AshGraphql.Test.SponsoredComment, :query_complexity, []}

     The complexity value must be a non negative integer.

It looks like only functions, {mod, fun} tuples and constants are supported:

https://github.com/absinthe-graphql/absinthe/blob/main/lib/absinthe/phase/document/complexity/analysis.ex#L107-L129

@zachdaniel
Copy link
Copy Markdown
Contributor

Ah, right you are. I had a similar complication at some point in the past and did some super hacky shit to get around it. In that case, never mind 😄

@zachdaniel
Copy link
Copy Markdown
Contributor

Please run mix docs and mix spark.formatter and mix docs and commit the result.

@zachdaniel zachdaniel merged commit 7906c4e into ash-project:main Feb 11, 2025
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.

Feat: Complexity callback in DSL

2 participants