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

docs: add incoming request data transform #154

Closed
wants to merge 7 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions docs/guides/http-filters.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
neo773 marked this conversation as resolved.
Show resolved Hide resolved
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}
}
Copy link
Contributor

Choose a reason for hiding this comment

The 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"
}
}
}
```
Loading