aws-amplify / aws-amplify/docs
AdminQueries usage in docs is wrong
- Dominant language
- MDX
- Stars
- 506
- Forks
- 1.1k
- Avg merge
- 3h 14m
- Merged PRs (30d)
- 1
Description
### Before opening, please confirm:
- [X] I have [searched for duplicate or closed issues](https://github.com/aws-amplify/amplify-js/issues?q=is%3Aissue+) and [discussions](https://github.com/aws-amplify/amplify-js/discussions).
- [X] I have read the guide for [submitting bug reports](https://github.com/aws-amplify/amplify-js/blob/main/CONTRIBUTING.md#bug-reports).
- [X] I have done my best to include a minimal, self-contained set of instructions for consistently reproducing the issue.
### JavaScript Framework
Next.js
### Amplify APIs
REST API
### Amplify Categories
api
### Environment information
System:
OS: Linux 5.15 Ubuntu 20.04.3 LTS (Focal Fossa)
CPU: (8) x64 Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz
Memory: 2.27 GB / 7.66 GB
Container: Yes
Shell: 5.0.17 - /bin/bash
Binaries:
Node: 18.18.2 - ~/.nvm/versions/node/v18.18.2/bin/node
npm: 9.8.1 - ~/.nvm/versions/node/v18.18.2/bin/npm
pnpm: 8.3.1 - ~/.local/share/pnpm/pnpm
npmPackages:
@ampproject/toolbox-optimizer: undefined ()
@aws-amplify/adapter-nextjs: ^1.0.5 => 1.0.5
@aws-amplify/adapter-nextjs/api: undefined ()
@aws-amplify/adapter-nextjs/data: undefined ()
@aws-amplify/ui-react: ^6.0.3 => 6.0.3
@aws-amplify/ui-react-internal: undefined ()
### Describe the bug
I think there is a bug either in the implementation or the documentation is wrong:
* it says the the AdminQueries should be called with queryStringParameters. But my query only works if I directly add ?groupname=admins to the apiName
* it says accesstoken.payload should be in headers. Payload is an object, accessToken.toString() creates a JWT what should be put into the headers.
* it says one await is enough for the response. In my code I need to await for the list, then for the response and then for the body json.
I am using a server component, so I tried `import { post, get } from 'aws-amplify/api/server';` but it does not work.
```
https://docs.amplify.aws/javascript/build-a-backend/auth/admin-actions/
async function listEditors(limit){
let apiName = 'AdminQueries';
let path = '/listUsersInGroup';
let options = {
queryStringParameters: {
"groupname": "Editors",
"limit": limit,
},
headers: {
'Content-Type' : 'application/json',
Authorization: `${(await fetchAuthSession()).tokens.accessToken.payload}`
}
}
const response = await get({apiName, path, options});
return response;
}
```
However this does not work, the error is that lambda returns a message "groupname is required".
```
'use server';
import { Amplify } from '@aws-amplify/core';
import { getAllUsers } from '@/src/helpers/apiAdmin';
import { fetchAuthSession } from 'aws-amplify/auth/server';
import config from '@/src/amplifyconfiguration.json';
import { post, get } from 'aws-amplify/api';
import { getCurrentUser } from '@aws-amplify/auth/server';
import { runWithAmplifyServerContext } from '@/utils/amplifyServerUtils';
import { cookies } from 'next/headers';
Amplify.configure(config, { ssr: true });
async function listAdmins(limit, payload) {
let apiName = 'AdminQueries';
let path = '/listUsersInGroup?groupname=admins';
let options = {
queryStringParameters: {
limit: limit,
groupname: 'admins',
},
headers: {
'Content-Type': 'application/json',
Authorization: `${payload}`,
},
};
const response = await get({
apiName,
path,
options,
});
return response;
}
export async function getUsers() {
const accessToken = await runWithAmplifyServerContext({
nextServerContext: { cookies },
operation: async (contextSpec) => {
try {
console.log('fetching access token');
const session = await fetchAuthSession(contextSpec);
return session.tokens.accessToken;
} catch (error) {
console.log('error', error);
return {};
}
},
});
const u = await listAdmins(10, accessToken.toString());
const users = await u.response;
const d = await users.body.json();
```
### Expected behavior
I think either the documentation needs to be fixed or examples improved, or the `get` function should be fixed.
### Reproduction steps
Create Admin Queries API and follow the documentation to call the listUsers in group.
### Code Snippet
```javascript
// Put your code below this line.
```
### Log output
```
// Put your logs below this line
```
### aws-exports.js
_No response_
### Manual configuration
_No response_
### Additional configuration
_No response_
### Mobile Device
_No response_
### Mobile Operating System
_No response_
### Mobile Browser
_No response_
### Mobile Browser Version
_No response_
### Additional information and screenshots
_No response_
Contributor guide
Assessment
This issue has not been assessed yet.