Skip to content

Commit

Permalink
bump types package and add recursive flatten (#12314)
Browse files Browse the repository at this point in the history
  • Loading branch information
iartemiev authored Oct 16, 2023
1 parent 179af4d commit 171516f
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 14 deletions.
14 changes: 7 additions & 7 deletions .github/workflows/callable-release-verification.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ jobs:
prebuild-samples-staging:
secrets: inherit
uses: ./.github/workflows/callable-prebuild-samples-staging.yml
e2e:
needs:
- prebuild-macos
- prebuild-ubuntu
- prebuild-samples-staging
secrets: inherit
uses: ./.github/workflows/callable-e2e-tests.yml
# e2e:
# needs:
# - prebuild-macos
# - prebuild-ubuntu
# - prebuild-samples-staging
# secrets: inherit
# uses: ./.github/workflows/callable-e2e-tests.yml
unit-tests:
needs:
- prebuild-ubuntu
Expand Down
118 changes: 117 additions & 1 deletion packages/api/__tests__/models/APIClient.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { normalizeMutationInput, initializeModel } from '../../src/APIClient';
import { normalizeMutationInput, flattenItems } from '../../src/APIClient';
import modelIntroSchema from '../assets/model-introspection';

describe('APIClient', () => {
Expand Down Expand Up @@ -39,3 +39,119 @@ describe('APIClient', () => {

test('initializeModel', () => {});
});

describe('flattenItems', () => {
test('no-op on get without relationships', () => {
const getResponse = { getPost: { id: 'myPost' } };

const expected = { getPost: { id: 'myPost' } };

const flattened = flattenItems(getResponse);

expect(flattened).toEqual(expected);
});

test('flatten list without relationships', () => {
const listResponse = {
listPost: { items: [{ id: 'myPost' }, { id: 'myPost2' }] },
};

const expected = {
listPost: [{ id: 'myPost' }, { id: 'myPost2' }],
};

const flattened = flattenItems(listResponse);

expect(flattened).toEqual(expected);
});

test('flatten list with relationships', () => {
const listResponse = {
listPosts: {
items: [
{
id: 'post1',
comments: {
items: [
{
id: 'comment1',
content: 'my comment 1',
meta: {
items: [{ id: 'meta1' }],
},
post: {
id: 'post1',
comments: {
items: [
{
id: 'comment1',
content: 'my comment 1',
meta: {
items: [{ id: 'meta1' }],
},
},
],
},
},
},
{
id: 'comment1',
content: 'my comment 1',
meta: {
items: [{ id: 'meta1' }],
},
},
],
},
},
],
},
};

const expected = {
listPosts: [
{
id: 'post1',
comments: [
{
id: 'comment1',
content: 'my comment 1',
meta: [
{
id: 'meta1',
},
],
post: {
id: 'post1',
comments: [
{
id: 'comment1',
content: 'my comment 1',
meta: [
{
id: 'meta1',
},
],
},
],
},
},
{
id: 'comment1',
content: 'my comment 1',
meta: [
{
id: 'meta1',
},
],
},
],
},
],
};

const flattened = flattenItems(listResponse);

expect(flattened).toEqual(expected);
});
});
2 changes: 1 addition & 1 deletion packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"dependencies": {
"@aws-amplify/api-graphql": "3.4.11",
"@aws-amplify/api-rest": "3.5.5",
"@aws-amplify/amplify-api-next-types-alpha": "^0.0.5",
"@aws-amplify/amplify-api-next-types-alpha": "^0.1.2",
"tslib": "^2.6.1"
},
"size-limit": [
Expand Down
4 changes: 3 additions & 1 deletion packages/api/src/API.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
buildGraphQLVariables,
graphQLOperationsInfo,
ModelOperation,
flattenItems,
} from './APIClient';
import type { ModelTypes } from '@aws-amplify/amplify-api-next-types-alpha';

Expand All @@ -26,6 +27,7 @@ const logger = new Logger('API');
* Use RestApi or GraphQLAPI to reduce your application bundle size
* Export Cloud Logic APIs
*/

export class APIClass extends InternalAPIClass {
public getModuleName() {
return 'API';
Expand Down Expand Up @@ -107,7 +109,7 @@ export class APIClass extends InternalAPIClass {
const [key] = Object.keys(res.data);

if (res.data[key].items) {
const flattenedResult = res.data[key].items;
const flattenedResult = flattenItems(res.data)[key];

// don't init if custom selection set
if (args?.selectionSet) {
Expand Down
26 changes: 26 additions & 0 deletions packages/api/src/APIClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,32 @@ const connectionType = {
BELONGS_TO: 'BELONGS_TO',
};

/**
*
* @param GraphQL response object
* @returns response object with `items` properties flattened
*/
export const flattenItems = (obj: Record<string, any>): Record<string, any> => {
const res: Record<string, any> = {};

Object.entries(obj).forEach(([prop, value]) => {
if (typeof value === 'object' && value !== null) {
if (value.items !== undefined) {
res[prop] = value.items.map((item: Record<string, any>) =>
flattenItems(item)
);
return;
}
res[prop] = flattenItems(value);
return;
}

res[prop] = value;
});

return res;
};

// TODO: this should accept single result to support CRUD methods; create helper for array/list
export function initializeModel(
client: any,
Expand Down
1 change: 1 addition & 0 deletions packages/api/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ export {
GraphQLQuery,
GraphQLSubscription,
} from '@aws-amplify/api-graphql';
export { SelectionSet } from '@aws-amplify/amplify-api-next-types-alpha';
8 changes: 4 additions & 4 deletions yarn.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 171516f

Please sign in to comment.