简体   繁体   中英

AWS Amplify: how to use API_KEY for test and development purpose

I am using amplify for my project. I have enabled both cognito and API_KEY authorization and added following function to use instead of 'graphqlOperation' of amplify-cli

function graphqlOperationWithAPIKey(query, variables) {
    return {query: query, variables: variables, authMode: 'API_KEY'}
}

And I doing the call:

const response = await API.graphql(graphqlOperationWithAPIKey(mutations.createContract, {input: mockContract}))

However I am getting the following error:

Failed: Object {
  "data": Object {
    "createContract": null,
  },
  "errors": Array [
    Object {
      "data": [Object],
      "errorInfo": [Object],
      "errorType": "Unauthorized",
      "locations": [Array],
      "message": "Not Authorized to access createContract on type Mutation",
      "path": [Array],
    },
  ],
}

Here is my schema:

type Contract @model {
  id: ID!    
  rental: Car @connection

  from: AWSDate!
  to: AWSDate!
  tenants: [String!]! 
  status: ContractStatus!
}

Can someone help me to get this work?

I don't know whether is bug or not, the below works if I add it to schema, but @aws_key doe not work (even according to doc should work as well)

@auth (
  rules: [
    { allow: public, provider: apiKey }
  ]
) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM