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

throw with body in rhai map_response fails #3069

Closed
lennyburdette opened this issue May 9, 2023 · 1 comment · Fixed by #3089
Closed

throw with body in rhai map_response fails #3069

lennyburdette opened this issue May 9, 2023 · 1 comment · Fixed by #3089

Comments

@lennyburdette
Copy link
Contributor

lennyburdette commented May 9, 2023

Version 1.18.0

This works (copied from the docs).

fn supergraph_service(service) {
  let f = |request| {
    throw #{
      status: 403,
      body: #{
        errors: [#{
          message: `I have raised a 403`,
          extensions: #{
            code: "ACCESS_DENIED"
          }
        }]
      }
    };
  };

  service.map_request(f);
}

The result (which has the correct 403 status code):

{
  "data": {},
  "errors": [
    {
      "message": "I have raised a 403",
      "extensions": {
        "code": "ACCESS_DENIED"
      }
    }
  ]
}

This does not:

fn supergraph_service(service) {
  let f = |response| {
    throw #{
      status: 403,
      body: #{
        errors: [#{
          message: `I have raised a 403`,
          extensions: #{
            code: "ACCESS_DENIED"
          }
        }]
      }
    };
  };

  service.map_response(f); // RESPONSE NOT REQUEST
}

The result has the correct 403 status code but the response body is

{
  "data": {},
  "errors": [
    {
      "message": ""
    }
  ]
}

Throwing with just a message in map_response works correctly:

fn supergraph_service(service) {
  let f = |response| {
    throw #{
      status: 403,
      message: "wtf"
    };
  };

  service.map_response(f);
}
@garypen
Copy link
Contributor

garypen commented May 9, 2023

also: the example in the docs is wrong. I think @lennyburdette must have fixed that when he copied the example.

git diff docs/source/customizations/rhai-api.mdx
diff --git a/docs/source/customizations/rhai-api.mdx b/docs/source/customizations/rhai-api.mdx
index c181f8825..6ca0ea151 100644
--- a/docs/source/customizations/rhai-api.mdx
+++ b/docs/source/customizations/rhai-api.mdx
@@ -108,6 +108,7 @@ fn supergraph_service(service) {
                 }]
             }
         };
+    };
     // Map our request using our closure
     service.map_request(f);
 }

@garypen garypen changed the title throw with body in rhai map_request fails throw with body in rhai map_response fails May 10, 2023
@garypen garypen self-assigned this May 11, 2023
garypen added a commit that referenced this issue May 12, 2023
In #2677 we added support for throwing graphql errors from rhai.
Support was added for requests. This extends the mechanism to include
responses.

fixes: #3069
garypen added a commit that referenced this issue May 15, 2023
In #2677 we added support for throwing graphql errors from rhai. Support
was added for requests. This extends the mechanism to include responses.

fixes: #3069

<!-- start metadata -->

**Checklist**

Complete the checklist (and note appropriate exceptions) before a final
PR is raised.

- [x] Changes are compatible[^1]
- [x] Documentation[^2] completed
- [x] Performance impact assessed and acceptable
- Tests added and passing[^3]
    - [ ] Unit Tests
    - [ ] Integration Tests
    - [ ] Manual Tests

**Exceptions**

*Note any exceptions here*

**Notes**

[^1]. It may be appropriate to bring upcoming changes to the attention
of other (impacted) groups. Please endeavour to do this before seeking
PR approval. The mechanism for doing this will vary considerably, so use
your judgement as to how and when to do this.
[^2]. Configuration is an important part of many changes. Where
applicable please try to document configuration examples.
[^3]. Tick whichever testing boxes are applicable. If you are adding
Manual Tests:
- please document the manual testing (extensively) in the Exceptions.
- please raise a separate issue to automate the test and label it (or
ask for it to be labeled) as `manual test`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants