Closed paluka closed 3 years ago
Do I need to create a model or can I make a query like the below:
query: `query QueryAccount{
account{
id
name
email {
address
}
phone {
number
}
partners{
edges{
cursor
node{
tenants{
id
name
}
}
}
}
}
}`
Hi, @paluka Thanks for reaching out.
What you can do is:
Edit backend
button. You can see the provided command line something like "amplify pull --appId ***** --envName dev"amplify pull
. Upon completion, the amplify environment is in your local computer including the schema defined previously.amplify codegen models
and it should generate the correct AmplifyModels
files for you to use.Hope this helps you. Feel free to reach out if it doesn't work.
Hi @ruiguoamz, there are no Amplify apps in my AWS. I am using the Amplify iOS libraries to use "existing Cognito and other AWS resources. Connect to them from your app with the Amplify Libraries." See the attached image:
As @ruiguoamz investigates the right way to get codegen of your existing API into your project...
Do I need to create a model
You can use the Amplify API category without generating models by writing custom GraphQL queries. See the advanced workflows documentation for examples of using custom documents to query your backend.
Code-generated models provide a convenient way of working with API data, since Amplify understands how to query, mutate, and subscribe to models without having to write custom documents. Behind the scenes, Amplify is just converting those model-based calls to a GraphQL document using metadata stored in the model schema, saving you the work of writing the document and maintaining it as you evolve your GraphQL schema.
Thank you @palpatim. We are using Amazon API Gateway which goes to a Lambda function that acts as proxy for GraphQL
@palpatim @ruiguoamz Since I am using Amazon API Gateway, I've switched the AWS Amplify API plugin from APPSync GraphQL to Amazon API Gateway REST endpoint type, but I am getting http 401 response unauthorized.
let apiConfig = APICategoryConfiguration(plugins: [
"awsAPIPlugin": [
"<name-of-api>": [
"endpointType": "REST",
"endpoint": "https://<...>.execute-api.us-west-2.amazonaws.com/dev/graphql",
"region": amplifyAuthConfig.region,
"authorizationType": "AMAZON_COGNITO_USER_POOLS",
// AMAZON_COGNITO_USER_POOLS, AWS_IAM, OPENID_CONNECT, or API_KEY
]]])
The endpoint requires an Authentication Bearer token header value, so I grab it from:
var idToken = ""
Amplify.Auth.fetchAuthSession { result in
do {
let session = try result.get()
// Get cognito user pool token
if let cognitoTokenProvider = session as? AuthCognitoTokensProvider {
let tokens = try cognitoTokenProvider.getCognitoTokens().get()
idToken = tokens.idToken
} } }
then make the AWS API Gateway post http call to the lambda GraphQL proxy
Amplify.API.post(request: .getAccount(token: idToken)) { result in ...
with getAccount being
extension RESTRequest {
static func getAccount(token: String) -> RESTRequest {
let graphQLQuery = ...
return RESTRequest(headers: ["Authorization": "Bearer \(token)"], body: body.data(using: .utf8))
} }
The idToken that I am grabbing works in Postman Post request. Am I doing it correctly in iOS Swift?
If your API Gateway endpoint is set up with Cognito User Pools authorization, you should just be able to configure your plugin to use AMAZON_COGNITO_USER_POOLS
authorization and have it 'just work', as described here.
It sounds like your custom specification of the Authorization
header is being overwritten by the standard User Pools behavior that injects an appropriate Authorization
header when it detects that your API has an authorizationType of AMAZON_COGNITO_USER_POOLS
.
The authorizationType key also supports a value of NONE
, which will cause Amplify to not attempt to inject any authorization tokens into the request. That should allow your custom header to work as expected.
Finally: I see that we don't have the NONE
value documented in our API Authorization docs. That seems to be a miss that we'll need to rectify. I'll open a docs issue for that.
This issue is stale because it has been open for 14 days with no activity. Please, provide an update or it will be automatically closed in 7 days.
This issue is being automatically closed due to inactivity. If you believe it was closed by mistake, provide an update and re-open it.
Hi, in my iOS Swift app, I am configuring amplify manually using AWSCognitoAuthPlugin + AWSAPIPlugin plugins and their respective configuration variables. I want to be able to use GraphQL queries to grab data and the documentation states that I need to generate models for example ToDo model. Since I am configuring Amplify manually (inline code) to use existing AWS resources, how do I generate these models manually?
amplify codegen models
CLI command states that:and
amplify pull
states:I am able to get
Amplify.Auth.signIn
working with a proper username + password, so the authConfig is correct.