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

Unable to use existing appsync api without models #2574

Closed
1 task done
seyikayode opened this issue Aug 28, 2023 · 14 comments
Closed
1 task done

Unable to use existing appsync api without models #2574

seyikayode opened this issue Aug 28, 2023 · 14 comments
Labels
api pending-triage Issue is pending triage

Comments

@seyikayode
Copy link

seyikayode commented Aug 28, 2023

Before opening, please confirm:

Language and Async Model

Kotlin

Amplify Categories

GraphQL API

Gradle script dependencies

// Put output below this line

implementation("com.amplifyframework:core:2.12.0")

Environment information

# Put output below this line


Please include any relevant guides or documentation you're referencing

No response

Describe the bug

I followed the amplify android docs on using existing graphql api. After following this process, i wasn't able to use the appsync graphql api beacuse it doesn't have models. I couldn't find any part in the docs where an existing appsync graphql api was used and up till now i am unable to call any mutation or query because there i have not generated models to use. In the existing appsync api, model was not used.

Reproduction steps (if applicable)

No response

Code Snippet

// Put your code below this line.

Log output

// Put your logs below this line


amplifyconfiguration.json

No response

GraphQL Schema

// Put your schema below this line

Additional information and screenshots

No response

@ankpshah ankpshah added pending-triage Issue is pending triage api labels Aug 28, 2023
@ankpshah
Copy link
Contributor

Hi could you check if following Amplify documentation helps you to add existing AppSync endpoint for your usecase: https://docs.amplify.aws/lib/graphqlapi/existing-resources/q/platform/android/

@seyikayode
Copy link
Author

seyikayode commented Aug 30, 2023

Hi could you check if following Amplify documentation helps you to add existing AppSync endpoint for your usecase: https://docs.amplify.aws/lib/graphqlapi/existing-resources/q/platform/android/

I followed this but still wasn’t able to call the appsync api. I had to use custom request with the amplify GraphQLRequest where I parsed the schema as string and sent the request. So it works now.

@ankpshah
Copy link
Contributor

ankpshah commented Aug 31, 2023

Have you tried using amplify codegen models as given in docs here
This will create model for the schema.

@seyikayode
Copy link
Author

Have you tried using amplify codegen models as given in docs here
This will create model for the schema.

I have gotten it to work using the custom GraphQLRequest, the existing appsync api didn’t have model

@ankpshah
Copy link
Contributor

ankpshah commented Sep 1, 2023

Ok is there any open question that needs to be addressed?

@seyikayode
Copy link
Author

Ok is there any open question that needs to be addressed?

Not at all, thank you

@ankpshah ankpshah closed this as completed Sep 1, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Sep 1, 2023

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

@ndeka28
Copy link

ndeka28 commented Jan 22, 2024

@seyikayode Can you please paste an example how the custom query works, I am trying the same from the documentation, but gives me a null response. Your help will highly be appreciated, as I am completely blocked

@seyikayode
Copy link
Author

@seyikayode Can you please paste an example how the custom query works, I am trying the same from the documentation, but gives me a null response. Your help will highly be appreciated, as I am completely blocked

for android kotlin, i generated the queries with the schema.graphql file using amplify codegen. Below is an example of a generated query after running "amplify codegen add"

query GetUserDetails($id: String) {
getUserDetails(id: $id) {
userDetails {
id
name
address
contact
email
city
country
userId
createdAt
updatedAt
}
error
}
}

This generated query above, make it a string variable and it becomes...

val getUserDetailsQuery = "query GetUserDetails($id: String) {\n" +
" getUserDetails(id: $id) {\n" +
" userDetails {\n" +
" id\n" +
" name\n" +
" address\n" +
" contact\n" +
" email\n" +
" city\n" +
" country\n" +
" userId\n" +
" createdAt\n" +
" updatedAt\n" +
" }\n" +
" error\n" +
" }\n" +
"}"

After making it a string, write another function that contains the graphqlRequest which maps the id parameter and also makes use of the getUserDetailsQuery variable above (assuming it's kotlin)...

fun getUserDetailsRequest(id: String): GraphQLRequest {
return SimpleGraphQLRequest(
Queries.getUserDetailsQuery,
mapOf("id" to id),
String::class.java,
GsonVariablesSerializer()
)
}

To finally make the api request, use the Amplify.API.query together with getUserDetailsRequest above. example below...

suspend fun getUserDetails(id: String): GetUserDetailsResult {
return try {
val response = Amplify.API.query(getUserDetailsRequest(id))
val gson = Gson()
val graphqlResponse = gson.fromJson(response.data, GetUserDetailsGraphQLResponse::class.java)
GetUserDetailsResult.Success(graphqlResponse)
} catch (error: ApiException) {
GetUserDetailsResult.Failure(error)
}
}

from here, you will be able to get your result. I used Gson() to deserialize the data here and also used GetUserDetailsResult to declare the result type. You can customize yours.

@ndeka28
Copy link

ndeka28 commented Jan 23, 2024

Thanks a lot @seyikayode I will have a try now, probably i have done something wrong with Gson desrialization

@ndeka28
Copy link

ndeka28 commented Jan 23, 2024

@seyikayode I Get below error while trying the same

getUserDetails: NonRetryableException{message=OkHttp client request failed., cause=null, recoverySuggestion=Irrecoverable error}
2024-01-23 10:30:58.124 30207-30249 ProfileInstaller        com.nila.carwithcustomquery          D  

My code here:


suspend fun getUserDetails(id: String){
     try {
        val response = Amplify.API.query(getCar(id))
        val gson = Gson()
        val graphqlResponse = gson.fromJson(response.data, CarNew::class.java)

    } catch (error: ApiException) {
       
         Log.e("Nila", "getUserDetails: $error")
    }
}

private fun getCar(id: String) : GraphQLRequest<String> {
    val document = "query GetCarNew($id: String!) {\n" +
            "  getCarNew(id: $id) {\n" +
            "    id\n" +
            "    name\n" +
            "    __typename\n" +
            "  }\n" +
            "}"

    return SimpleGraphQLRequest(
        document,
        mapOf("id" to id),
        String::class.java,
        GsonVariablesSerializer()
    )
}


    // Amplify API and Datastore dependencies
    implementation("com.amplifyframework:core:2.14.6")
    implementation("com.amplifyframework:aws-api:2.14.6")
    implementation("com.amplifyframework:core-kotlin:2.14.6")

    // Support for Java 8 features
    coreLibraryDesugaring("com.android.tools:desugar_jdk_libs:1.1.5")

Do you know any reason for this ?

Thanks,
Nila

@seyikayode
Copy link
Author

seyikayode commented Jan 23, 2024

@seyikayode I Get below error while trying the same

getUserDetails: NonRetryableException{message=OkHttp client request failed., cause=null, recoverySuggestion=Irrecoverable error}
2024-01-23 10:30:58.124 30207-30249 ProfileInstaller        com.nila.carwithcustomquery          D  

My code here:


suspend fun getUserDetails(id: String){
     try {
        val response = Amplify.API.query(getCar(id))
        val gson = Gson()
        val graphqlResponse = gson.fromJson(response.data, CarNew::class.java)

    } catch (error: ApiException) {
       
         Log.e("Nila", "getUserDetails: $error")
    }
}

private fun getCar(id: String) : GraphQLRequest<String> {
    val document = "query GetCarNew($id: String!) {\n" +
            "  getCarNew(id: $id) {\n" +
            "    id\n" +
            "    name\n" +
            "    __typename\n" +
            "  }\n" +
            "}"

    return SimpleGraphQLRequest(
        document,
        mapOf("id" to id),
        String::class.java,
        GsonVariablesSerializer()
    )
}


    // Amplify API and Datastore dependencies
    implementation("com.amplifyframework:core:2.14.6")
    implementation("com.amplifyframework:aws-api:2.14.6")
    implementation("com.amplifyframework:core-kotlin:2.14.6")

    // Support for Java 8 features
    coreLibraryDesugaring("com.android.tools:desugar_jdk_libs:1.1.5")

Do you know any reason for this ?

Thanks, Nila

remove __typename from the document, add a slash before $id like I did in the example

@ndeka28
Copy link

ndeka28 commented Jan 23, 2024

@seyikayode That helps resolve the error, I can query and mutate now. Thanks a lot for your help

Another note, since Aws App sync sdk , already generates all queries nad mutations already and much easier to use. Is there any particular reason to use Amplify SDK ? Looks doing through Amplify is much complex than Aws App Sync SDK.

Thanks
Nilanjana

@seyikayode
Copy link
Author

@seyikayode That helps resolve the error, I can query and mutate now. Thanks a lot for your help

Another note, since Aws App sync sdk , already generates all queries nad mutations already and much easier to use. Is there any particular reason to use Amplify SDK ? Looks doing through Amplify is much complex than Aws App Sync SDK.

Thanks Nilanjana

Amplify is used for authentication with aws cognito, its also used for mobile development on aws. So its just better to call endpoints with it too instead of using another new library to make requests. Amplify can also be used to create the api, depends on the requirements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api pending-triage Issue is pending triage
Projects
None yet
Development

No branches or pull requests

3 participants