Skip to content

Commit

Permalink
feat(api-graphql): onCreate, onUpdate, onDelete methods on client mod…
Browse files Browse the repository at this point in the history
…els (#12370)

* stash

* checkpoint; working integ in sample app

* fixed tests

* updated merged-in unit tests for new owner field in selection sets
  • Loading branch information
svidgen authored Oct 20, 2023
1 parent 572a7b5 commit 3eb7748
Show file tree
Hide file tree
Showing 9 changed files with 649 additions and 29 deletions.
6 changes: 4 additions & 2 deletions packages/api-graphql/__tests__/APIClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ describe('flattenItems', () => {
items: {
id: '',
body: '',
owner: '',
createdAt: '',
updatedAt: '',
todoNotesId: '',
Expand All @@ -223,7 +224,8 @@ describe('flattenItems', () => {
test('it should generate default selection set', () => {
const selSet = generateSelectionSet(modelIntroSchema.models, 'Todo');

const expected = 'id name description createdAt updatedAt todoMetaId';
const expected =
'id name description createdAt updatedAt todoMetaId owner';

expect(selSet).toEqual(expected);
});
Expand Down Expand Up @@ -260,7 +262,7 @@ describe('flattenItems', () => {
]);

const expected =
'id name notes { items { id body createdAt updatedAt todoNotesId } }';
'id name notes { items { id body createdAt updatedAt todoNotesId owner } }';

expect(selSet).toEqual(expected);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
/**
* Generated from `./schema.ts` by samsara.
*
* Cognito fields etc. omitted.
*/
const amplifyConfig = {
aws_appsync_graphqlEndpoint: 'https://0.0.0.0/modeled/graphql',
aws_appsync_graphqlEndpoint: 'https://localhost/graphql',
aws_appsync_region: 'us-west-1',
aws_appsync_authenticationType: 'API_KEY',
aws_appsync_apiKey: 'some-api-key',
aws_appsync_apiKey: 'FAKE-KEY',
modelIntrospection: {
version: 1,
models: {
Expand Down Expand Up @@ -89,6 +94,25 @@ const amplifyConfig = {
type: 'model',
properties: {},
},
{
type: 'auth',
properties: {
rules: [
{
allow: 'public',
provider: 'apiKey',
operations: ['create', 'update', 'delete', 'read'],
},
{
provider: 'userPools',
ownerField: 'owner',
allow: 'owner',
identityClaim: 'cognito:username',
operations: ['create', 'update', 'delete', 'read'],
},
],
},
},
],
primaryKeyInfo: {
isCustomPrimaryKey: false,
Expand Down Expand Up @@ -157,6 +181,25 @@ const amplifyConfig = {
type: 'model',
properties: {},
},
{
type: 'auth',
properties: {
rules: [
{
allow: 'public',
provider: 'apiKey',
operations: ['create', 'update', 'delete', 'read'],
},
{
provider: 'userPools',
ownerField: 'owner',
allow: 'owner',
identityClaim: 'cognito:username',
operations: ['create', 'update', 'delete', 'read'],
},
],
},
},
],
primaryKeyInfo: {
isCustomPrimaryKey: false,
Expand Down Expand Up @@ -205,6 +248,295 @@ const amplifyConfig = {
type: 'model',
properties: {},
},
{
type: 'auth',
properties: {
rules: [
{
allow: 'public',
provider: 'apiKey',
operations: ['create', 'update', 'delete', 'read'],
},
{
provider: 'userPools',
ownerField: 'owner',
allow: 'owner',
identityClaim: 'cognito:username',
operations: ['create', 'update', 'delete', 'read'],
},
],
},
},
],
primaryKeyInfo: {
isCustomPrimaryKey: false,
primaryKeyFieldName: 'id',
sortKeyFieldNames: [],
},
},
ThingWithCustomerOwnerField: {
name: 'ThingWithCustomerOwnerField',
fields: {
id: {
name: 'id',
isArray: false,
type: 'ID',
isRequired: true,
attributes: [],
},
customField: {
name: 'customField',
isArray: false,
type: 'String',
isRequired: false,
attributes: [],
},
description: {
name: 'description',
isArray: false,
type: 'String',
isRequired: false,
attributes: [],
},
createdAt: {
name: 'createdAt',
isArray: false,
type: 'AWSDateTime',
isRequired: false,
attributes: [],
isReadOnly: true,
},
updatedAt: {
name: 'updatedAt',
isArray: false,
type: 'AWSDateTime',
isRequired: false,
attributes: [],
isReadOnly: true,
},
},
syncable: true,
pluralName: 'ThingWithCustomerOwnerFields',
attributes: [
{
type: 'model',
properties: {},
},
{
type: 'key',
properties: {
fields: ['id'],
},
},
{
type: 'auth',
properties: {
rules: [
{
provider: 'userPools',
ownerField: 'customField',
allow: 'owner',
identityClaim: 'cognito:username',
operations: ['create', 'update', 'delete', 'read'],
},
],
},
},
],
primaryKeyInfo: {
isCustomPrimaryKey: false,
primaryKeyFieldName: 'id',
sortKeyFieldNames: [],
},
},
ThingWithOwnerFieldSpecifiedInModel: {
name: 'ThingWithOwnerFieldSpecifiedInModel',
fields: {
id: {
name: 'id',
isArray: false,
type: 'ID',
isRequired: true,
attributes: [],
},
name: {
name: 'name',
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: false,
attributes: [],
isReadOnly: true,
},
updatedAt: {
name: 'updatedAt',
isArray: false,
type: 'AWSDateTime',
isRequired: false,
attributes: [],
isReadOnly: true,
},
},
syncable: true,
pluralName: 'ThingWithOwnerFieldSpecifiedInModels',
attributes: [
{
type: 'model',
properties: {},
},
{
type: 'key',
properties: {
fields: ['id'],
},
},
{
type: 'auth',
properties: {
rules: [
{
provider: 'userPools',
ownerField: 'owner',
allow: 'owner',
identityClaim: 'cognito:username',
operations: ['create', 'update', 'delete', 'read'],
},
],
},
},
],
primaryKeyInfo: {
isCustomPrimaryKey: false,
primaryKeyFieldName: 'id',
sortKeyFieldNames: [],
},
},
ThingWithAPIKeyAuth: {
name: 'ThingWithAPIKeyAuth',
fields: {
id: {
name: 'id',
isArray: false,
type: 'ID',
isRequired: true,
attributes: [],
},
description: {
name: 'description',
isArray: false,
type: 'String',
isRequired: false,
attributes: [],
},
createdAt: {
name: 'createdAt',
isArray: false,
type: 'AWSDateTime',
isRequired: false,
attributes: [],
isReadOnly: true,
},
updatedAt: {
name: 'updatedAt',
isArray: false,
type: 'AWSDateTime',
isRequired: false,
attributes: [],
isReadOnly: true,
},
},
syncable: true,
pluralName: 'ThingWithAPIKeyAuths',
attributes: [
{
type: 'model',
properties: {},
},
{
type: 'key',
properties: {
fields: ['id'],
},
},
{
type: 'auth',
properties: {
rules: [
{
allow: 'public',
provider: 'apiKey',
operations: ['create', 'update', 'delete', 'read'],
},
],
},
},
],
primaryKeyInfo: {
isCustomPrimaryKey: false,
primaryKeyFieldName: 'id',
sortKeyFieldNames: [],
},
},
ThingWithoutExplicitAuth: {
name: 'ThingWithoutExplicitAuth',
fields: {
id: {
name: 'id',
isArray: false,
type: 'ID',
isRequired: true,
attributes: [],
},
description: {
name: 'description',
isArray: false,
type: 'String',
isRequired: false,
attributes: [],
},
createdAt: {
name: 'createdAt',
isArray: false,
type: 'AWSDateTime',
isRequired: false,
attributes: [],
isReadOnly: true,
},
updatedAt: {
name: 'updatedAt',
isArray: false,
type: 'AWSDateTime',
isRequired: false,
attributes: [],
isReadOnly: true,
},
},
syncable: true,
pluralName: 'ThingWithoutExplicitAuths',
attributes: [
{
type: 'model',
properties: {},
},
{
type: 'key',
properties: {
fields: ['id'],
},
},
],
primaryKeyInfo: {
isCustomPrimaryKey: false,
Expand Down
Loading

0 comments on commit 3eb7748

Please sign in to comment.