Skip to content

fix(data): add native client examples to connect HTTP datasource page - #8633

Open
VarshithaPamisetty wants to merge 2 commits into
mainfrom
fix/http-datasource-native-client-examples
Open

VarshithaPamisetty wants to merge 2 commits into
mainfrom
fix/http-datasource-native-client-examples

Conversation

@VarshithaPamisetty

Copy link
Copy Markdown
Contributor

Description of changes:

Step 5 of the connect HTTP datasource page served TypeScript client code (client.mutations.addPost, client.queries.getPost, in files titled App.tsx) to all nine platforms, because the section had no InlineFilter. Added Swift, Android, and Flutter sections using raw GraphQL, and scoped the existing TypeScript example to the JS platforms.

Each native section defines a type matching the Post return type, then a BlockSwitcher with the four custom operations, mirroring the structure of the parent custom business logic page. Steps 1 through 4 are unchanged: those snippets define the Gen 2 backend, which is TypeScript on every platform.

Checks

  • yarn build succeeds with no errors
  • Deployed the page's schema and resolvers to a sandbox and ran all twelve documents against the live AppSync endpoint; each is accepted and schema-validated, and the live Post type matches the types shown

Refs #7922

@VarshithaPamisetty VarshithaPamisetty self-assigned this Oct 26, 2026
@VarshithaPamisetty
VarshithaPamisetty marked this pull request as ready for review October 26, 2026 13:34
@VarshithaPamisetty
VarshithaPamisetty requested a review from a team as a code owner October 26, 2026 13:34
soberm
soberm previously approved these changes Oct 27, 2026
Comment on lines +453 to +455
struct AddPostResponse: Codable {
public let addPost: Post
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep Swift response properties at internal access

Post is declared with the default internal access level, so exposing it through the public addPost property violates Swift access control and makes the example fail to compile. The get, update, and delete response wrappers repeat the same pattern.

Suggested change
struct AddPostResponse: Codable {
public let addPost: Post
}
struct AddPostResponse: Codable {
let addPost: Post
}

@VarshithaPamisetty VarshithaPamisetty Oct 27, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't reproduce the compile failure. All five declarations type-check at top level:

swiftc -typecheck all.swift    exit=0    (Apple Swift 6.3.3)

Swift rejects a public member only when the type is more restrictive. Control confirms the check works:

private struct P: Codable { let a: String }
struct R: Codable { public let p: P }
-> error: property cannot be declared public because its type uses a private type

The same shape is merged at custom-business-logic/index.mdx:364 and type-checks too.

Leaving as is, since the example compiles as written.

Comment on lines +621 to +629
Amplify.API.mutate(
request,
{
var gson = Gson()
val response = gson.fromJson(it.data, AddPostResponse::class.java)
Log.i("MyAmplifyApp", "${response.addPost}")
},
{ Log.e("MyAmplifyApp", "Mutation failed", it) }
)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check Android GraphQL errors before parsing data

A successful HTTP exchange can invoke this callback with GraphQL errors and null data, but the example immediately parses and dereferences it.data. Resolver errors therefore turn into a null parse or dereference instead of being reported; all four Android examples repeat this handling.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in all four Android blocks.

Verified: AppSyncGraphQLOperation.java:165 calls onResponse.accept(...) unconditionally on HTTP success, and a live run returned errors=1 data={"getPost":null} on 9 of 12 documents.

Comment on lines +788 to +792
final response = await Amplify.API.mutate(request: request).response;

Map<String, dynamic> jsonMap = json.decode(response.data!);
final post = Post.fromJson(jsonMap['addPost']);
safePrint(post.author);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handle nullable Flutter GraphQL response data

GraphQLResponse.data is nullable and is commonly null when the response contains GraphQL errors, so response.data! throws before the errors can be inspected. The query, update, and delete snippets repeat the same force unwrap.

Suggested change
final response = await Amplify.API.mutate(request: request).response;
Map<String, dynamic> jsonMap = json.decode(response.data!);
final post = Post.fromJson(jsonMap['addPost']);
safePrint(post.author);
final response = await Amplify.API.mutate(request: request).response;
final data = response.data;
if (data == null) {
safePrint('errors: ${response.errors}');
} else {
final jsonMap = json.decode(data) as Map<String, dynamic>;
final post = Post.fromJson(jsonMap['addPost']);
safePrint(post.author);
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in all four, using hasErrors rather than the suggested null check.

Live AppSync returns data={"getPost":null} with errors non-empty, so if (data == null) never fires. Replaying that body against the suggestion:

Unhandled exception: type 'Null' is not a subtype of type 'Map<String, dynamic>'

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants