Skip to content

Commit

Permalink
fix(api-graphql): deep multi-level selectionSet resolution (#12701)
Browse files Browse the repository at this point in the history
  • Loading branch information
erickriva authored Dec 21, 2023
1 parent 1477488 commit 4a3a756
Show file tree
Hide file tree
Showing 5 changed files with 452 additions and 37 deletions.
26 changes: 26 additions & 0 deletions packages/api-graphql/__tests__/APIClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,32 @@ describe('flattenItems', () => {

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

test('mix of related and non-related fields in a nested model creates a nested object with all necessary fields', () => {
const selSet = customSelectionSetToIR(
modelIntroSchema.models,
'CommunityPost',
['poll.question', 'poll.answers.answer', 'poll.answers.votes.id']
);

const expected = {
poll: {
question: '',
answers: {
items: {
answer: '',
votes: {
items: {
id: '',
},
},
},
},
},
};

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

describe('generateSelectionSet', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,342 @@ const amplifyConfig = {
sortKeyFieldNames: ['cpk_sort_key'],
},
},

//

CommunityPoll: {
name: 'CommunityPoll',
fields: {
id: {
name: 'id',
isArray: false,
type: 'ID',
isRequired: true,
attributes: [],
},
question: {
name: 'question',
isArray: false,
type: 'String',
isRequired: true,
attributes: [],
},
answers: {
name: 'answers',
isArray: true,
type: {
model: 'CommunityPollAnswer',
},
isRequired: true,
attributes: [],
isArrayNullable: false,
association: {
connectionType: 'HAS_MANY',
associatedWith: ['communityPollAnswersId'],
},
},
createdAt: {
name: 'createdAt',
isArray: false,
type: 'AWSDateTime',
isRequired: true,
attributes: [],
},
updatedAt: {
name: 'updatedAt',
isArray: false,
type: 'AWSDateTime',
isRequired: true,
attributes: [],
},
},
syncable: true,
pluralName: 'CommunityPolls',
attributes: [
{
type: 'model',
properties: {},
},
{
type: 'key',
properties: {
fields: ['id'],
},
},
{
type: 'auth',
properties: {
rules: [
{
allow: 'public',
provider: 'apiKey',
},
],
},
},
],
primaryKeyInfo: {
isCustomPrimaryKey: false,
primaryKeyFieldName: 'id',
sortKeyFieldNames: [],
},
},
CommunityPollAnswer: {
name: 'CommunityPollAnswer',
fields: {
id: {
name: 'id',
isArray: false,
type: 'ID',
isRequired: true,
attributes: [],
},
answer: {
name: 'answer',
isArray: false,
type: 'String',
isRequired: true,
attributes: [],
},
votes: {
name: 'votes',
isArray: true,
type: {
model: 'CommunityPollVote',
},
isRequired: true,
attributes: [],
isArrayNullable: false,
association: {
connectionType: 'HAS_MANY',
associatedWith: ['communityPollAnswerVotesId'],
},
},
communityPollAnswersId: {
name: 'communityPollAnswersId',
isArray: false,
type: 'ID',
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: 'CommunityPollAnswers',
attributes: [
{
type: 'model',
properties: {},
},
{
type: 'key',
properties: {
fields: ['id'],
},
},
{
type: 'key',
properties: {
name: 'gsi-CommunityPoll.answers',
fields: ['communityPollAnswersId'],
},
},
{
type: 'auth',
properties: {
rules: [
{
allow: 'public',
provider: 'apiKey',
},
],
},
},
],
primaryKeyInfo: {
isCustomPrimaryKey: false,
primaryKeyFieldName: 'id',
sortKeyFieldNames: [],
},
},
CommunityPollVote: {
name: 'CommunityPollVote',
fields: {
id: {
name: 'id',
isArray: false,
type: 'ID',
isRequired: true,
attributes: [],
},
communityPollAnswerVotesId: {
name: 'communityPollAnswerVotesId',
isArray: false,
type: 'ID',
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: 'CommunityPollVotes',
attributes: [
{
type: 'model',
properties: {},
},
{
type: 'key',
properties: {
fields: ['id'],
},
},
{
type: 'key',
properties: {
name: 'gsi-CommunityPollAnswer.votes',
fields: ['communityPollAnswerVotesId'],
},
},
{
type: 'auth',
properties: {
rules: [
{
provider: 'userPools',
ownerField: 'owner',
allow: 'owner',
identityClaim: 'cognito:username',
operations: ['create', 'update', 'delete', 'read'],
},
],
},
},
],
primaryKeyInfo: {
isCustomPrimaryKey: false,
primaryKeyFieldName: 'id',
sortKeyFieldNames: [],
},
},
CommunityPost: {
name: 'CommunityPost',
fields: {
id: {
name: 'id',
isArray: false,
type: 'ID',
isRequired: true,
attributes: [],
},
content: {
name: 'content',
isArray: false,
type: 'String',
isRequired: false,
attributes: [],
},
poll: {
name: 'poll',
isArray: false,
type: {
model: 'CommunityPoll',
},
isRequired: false,
attributes: [],
association: {
connectionType: 'HAS_ONE',
associatedWith: ['id'],
targetNames: ['communityPostPollId'],
},
},

communityPostPollId: {
name: 'communityPostPollId',
isArray: false,
type: 'ID',
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: 'CommunityPosts',
attributes: [
{
type: 'model',
properties: {},
},
{
type: 'key',
properties: {
fields: ['id'],
},
},
{
type: 'auth',
properties: {
rules: [
{
allow: 'public',
provider: 'apiKey',
},
],
},
},
],
primaryKeyInfo: {
isCustomPrimaryKey: false,
primaryKeyFieldName: 'id',
sortKeyFieldNames: [],
},
},
},
enums: {
Status: {
Expand Down
Loading

0 comments on commit 4a3a756

Please sign in to comment.