Skip to content

Commit

Permalink
fix(api-graphql): wrong arguments for GET operation of a CPK model
Browse files Browse the repository at this point in the history
  • Loading branch information
HuiSF committed Feb 2, 2024
1 parent 08503c8 commit d2367f7
Show file tree
Hide file tree
Showing 3 changed files with 236 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
import { SchemaModel } from '@aws-amplify/core/internals/utils';

export const userSchemaModel: SchemaModel = {
name: 'User',
fields: {
userId: {
name: 'userId',
isArray: false,
type: 'ID',
isRequired: true,
attributes: [],
},
name: {
name: 'name',
isArray: false,
type: 'String',
isRequired: true,
attributes: [],
},
createdTodos: {
name: 'createdTodos',
isArray: true,
type: {
model: 'Todo',
},
isRequired: false,
attributes: [],
isArrayNullable: true,
association: {
connectionType: 'HAS_MANY' as any,
associatedWith: ['userCreatedTodosUserId'],
},
},
assignedTodos: {
name: 'assignedTodos',
isArray: true,
type: {
model: 'Todo',
},
isRequired: false,
attributes: [],
isArrayNullable: true,
association: {
connectionType: 'HAS_MANY' as any,
associatedWith: ['userAssignedTodosUserId'],
},
},
owner: {
name: 'owner',
isArray: false,
type: 'String',
isRequired: false,
attributes: [],
},
createdAt: {
name: 'createdAt',
isArray: false,
type: 'AWSDateTime',
isRequired: true,
attributes: [],
},
updatedAt: {
name: 'updatedAt',
isArray: false,
type: 'AWSDateTime',
isRequired: true,
attributes: [],
},
},
syncable: true,
pluralName: 'Users',
attributes: [
{
type: 'model',
properties: {},
},
{
type: 'key',
properties: {
fields: ['userId'],
},
},
{
type: 'auth',
properties: {
rules: [
{
provider: 'userPools',
ownerField: 'owner',
allow: 'owner',
identityClaim: 'cognito:username',
operations: ['create', 'update', 'delete', 'read'],
},
{
allow: 'public',
operations: ['read'],
},
],
},
},
],
primaryKeyInfo: {
isCustomPrimaryKey: true,
primaryKeyFieldName: 'userId',
sortKeyFieldNames: [],
},
};

export const productSchemaModel: SchemaModel = {
name: 'Product',
fields: {
sku: {
name: 'sku',
isArray: false,
type: 'String',
isRequired: true,
attributes: [],
},
factoryId: {
name: 'factoryId',
isArray: false,
type: 'String',
isRequired: true,
attributes: [],
},
warehouseId: {
name: 'warehouseId',
isArray: false,
type: 'String',
isRequired: true,
attributes: [],
},
description: {
name: 'description',
isArray: false,
type: 'String',
isRequired: false,
attributes: [],
},
owner: {
name: 'owner',
isArray: false,
type: 'String',
isRequired: false,
attributes: [],
},
createdAt: {
name: 'createdAt',
isArray: false,
type: 'AWSDateTime',
isRequired: true,
attributes: [],
},
updatedAt: {
name: 'updatedAt',
isArray: false,
type: 'AWSDateTime',
isRequired: true,
attributes: [],
},
},
syncable: true,
pluralName: 'Products',
attributes: [
{
type: 'model',
properties: {},
},
{
type: 'key',
properties: {
fields: ['sku', 'factoryId', 'warehouseId'],
},
},
{
type: 'auth',
properties: {
rules: [
{
provider: 'userPools',
ownerField: 'owner',
allow: 'owner',
identityClaim: 'cognito:username',
operations: ['create', 'update', 'delete', 'read'],
},
{
allow: 'public',
operations: ['read'],
},
],
},
},
],
primaryKeyInfo: {
isCustomPrimaryKey: true,
primaryKeyFieldName: 'sku',
sortKeyFieldNames: ['factoryId', 'warehouseId'],
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ import {
flattenItems,
generateSelectionSet,
customSelectionSetToIR,
} from '../src/internals/APIClient';
generateGraphQLDocument,
} from '../../src/internals/APIClient';

import config from './fixtures/modeled/amplifyconfiguration';
import config from '../fixtures/modeled/amplifyconfiguration';
import {
productSchemaModel,
userSchemaModel,
} from '../fixtures/schema-models/with-custom-primary-key/models';
const modelIntroSchema = config.modelIntrospection as ModelIntrospectionSchema;

describe('APIClient', () => {
Expand Down Expand Up @@ -38,7 +43,7 @@ describe('APIClient', () => {
const normalized = normalizeMutationInput(
note,
noteModelDef,
modelIntroSchema
modelIntroSchema,
);

expect(normalized).toEqual(expectedInput);
Expand Down Expand Up @@ -299,7 +304,7 @@ describe('flattenItems', () => {
const selSet = customSelectionSetToIR(
modelIntroSchema.models,
'CommunityPost',
['poll.question', 'poll.answers.answer', 'poll.answers.votes.id']
['poll.question', 'poll.answers.answer', 'poll.answers.votes.id'],
);

const expected = {
Expand Down Expand Up @@ -383,3 +388,30 @@ describe('flattenItems', () => {
});
});
});

describe('generateGraphQLDocument()', () => {
describe('for `READ` operation', () => {
const modelOperation = 'READ';
const mockModelDefinitions = {
User: userSchemaModel,
Product: productSchemaModel,
};

test.each([
['User', '$userId: ID!'],
['Product', '$sku: String!,$factoryId: String!,$warehouseId: String!'],
])(
'generates arguments for model %s to be %s',
(modelName, expectedArgs) => {
const document = generateGraphQLDocument(
mockModelDefinitions,
modelName,
modelOperation,
);

console.log(document);
expect(document.includes(expectedArgs)).toBe(true);
},
);
});
});
2 changes: 1 addition & 1 deletion packages/api-graphql/src/internals/APIClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ export function generateGraphQLDocument(
(graphQLArguments = isCustomPrimaryKey
? [primaryKeyFieldName, ...sortKeyFieldNames].reduce(
(acc: Record<string, any>, fieldName) => {
acc[fieldName] = fields[fieldName].type;
acc[fieldName] = `${fields[fieldName].type}!`;

return acc;
},
Expand Down

0 comments on commit d2367f7

Please sign in to comment.