-
Notifications
You must be signed in to change notification settings - Fork 107
docs: add incoming request data transform #154
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
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a2229f3
docs: add incoming request data transform
neo773 998f812
chore: run prettier
neo773 c574518
rewrite example
neo773 0189982
Merge branch 'develop' into http-filters
neo773 a59e97e
fix: json response
neo773 e3a0c20
Merge branch 'develop' into http-filters
tusharmath 43304fe
Merge branch 'develop' into http-filters
tusharmath File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -129,3 +129,79 @@ 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: UserInput): User | ||
@http( | ||
method: POST | ||
path: "/" | ||
baseURL: "http://localhost" | ||
query: [{key: "user", value: "{{args.input}}"}] | ||
) | ||
} | ||
|
||
type Query { | ||
user: User | ||
@http(baseURL: "http://localhost", path: "/user/1") | ||
} | ||
|
||
type UserInput { | ||
name: String | ||
age: Int | ||
} | ||
|
||
type User { | ||
userName: String | ||
isAdult: Boolean | ||
} | ||
|
||
schema | ||
@link(type: Script, src: "./worker.js") | ||
@server(graphiql: true) { | ||
mutation: Mutation | ||
query: Query | ||
} | ||
``` | ||
|
||
```javascript | ||
function onRequest({request}) { | ||
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} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need so much code anymore. The query params in the latest of tailcall are already parsed. |
||
``` | ||
|
||
### Transformed Data | ||
|
||
#### Request | ||
|
||
```graphql | ||
mutation { | ||
createUser(input: {name: "John", age: 18}) { | ||
isAdult | ||
userName | ||
} | ||
} | ||
``` | ||
|
||
#### Response | ||
|
||
```json | ||
{ | ||
"data": { | ||
"createUser": { | ||
"isAdult": true, | ||
"userName": "John" | ||
} | ||
} | ||
} | ||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.