From a2229f3e3b5263457afbac69241bcb1436e747f4 Mon Sep 17 00:00:00 2001 From: neo773 <62795688+neo773@users.noreply.github.com> Date: Wed, 13 Mar 2024 23:39:27 +0530 Subject: [PATCH 1/4] docs: add incoming request data transform --- docs/guides/http-filters.md | 85 +++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/docs/guides/http-filters.md b/docs/guides/http-filters.md index 2926765322..312eddfa3e 100644 --- a/docs/guides/http-filters.md +++ b/docs/guides/http-filters.md @@ -131,3 +131,88 @@ type Response = { body?: string } ``` + +## Modify Incoming Request Data + +You can also modify incoming request data by parsing and transforming the request's query parameters or body. Below is an example where we modify the incoming request data by extracting user information from a query parameter and then setting the request body with the transformed user data. + +```graphql +type Mutation { + createUser(input: InputUser): User + @http( + method: POST + path: "/" + baseURL: "https://echo.free.beeceptor.com" + query: [{ key: "user", value: "{{args.input}}" }] + ) +} + +type Query { + user: User @http(baseURL: "https://jsonplaceholder.typicode.com", path: "/user/1") +} + +type UserProperties { + userFirstName: String + userLastName: String + userAge: Int +} + +type User { + parsedQueryParams: UserProperties +} + +input InputUser { + firstName: String + lastName: String + age: Int +} + +schema + @link(type: Script, src: "./worker.js") + @server(graphiql: true) { + mutation: Mutation + query: Query +} + +``` + +```javascript +function onRequest({ request }) { + console.log(request); + const baseUrl = request.url.split('?')[0]; + const user = JSON.parse(decodeURIComponent(request.url.split('?user=')[1])); + const modifiedUrl = baseUrl + `?userAge=${encodeURIComponent(user.age)}&userFirstName=${encodeURIComponent(user.firstName)}&userLastName=${encodeURIComponent(user.lastName)}`; + request.url = modifiedUrl; + + return { request: request } +} +``` + +### Transformed Data + +#### Request +```graphql +mutation { + createUser(input: { firstName: "John", lastName: "Doe", age: 23 }) { + parsedQueryParams { + userAge + userLastName + userFirstName + } + } +} +``` +#### Response +```json +{ + "data": { + "createUser": { + "parsedQueryParams": { + "userAge": "23", + "userLastName": "Doe", + "userFirstName": "John" + } + } + } +} +``` \ No newline at end of file From 998f8125e432f0551f140e355083d959de5d3246 Mon Sep 17 00:00:00 2001 From: neo773 <62795688+neo773@users.noreply.github.com> Date: Wed, 13 Mar 2024 23:44:14 +0530 Subject: [PATCH 2/4] chore: run prettier --- docs/guides/http-filters.md | 42 +++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/docs/guides/http-filters.md b/docs/guides/http-filters.md index 312eddfa3e..2d803c055c 100644 --- a/docs/guides/http-filters.md +++ b/docs/guides/http-filters.md @@ -143,12 +143,16 @@ type Mutation { method: POST path: "/" baseURL: "https://echo.free.beeceptor.com" - query: [{ key: "user", value: "{{args.input}}" }] + query: [{key: "user", value: "{{args.input}}"}] ) } type Query { - user: User @http(baseURL: "https://jsonplaceholder.typicode.com", path: "/user/1") + user: User + @http( + baseURL: "https://jsonplaceholder.typicode.com" + path: "/user/1" + ) } type UserProperties { @@ -173,27 +177,37 @@ schema mutation: Mutation query: Query } - ``` ```javascript -function onRequest({ request }) { - console.log(request); - const baseUrl = request.url.split('?')[0]; - const user = JSON.parse(decodeURIComponent(request.url.split('?user=')[1])); - const modifiedUrl = baseUrl + `?userAge=${encodeURIComponent(user.age)}&userFirstName=${encodeURIComponent(user.firstName)}&userLastName=${encodeURIComponent(user.lastName)}`; - request.url = modifiedUrl; - - return { request: request } +function onRequest({request}) { + console.log(request) + const baseUrl = request.url.split("?")[0] + const user = JSON.parse( + decodeURIComponent(request.url.split("?user=")[1]), + ) + const modifiedUrl = + baseUrl + + `?userAge=${encodeURIComponent( + user.age, + )}&userFirstName=${encodeURIComponent( + user.firstName, + )}&userLastName=${encodeURIComponent(user.lastName)}` + request.url = modifiedUrl + + return {request: request} } ``` ### Transformed Data #### Request + ```graphql mutation { - createUser(input: { firstName: "John", lastName: "Doe", age: 23 }) { + createUser( + input: {firstName: "John", lastName: "Doe", age: 23} + ) { parsedQueryParams { userAge userLastName @@ -202,7 +216,9 @@ mutation { } } ``` + #### Response + ```json { "data": { @@ -215,4 +231,4 @@ mutation { } } } -``` \ No newline at end of file +``` From c574518e61465d9dce45b50761078599590f94a8 Mon Sep 17 00:00:00 2001 From: neo773 <62795688+neo773@users.noreply.github.com> Date: Fri, 15 Mar 2024 14:51:48 +0530 Subject: [PATCH 3/4] rewrite example --- docs/guides/http-filters.md | 63 +++++++++++-------------------------- 1 file changed, 19 insertions(+), 44 deletions(-) diff --git a/docs/guides/http-filters.md b/docs/guides/http-filters.md index 2d803c055c..8e3b9c222e 100644 --- a/docs/guides/http-filters.md +++ b/docs/guides/http-filters.md @@ -138,37 +138,28 @@ You can also modify incoming request data by parsing and transforming the reques ```graphql type Mutation { - createUser(input: InputUser): User + createUser(input: UserInput): User @http( method: POST path: "/" - baseURL: "https://echo.free.beeceptor.com" + baseURL: "http://localhost" query: [{key: "user", value: "{{args.input}}"}] ) } type Query { user: User - @http( - baseURL: "https://jsonplaceholder.typicode.com" - path: "/user/1" - ) + @http(baseURL: "http://localhost", path: "/user/1") } -type UserProperties { - userFirstName: String - userLastName: String - userAge: Int +type UserInput { + name: String + age: Int } type User { - parsedQueryParams: UserProperties -} - -input InputUser { - firstName: String - lastName: String - age: Int + userName: String + isAdult: Boolean } schema @@ -181,20 +172,12 @@ schema ```javascript function onRequest({request}) { - console.log(request) - const baseUrl = request.url.split("?")[0] - const user = JSON.parse( - decodeURIComponent(request.url.split("?user=")[1]), - ) - const modifiedUrl = - baseUrl + - `?userAge=${encodeURIComponent( - user.age, - )}&userFirstName=${encodeURIComponent( - user.firstName, - )}&userLastName=${encodeURIComponent(user.lastName)}` - request.url = modifiedUrl - + const query = JSON.parse(request.uri.query.user) + const modifiedQuery = { + user_name: query.name, + is_adult: query.age >= 18, + } + request.uri.query = {user: JSON.stringify(modifiedQuery)} return {request: request} } ``` @@ -205,14 +188,9 @@ function onRequest({request}) { ```graphql mutation { - createUser( - input: {firstName: "John", lastName: "Doe", age: 23} - ) { - parsedQueryParams { - userAge - userLastName - userFirstName - } + createUser(input: {name: "John", age: 18}) { + isAdult + userName } } ``` @@ -223,11 +201,8 @@ mutation { { "data": { "createUser": { - "parsedQueryParams": { - "userAge": "23", - "userLastName": "Doe", - "userFirstName": "John" - } + "isAdult": false, + "userName": "Jdasdasdohn" } } } From a59e97e208ad4ddc3f7140d6d35595982c80a67d Mon Sep 17 00:00:00 2001 From: neo773 <62795688+neo773@users.noreply.github.com> Date: Fri, 15 Mar 2024 14:54:55 +0530 Subject: [PATCH 4/4] fix: json response --- docs/guides/http-filters.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/guides/http-filters.md b/docs/guides/http-filters.md index 8e3b9c222e..f3e9b2496a 100644 --- a/docs/guides/http-filters.md +++ b/docs/guides/http-filters.md @@ -201,8 +201,8 @@ mutation { { "data": { "createUser": { - "isAdult": false, - "userName": "Jdasdasdohn" + "isAdult": true, + "userName": "John" } } }