Skip to content

Commit

Permalink
fix(graphql): change aggregate field type to enum (#8484)
Browse files Browse the repository at this point in the history
* fix(graphql): change aggregate field type to enum

* update E2E test
  • Loading branch information
sundersc committed Oct 21, 2021
1 parent 12c1afa commit 78d1e43
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 46 deletions.
Expand Up @@ -101,12 +101,6 @@ input SearchableEmployeeSortInput {
direction: SearchableSortDirection
}
input SearchableEmployeeAggregationInput {
name: String!
type: SearchableAggregateType!
field: String!
}
enum SearchableAggregateType {
terms
avg
Expand All @@ -115,6 +109,18 @@ enum SearchableAggregateType {
sum
}
enum SearchableEmployeeAggregateField {
id
firstName
lastName
}
input SearchableEmployeeAggregationInput {
name: String!
type: SearchableAggregateType!
field: SearchableEmployeeAggregateField!
}
type SearchableEmployeeConnection {
items: [Employee]
nextToken: String
Expand Down Expand Up @@ -396,12 +402,6 @@ input SearchablePostSortInput {
direction: SearchableSortDirection
}
input SearchablePostAggregationInput {
name: String!
type: SearchableAggregateType!
field: String!
}
enum SearchableAggregateType {
terms
avg
Expand All @@ -410,6 +410,19 @@ enum SearchableAggregateType {
sum
}
enum SearchablePostAggregateField {
id
title
createdAt
updatedAt
}
input SearchablePostAggregationInput {
name: String!
type: SearchableAggregateType!
field: SearchablePostAggregateField!
}
type SearchablePostConnection {
items: [Post]
nextToken: String
Expand Down Expand Up @@ -1277,12 +1290,6 @@ input SearchablePostSortInput {
direction: SearchableSortDirection
}
input SearchablePostAggregationInput {
name: String!
type: SearchableAggregateType!
field: String!
}
enum SearchableAggregateType {
terms
avg
Expand All @@ -1291,6 +1298,19 @@ enum SearchableAggregateType {
sum
}
enum SearchablePostAggregateField {
id
title
createdAt
updatedAt
}
input SearchablePostAggregationInput {
name: String!
type: SearchableAggregateType!
field: SearchablePostAggregateField!
}
type SearchablePostConnection {
items: [Post]
nextToken: String
Expand Down Expand Up @@ -1345,10 +1365,15 @@ input SearchableUserSortInput {
direction: SearchableSortDirection
}
enum SearchableUserAggregateField {
id
name
}
input SearchableUserAggregationInput {
name: String!
type: SearchableAggregateType!
field: String!
field: SearchableUserAggregateField!
}
type SearchableUserConnection {
Expand Down Expand Up @@ -1641,12 +1666,6 @@ input SearchablePostSortInput {
direction: SearchableSortDirection
}
input SearchablePostAggregationInput {
name: String!
type: SearchableAggregateType!
field: String!
}
enum SearchableAggregateType {
terms
avg
Expand All @@ -1655,6 +1674,19 @@ enum SearchableAggregateType {
sum
}
enum SearchablePostAggregateField {
id
title
createdAt
updatedAt
}
input SearchablePostAggregationInput {
name: String!
type: SearchableAggregateType!
field: SearchablePostAggregateField!
}
type SearchablePostConnection {
items: [Post]
nextToken: String
Expand Down Expand Up @@ -1916,12 +1948,6 @@ input SearchablePostSortInput {
direction: SearchableSortDirection
}
input SearchablePostAggregationInput {
name: String!
type: SearchableAggregateType!
field: String!
}
enum SearchableAggregateType {
terms
avg
Expand All @@ -1930,6 +1956,19 @@ enum SearchableAggregateType {
sum
}
enum SearchablePostAggregateField {
id
title
createdAt
updatedAt
}
input SearchablePostAggregationInput {
name: String!
type: SearchableAggregateType!
field: SearchablePostAggregateField!
}
type SearchablePostConnection {
items: [Post]
nextToken: String
Expand Down Expand Up @@ -2206,12 +2245,6 @@ input SearchablePostSortInput {
direction: SearchableSortDirection
}
input SearchablePostAggregationInput {
name: String!
type: SearchableAggregateType!
field: String!
}
enum SearchableAggregateType {
terms
avg
Expand All @@ -2220,6 +2253,19 @@ enum SearchableAggregateType {
sum
}
enum SearchablePostAggregateField {
id
title
createdAt
updatedAt
}
input SearchablePostAggregationInput {
name: String!
type: SearchableAggregateType!
field: SearchablePostAggregateField!
}
type SearchablePostConnection {
items: [Post]
nextToken: String
Expand Down
Expand Up @@ -194,6 +194,28 @@ export function makeSearchableXSortableFieldsEnumObject(obj: ObjectTypeDefinitio
};
}

export function makeSearchableXAggregateFieldEnumObject(obj: ObjectTypeDefinitionNode): EnumTypeDefinitionNode {
const name = graphqlName(`Searchable${obj.name.value}AggregateField`);
assert(obj.fields);
const values: EnumValueDefinitionNode[] = obj.fields
.filter((field: FieldDefinitionNode) => isScalar(field.type))
.map((field: FieldDefinitionNode) => ({
kind: Kind.ENUM_VALUE_DEFINITION,
name: field.name,
directives: [],
}));

return {
kind: Kind.ENUM_TYPE_DEFINITION,
name: {
kind: 'Name',
value: name,
},
values,
directives: [],
};
}

export function makeSearchableXSortInputObject(obj: ObjectTypeDefinitionNode): InputObjectTypeDefinitionNode {
const name = graphqlName(`Searchable${obj.name.value}SortInput`);
return {
Expand Down Expand Up @@ -278,7 +300,7 @@ export function makeSearchableXAggregationInputObject(obj: ObjectTypeDefinitionN
{
kind: Kind.INPUT_VALUE_DEFINITION,
name: { kind: 'Name', value: 'field' },
type: makeNonNullType(makeNamedType('String')),
type: makeNonNullType(makeNamedType(`Searchable${obj.name.value}AggregateField`)),
directives: [],
},
],
Expand Down
Expand Up @@ -34,6 +34,7 @@ import {
makeSearchableSortDirectionEnumObject,
makeSearchableXFilterInputObject,
makeSearchableXSortableFieldsEnumObject,
makeSearchableXAggregateFieldEnumObject,
makeSearchableXSortInputObject,
makeSearchableXAggregationInputObject,
makeSearchableAggregateTypeEnumObject,
Expand Down Expand Up @@ -339,15 +340,21 @@ export class SearchableModelTransformer extends TransformerPluginBase {
ctx.output.addInput(searchableXSortableInputDirection);
}

if (!ctx.output.hasType(`Searchable${definition.name.value}AggregationInput`)) {
const searchableXAggregationInputDirection = makeSearchableXAggregationInputObject(definition);
ctx.output.addInput(searchableXAggregationInputDirection);
}

if (!ctx.output.hasType('SearchableAggregateType')) {
const searchableAggregateTypeEnum = makeSearchableAggregateTypeEnumObject();
ctx.output.addEnum(searchableAggregateTypeEnum);
}

if (!ctx.output.hasType(`Searchable${definition.name.value}AggregateField`)) {
const searchableXAggregationField = makeSearchableXAggregateFieldEnumObject(definition);
ctx.output.addEnum(searchableXAggregationField);
}

if (!ctx.output.hasType(`Searchable${definition.name.value}AggregationInput`)) {
const searchableXAggregationInput = makeSearchableXAggregationInputObject(definition);
ctx.output.addInput(searchableXAggregationInput);
}

}
}

Expand Down
Expand Up @@ -159,7 +159,7 @@ test('query for aggregate scalar results', async () => {
searchPosts(aggregates: [{
name: "Minimum",
type: min,
field: "ups"
field: ups
}]) {
aggregateItems {
name
Expand All @@ -185,7 +185,7 @@ test('query for aggregate bucket results', async () => {
searchPosts(aggregates: [{
name: "Terms",
type: terms,
field: "title"
field: title
}]) {
aggregateItems {
name
Expand Down Expand Up @@ -214,7 +214,7 @@ test('query for multiple aggregates', async () => {
searchPosts(aggregates: [{
name: "Minimum",
type: min,
field: "ups"
field: ups
},
{
name: "Terms",
Expand Down

0 comments on commit 78d1e43

Please sign in to comment.