Setting HTTP Request Bodies
Set bodies in HTTP requests
Apollo Connectors support POST
, PUT
, PATCH
, and DELETE
requests, including request bodies.
The http.body
field defines a JSON body to send with requests using the mapping language.
Example request bodies
Basic http.body
example
This example uses the id
and quantity
fields from the input
argument to create a JSON body like this:
type Mutation {
addProduct(input: AddProductInput!): Product
@connect(
source: "ecomm"
http: {
POST: "/products/add"
body: """
$args.input {
id
quantity
}
"""
}
selection: "id"
)
}
{
"id": 1,
"quantity": 2
}
The values in the JSON body depend on the input
argument's values.
Example http.body
with methods
This example updates a product country code using the slice
method in the body
.
type Mutation {
updateProduct(id: ID!, countryName: String!): Product
@connect(
source: "ecomm"
http: {
PUT: "/products/{$args.id}"
body: "countryCode: $args.countryName->slice(0, 2)"
}
selection: "id"
)
}
Given a countryName
of "FRANCE"
the request body would look like this:
{
"countryCode": "FR"
}
Example http.body
with literal value
This example shows how to include literal values in your request body using the $()
syntax.
type Mutation {
deleteProduct(id: ID!): DeleteResult
@connect(
source: "ecomm"
http: {
DELETE: "/products/{$args.id}"
body: """
$({
reason: "User requested deletion",
permanentDelete: true,
})
"""
}
selection: "success"
)
}
{
"reason": "User requested deletion",
"permanentDelete": true,
}
Form URL encoding
By adding a Content-Type
header of exactly application/x-www-form-urlencoded
, GraphOS Router encodes the request body as a form URL encoded string.
1type Mutation {
2 createPost(input: CreatePostInput!): Post
3 @connect(
4 http: {
5 POST: "https://api.example.com/posts"
6 headers: [{ name: "Content-Type", value: "application/x-www-form-urlencoded" }],
7 body: """
8 $args.input {
9 title
10 content
11 }
12 """
13 }
14 )
15 }
The router first maps the request body to a JSON object:
{
"title": "Hello, world!",
"content": "This is a post."
}
Then, it encodes the object as a x-www-form-urlencoded
string:
title=Hello%2C+world%21&content=This+is+a+post.
URL encoding details
Connectors follow these rules for URL encoding:
List values are indexed starting from 0 using the
list[0]=value
syntax.Nested objects use the
parent[child]=value
syntax.Spaces are encoded as
+
.
1type Mutation {
2 example(input: ExampleInput!): Example
3 @connect(
4 http: { POST: "/example", headers: [{ name: "content-type", value: "application/x-www-form-urlencoded" }] }
5 body: """
6 $args.input {
7 name
8 tags
9 addresses {
10 street
11 city
12 state
13 zip
14 }
15 }
16 """
17 )
18}
19
20input ExampleInput {
21 name: String!
22 tags: [String!]
23 addresses: [AddressInput!]
24}
25
26input AddressInput {
27 street: String!
28 city: String!
29 state: String!
30 zip: String!
31}
1name=Example
2&tags[0]=tag1
3&tags[1]=tag2
4&addresses[0][street]=123+Main+St
5&addresses[0][city]=Anytown
6&addresses[0][state]=CA
7&addresses[0][zip]=12345
8&addresses[1][street]=456+Elm+St
9&addresses[1][city]=Othertown
10&addresses[1][state]=NY
11&addresses[1][zip]=54321