Skip to content

Commit fc7cdc5

Browse files
author
Dane Pilcher
authored
Merge pull request #889 from aws-amplify/main
Prep release
2 parents f268f8f + 648f2ed commit fc7cdc5

File tree

5 files changed

+508
-14
lines changed

5 files changed

+508
-14
lines changed

packages/appsync-modelgen-plugin/src/__tests__/utils/process-connections.test.ts

Lines changed: 99 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ describe('process connection', () => {
5656

5757
it('should return HAS_MANY for Post.comments field connection info', () => {
5858
const commentsField = modelMap.Post.fields[0];
59-
const connectionInfo = (processConnections(commentsField, modelMap.Post, modelMap) as any) as CodeGenFieldConnectionHasMany;
59+
const connectionInfo = (processConnections(commentsField, modelMap.Post, modelMap, true) as any) as CodeGenFieldConnectionHasMany;
6060
expect(connectionInfo).toBeDefined();
6161

6262
expect(connectionInfo.kind).toEqual(CodeGenConnectionType.HAS_MANY);
@@ -66,7 +66,7 @@ describe('process connection', () => {
6666

6767
it('should return BELONGS_TO for Comment.post field connection info', () => {
6868
const postField = modelMap.Comment.fields[0];
69-
const connectionInfo = (processConnections(postField, modelMap.Comment, modelMap) as any) as CodeGenFieldConnectionBelongsTo;
69+
const connectionInfo = (processConnections(postField, modelMap.Comment, modelMap, true) as any) as CodeGenFieldConnectionBelongsTo;
7070
expect(connectionInfo).toBeDefined();
7171

7272
expect(connectionInfo.kind).toEqual(CodeGenConnectionType.BELONGS_TO);
@@ -120,7 +120,7 @@ describe('process connection', () => {
120120

121121
it('should return HAS_ONE Person.license field', () => {
122122
const licenseField = modelMap.Person.fields[0];
123-
const connectionInfo = (processConnections(licenseField, modelMap.Person, modelMap) as any) as CodeGenFieldConnectionHasOne;
123+
const connectionInfo = (processConnections(licenseField, modelMap.Person, modelMap, true) as any) as CodeGenFieldConnectionHasOne;
124124
expect(connectionInfo).toBeDefined();
125125
expect(connectionInfo.kind).toEqual(CodeGenConnectionType.HAS_ONE);
126126
expect(connectionInfo.associatedWith).toEqual(modelMap.License.fields[0]);
@@ -130,7 +130,7 @@ describe('process connection', () => {
130130

131131
it('should return BELONGS_TO License.person field', () => {
132132
const personField = modelMap.License.fields[0];
133-
const connectionInfo = (processConnections(personField, modelMap.License, modelMap) as any) as CodeGenFieldConnectionBelongsTo;
133+
const connectionInfo = (processConnections(personField, modelMap.License, modelMap, true) as any) as CodeGenFieldConnectionBelongsTo;
134134
expect(connectionInfo).toBeDefined();
135135
expect(connectionInfo.kind).toEqual(CodeGenConnectionType.BELONGS_TO);
136136
expect(connectionInfo.isConnectingFieldAutoCreated).toEqual(true);
@@ -141,9 +141,98 @@ describe('process connection', () => {
141141
// Make person field optional
142142
personField.isNullable = true;
143143
expect(() => {
144-
processConnections(personField, modelMap.License, modelMap);
144+
processConnections(personField, modelMap.License, modelMap, true);
145145
}).toThrowError('DataStore does not support 1 to 1 connection with both sides of connection as optional field');
146146
});
147+
148+
describe('Uni-directional connection', () => {
149+
const schema = /* GraphQL */ `
150+
type User @model {
151+
id: ID!
152+
}
153+
type Session @model {
154+
id: ID!
155+
sessionUserId: ID!
156+
user: User! @connection(fields: ["sessionUserId"])
157+
}
158+
`;
159+
160+
const modelMap: CodeGenModelMap = {
161+
User: {
162+
name: 'User',
163+
type: 'model',
164+
directives: [],
165+
fields: [
166+
{
167+
type: 'ID',
168+
isNullable: false,
169+
isList: false,
170+
name: 'id',
171+
directives: [],
172+
},
173+
],
174+
},
175+
Session: {
176+
name: 'Session',
177+
type: 'model',
178+
directives: [],
179+
fields: [
180+
{
181+
type: 'ID',
182+
isNullable: false,
183+
isList: false,
184+
name: 'id',
185+
directives: [],
186+
},
187+
{
188+
type: 'ID',
189+
isNullable: false,
190+
isList: false,
191+
name: 'sessionUserId',
192+
directives: [],
193+
},
194+
{
195+
type: 'User',
196+
isNullable: false,
197+
isList: false,
198+
name: 'user',
199+
directives: [{ name: 'connection', arguments: { fields: ['sessionUserId'] } }],
200+
},
201+
],
202+
},
203+
};
204+
205+
it('uni-directional One:One connection with required field and datastore is not enabled', () => {
206+
const connectionInfo = (processConnections(modelMap.Session.fields[2], modelMap.User, modelMap, false) as any) as CodeGenFieldConnectionBelongsTo;
207+
expect(connectionInfo).toBeDefined();
208+
expect(connectionInfo.kind).toEqual(CodeGenConnectionType.HAS_ONE);
209+
expect(connectionInfo.isConnectingFieldAutoCreated).toEqual(false);
210+
});
211+
212+
it('uni-directional One:One connection with optional field and datastore is not enabled', () => {
213+
const field = { ...modelMap.Session.fields[2] };
214+
field.isNullable = true;
215+
const connectionInfo = (processConnections(field, modelMap.User, modelMap, false) as any) as CodeGenFieldConnectionBelongsTo;
216+
expect(connectionInfo).toBeDefined();
217+
expect(connectionInfo.kind).toEqual(CodeGenConnectionType.HAS_ONE);
218+
expect(connectionInfo.isConnectingFieldAutoCreated).toEqual(false);
219+
});
220+
221+
it('uni-directional One:One connection with required field and datastore is enabled', () => {
222+
expect(() => {
223+
processConnections(modelMap.Session.fields[2], modelMap.User, modelMap, true);
224+
}).toThrowError('DataStore does not support 1 to 1 connection with both sides of connection as optional field');
225+
});
226+
227+
it('uni-directional One:One connection with optional field and datastore is enabled', () => {
228+
const field = { ...modelMap.Session.fields[2] };
229+
field.isNullable = true;
230+
const connectionInfo = (processConnections(field, modelMap.User, modelMap, true) as any) as CodeGenFieldConnectionBelongsTo;
231+
expect(connectionInfo).toBeDefined();
232+
expect(connectionInfo.kind).toEqual(CodeGenConnectionType.HAS_ONE);
233+
expect(connectionInfo.isConnectingFieldAutoCreated).toEqual(false);
234+
});
235+
});
147236
});
148237
});
149238
describe('Uni-directional connection (unnamed connection)', () => {
@@ -192,7 +281,7 @@ describe('process connection', () => {
192281

193282
it('should return HAS_MANY for Post.comments', () => {
194283
const commentsField = modelMap.Post.fields[0];
195-
const connectionInfo = (processConnections(commentsField, modelMap.Post, modelMap) as any) as CodeGenFieldConnectionHasMany;
284+
const connectionInfo = (processConnections(commentsField, modelMap.Post, modelMap, true) as any) as CodeGenFieldConnectionHasMany;
196285
expect(connectionInfo).toBeDefined();
197286

198287
expect(connectionInfo.kind).toEqual(CodeGenConnectionType.HAS_MANY);
@@ -208,7 +297,7 @@ describe('process connection', () => {
208297

209298
it('should return BELONGS_TO for Comment.post', () => {
210299
const commentsField = modelMap.Comment.fields[0];
211-
const connectionInfo = (processConnections(commentsField, modelMap.Comment, modelMap) as any) as CodeGenFieldConnectionBelongsTo;
300+
const connectionInfo = (processConnections(commentsField, modelMap.Comment, modelMap, true) as any) as CodeGenFieldConnectionBelongsTo;
212301
expect(connectionInfo).toBeDefined();
213302

214303
expect(connectionInfo.kind).toEqual(CodeGenConnectionType.BELONGS_TO);
@@ -280,12 +369,12 @@ describe('process connection', () => {
280369

281370
it('should not throw error if connection directive has keyName', () => {
282371
const commentsField = modelMap.Post.fields[0];
283-
expect(() => processConnections(commentsField, modelMap.Post, modelMap)).not.toThrowError();
372+
expect(() => processConnections(commentsField, modelMap.Post, modelMap, true)).not.toThrowError();
284373
});
285374

286375
it('should support connection with @key on BELONGS_TO side', () => {
287376
const postField = modelMap.Comment.fields[2];
288-
const connectionInfo = (processConnections(postField, modelMap.Post, modelMap) as any) as CodeGenFieldConnectionBelongsTo;
377+
const connectionInfo = (processConnections(postField, modelMap.Post, modelMap, true) as any) as CodeGenFieldConnectionBelongsTo;
289378
expect(connectionInfo).toBeDefined();
290379
expect(connectionInfo.kind).toEqual(CodeGenConnectionType.BELONGS_TO);
291380
expect(connectionInfo.targetName).toEqual(modelMap.Comment.fields[0].name);
@@ -462,4 +551,4 @@ describe('process connection', () => {
462551
expect(getConnectedField(subordinateField, employeeModel, employeeModel)).toEqual(supervisorField);
463552
});
464553
});
465-
});
554+
});

0 commit comments

Comments
 (0)