mailjet / mailjet-gem

[API v3] Mailjet official Ruby GEM
https://dev.mailjet.com
Other
130 stars 72 forks source link

Exception when exposing Mailjet::Resource in GraphQL Type #262

Open BrahimDahmani opened 1 month ago

BrahimDahmani commented 1 month ago

When attempting to expose Mailjet::Resource in a GraphQL Type, the following exception is raised:

GraphQL::Backtrace::TracedError - Unhandled error during GraphQL execution:
                Failed to implement MessageHistory.event_at, tried:
              - `Types::ResourceTypes::MessageHistoryType#event_at`, which did not exist
              - `Mailjet::Messagehistory#event_at`, which did not exist
              - Looking up hash key `:event_at` or `"event_at"` on `#<Mailjet::Messagehistory:0x00005622b03ad6e8>`, but it wasn't a Hash
              To implement this field, define one of the methods above (and check for typos), or supply a `fallback_value`.

Root cause: The issue stems from GraphQL's field resolution mechanism. GraphQL checks if a field exists using .respond_to?(field). However, Mailjet uses method_missing to dynamically handle attribute access, which causes .respond_to?(field) to always return false.

Proposed solution: To resolve this, Mailjet::Resource should implement .respond_to_missing?. This will ensure that .respond_to?(field) returns the correct value for dynamically generated attribute methods, allowing GraphQL to properly resolve fields on Mailjet resources.

I'll make a PR!

Thank you :slightly_smiling_face: