Amplify has re-imagined the way frontend developers build fullstack applications. Develop and deploy without the hassle.

Page updated Feb 21, 2024

Advanced Workflows

This section describes different use cases for constructing your own custom GraphQL requests and how to approach it. You may want to construct your own GraphQL request if you want to

  • retrieve only a subset of the data to reduce data transfer
  • retrieve nested objects at a depth that you choose
  • combine multiple operations into a single request
  • send custom headers to your AppSync endpoint

A GraphQL request is automatically generated for you when using AWSAPIPlugin with the existing workflow. For example, if you have a Todo model, a mutation request to save the Todo will look like this:

let todo = Todo(name: "my first todo", description: "todo description")
try await Amplify.API.mutate(request: .create(todo))

Underneath the covers, a request is generated with a GraphQL document and variables and sent to the AppSync service.

{
"query": "mutation createTodo($input: CreateTodoInput!) {
createTodo(input: $input) {
id
name
description
}
}",
"variables": "{
"input": {
"id": "[UNIQUE-ID]",
"name": "my first todo",
"description": "todo description"
}
}
}

The different parts of the document are described as follows

  • mutation - the operation type to be performed, other operation types are query and subscription
  • createTodo($input: CreateTodoInput!) - the name and input of the operation.
  • $input: CreateTodoInput! - the input of type CreateTodoInput! referencing the variables containing JSON input
  • createTodo(input: $input) - the mutation operation which takes a variable input from $input
  • the selection set containing id, name, and description are fields specified to be returned in the response

You can learn more about the structure of a request from GraphQL Query Language and AppSync documentation. To test out constructing your own requests, open the AppSync console using amplify console api and navigate to the Queries tab.

Subset of data

The selection set of the document specifies which fields are returned in the response. For example, if you are displaying a view of the Todo without the description, you can construct the document to omit the field. You can learn more about selection sets here.

query getTodo($id: ID!) {
getTodo(id: $id) {
id
name
}
}

The response data will look like this

{
"data": {
"getTodo": {
"id": "111",
"name": "my first todo"
}
}
}

First, create your own GraphQLRequest

extension GraphQLRequest {
static func getWithoutDescription(byId id: String) -> GraphQLRequest<Todo> {
let operationName = "getTodo"
let document = """
query getTodo($id: ID!) {
\(operationName)(id: $id) {
id
name
}
}
"""
return GraphQLRequest<Todo>(
document: document,
variables: ["id": id],
responseType: Todo.self,
decodePath: operationName
)
}
}

The decode path specifies which part of the response to deserialize to the responseType. You'll need to specify the operation name to deserialize the object at "data.getTodo" successfully into a Todo model.

Then, query for the Todo by a todo id

try await Amplify.API.query(request: .getWithoutDescription(byId: "[UNIQUE_ID]"))
// handle result

Custom Request Inputs

The GraphQLRequest's variables property takes in a JSON object. You can define this as an Encodable type and serialize it to a JSON object for the request.

Define the type that matches the expected input type from your GraphQL service.

struct CreateTodoInput: Encodable {
let id: String
let name: String
let description: String?
}

Then create the GraphQLRequest

import Foundation
extension GraphQLRequest {
static func createTodo(input: CreateTodoInput) throws -> GraphQLRequest<Todo> {
let operationName = "createTodo"
let document = """
query createTodo($input: CreateTodoInput!) {
\(operationName)(input: $input) {
id
name
description
}
}
"""
let data = try JSONEncoder().encode(input)
let jsonObject = try JSONSerialization.jsonObject(with: data, options: [])
return GraphQLRequest<Todo>(
document: document,
variables: ["input": jsonObject],
responseType: Todo.self,
decodePath: operationName
)
}
}

The input object is encoded as a data object and then serialized into the JSON object.

Nested Data

If you have a relational model, you can retrieve the nested object by creating a GraphQLRequest with a selection set containing the nested object's fields. For example, in this schema, the Post can contain multiple comments and notes.

enum PostStatus {
ACTIVE
INACTIVE
}
type Post @model {
id: ID!
title: String!
rating: Int!
status: PostStatus!
comments: [Comment] @hasMany(indexName: "byPost", fields: ["id"])
notes: [Note] @hasMany(indexName: "byNote", fields: ["id"])
}
type Comment @model {
id: ID!
postID: ID! @index(name: "byPost", sortKeyFields: ["content"])
post: Post! @belongsTo(fields: ["postID"])
content: String!
}
type Note @model {
id: ID!
postID: ID! @index(name: "byNote", sortKeyFields: ["content"])
post: Post! @belongsTo(fields: ["postID"])
content: String!
}

If you only want to retrieve the comments, without the notes, create a GraphQLRequest for the Post with nested fields only containing the comment fields.

extension GraphQLRequest {
static func getPostWithComments(byId id: String) -> GraphQLRequest<Post> {
let document = """
query getPost($id: ID!) {
getPost(id: $id) {
id
title
rating
status
comments {
items {
id
postID
content
}
}
}
}
"""
return GraphQLRequest<Post>(
document: document,
variables: ["id": id],
responseType: Post.self,
decodePath: "getPost"
)
}
}

Query with try await Amplify.API.query(request: .getPostWithComments(byId: "[POST_ID]")).

Combining multiple GraphQL operations in a single request

GraphQL allows you to run multiple GraphQL operations (queries/mutations) as part of a single network request from the client code. To perform multiple operations in a single request, you can place them within the same GraphQL document. For example, to retrieve a Post and a Todo:

extension GraphQLRequest {
static func get(byPostId postId: String, todoId: String) -> GraphQLRequest<JSONValue> {
let document = """
query get($postId: ID!, $todoId: ID!) {
getPost(id: $postId) {
id
title
rating
}
getTodo(id: $todoId) {
id
name
}
}
"""
return GraphQLRequest<JSONValue>(
document: document,
variables: [
"postId": postId,
"todoId": todoId
],
responseType: JSONValue.self
)
}
}

Notice here that JSONValue is used as the responseType. JSONValue is utility type that can be used to represent an arbitrary JSON response.

Once you have the response data in a JSONValue, you can access each object in the JSON structure by encoding it back to Data and decoding it to the expected Model.

do {
let response = try await Amplify.API.query(request: .get(byPostId: "[POST_ID]", todoId: "[TODO_ID]"))
switch response {
case .success(let data):
if let todoJSON = data.value(at: "getTodo"),
let todoData = try? JSONEncoder().encode(todoJSON),
let todo = try? JSONDecoder().decode(Todo.self, from: todoData) {
print(todo)
}
if let postJSON = data.value(at: "getPost"),
let postData = try? JSONEncoder().encode(postJSON),
let post = try? JSONDecoder().decode(Post.self, from: postData) {
print(post)
}
case .failure(let errorResponse):
print("Response contained errors: \(errorResponse)")
}
} catch let error as APIError {
print("Failed with error: \(error)")
} catch {
print("Unexpected error: \(error)")
}

If you have custom models or your Model has required fields that you have decided not to include in the response, you can create a Codable that conforms to the structure of the response data that you expect. From the previous example, the Codable would look like this

struct PostAndTodoResponse: Codable {
public let getTodo: Todo
public let getPost: Post
struct Todo: Codable {
public let id: String
public var name: String
}
struct Post: Codable {
public let id: String
public var title: String
public var rating: Int
}
}

Then use PostAndTodoResponse as the responseType of the GraphQLRequest instead of using JSONValue.

Combining multiple GraphQL requests on the client-side is different than server-side transaction support. To run multiple transactions as a batch operation refer to the Batch Put Custom Resolver example.

Adding Headers to Outgoing Requests

By default, the API plugin includes appropriate authorization headers on your outgoing requests. However, you may have an advanced use case where you wish to send additional request headers to AppSync.

If your API does not require any authorization or if you would like manipulate the request yourself, please refer to the Set authorization mode to NONE

To include custom headers in your outgoing requests, add an URLRequestInterceptor to the AWSAPIPlugin. Also specify the name of one of the APIs configured in your amplifyconfiguration.json file.

struct CustomInterceptor: URLRequestInterceptor {
func intercept(_ request: URLRequest) throws -> URLRequest {
var request = request
request.setValue("headerValue", forHTTPHeaderField: "headerKey")
return request
}
}
let apiPlugin = try AWSAPIPlugin()
try Amplify.addPlugin(apiPlugin)
try Amplify.configure()
try apiPlugin.add(interceptor: CustomInterceptor(), for: "yourApiName")