Skip to content

Commit

Permalink
fix(graphql-key-transformer): support sortEnum and filterInput (#6033)
Browse files Browse the repository at this point in the history
add ModelSortDirection and ModelXFilterInput if they don't exist in the schema (ex. list query is
null)

re #6029
  • Loading branch information
SwaySway committed Dec 11, 2020
1 parent cc1a74b commit 1dd373b
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 3 deletions.
50 changes: 48 additions & 2 deletions packages/graphql-key-transformer/src/KeyTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,15 @@ import {
getDirectiveArgument,
isListType,
} from 'graphql-transformer-common';
import { makeModelConnectionType } from 'graphql-dynamodb-transformer';
import {
makeModelConnectionType,
makeModelSortDirectionEnumObject,
makeScalarFilterInputs,
makeEnumFilterInputObjects,
makeModelXFilterInputObject,
makeAttributeTypeEnum,
CONDITIONS_MINIMUM_VERSION,
} from 'graphql-dynamodb-transformer';
import {
ObjectTypeDefinitionNode,
FieldDefinitionNode,
Expand Down Expand Up @@ -390,10 +398,46 @@ export class KeyTransformer extends Transformer {
};
ctx.putType(queryType);

// Create Sort Direction if it doesn't exist
if (!this.typeExist('ModelSortDirection', ctx)) {
const modelSortDirection = makeModelSortDirectionEnumObject();
ctx.addEnum(modelSortDirection);
}
this.generateFilterInputs(ctx, definition);
this.generateModelXConnectionType(ctx, definition);
}
};

private generateFilterInputs(ctx: TransformerContext, def: ObjectTypeDefinitionNode): void {
const scalarFilters = makeScalarFilterInputs(this.supportsConditions(ctx));
for (const filter of scalarFilters) {
if (!this.typeExist(filter.name.value, ctx)) {
ctx.addInput(filter);
}
}

// Create the Enum filters
const enumFilters = makeEnumFilterInputObjects(def, ctx, this.supportsConditions(ctx));
for (const filter of enumFilters) {
if (!this.typeExist(filter.name.value, ctx)) {
ctx.addInput(filter);
}
}

// Create the ModelXFilterInput
const tableXQueryFilterInput = makeModelXFilterInputObject(def, ctx, this.supportsConditions(ctx));
if (!this.typeExist(tableXQueryFilterInput.name.value, ctx)) {
ctx.addInput(tableXQueryFilterInput);
}

if (this.supportsConditions(ctx)) {
const attributeTypeEnum = makeAttributeTypeEnum();
if (!this.typeExist(attributeTypeEnum.name.value, ctx)) {
ctx.addType(attributeTypeEnum);
}
}
}

private generateModelXConnectionType(ctx: TransformerContext, def: ObjectTypeDefinitionNode): void {
const tableXConnectionName = ModelResourceIDs.ModelConnectionTypeName(def.name.value);
if (this.typeExist(tableXConnectionName, ctx)) {
Expand All @@ -403,7 +447,6 @@ export class KeyTransformer extends Transformer {
// Create the ModelXConnection
const connectionType = blankObject(tableXConnectionName);
ctx.addObject(connectionType);

ctx.addObjectExtension(makeModelConnectionType(def.name.value));
}

Expand Down Expand Up @@ -692,6 +735,9 @@ export class KeyTransformer extends Transformer {
private typeExist(type: string, ctx: TransformerContext): boolean {
return Boolean(type in ctx.nodeMap);
}
private supportsConditions(context: TransformerContext) {
return context.getTransformerVersion() >= CONDITIONS_MINIMUM_VERSION;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { parse, InputObjectTypeDefinitionNode } from 'graphql';
import { parse, InputObjectTypeDefinitionNode, DefinitionNode, DocumentNode, Kind } from 'graphql';
import { GraphQLTransform, InvalidDirectiveError, SyncConfig, ConflictHandlerType } from 'graphql-transformer-core';
import { KeyTransformer } from '../KeyTransformer';
import { DynamoDBModelTransformer } from 'graphql-dynamodb-transformer';
Expand Down Expand Up @@ -248,3 +248,44 @@ test('Check KeyTransformer Resolver Code when sync enabled', () => {
expect(out).toBeDefined();
expect(out.resolvers).toMatchSnapshot();
});

test('Test that sort direction and filter input are generated if default list query does not exist', () => {
const validSchema = `
type Todo
@model(queries: { get: "getTodo" })
@key(
name: "byCreatedAt"
fields: ["createdAt"]
queryField: "byCreatedAt"
){
id: ID!
description: String
createdAt: AWSDateTime
}`;
const transformer = new GraphQLTransform({
transformers: [new DynamoDBModelTransformer(), new KeyTransformer()],
});
const out = transformer.transform(validSchema);
expect(out).toBeDefined();
const schema = parse(out.schema);
const sortDirection = schema.definitions.find(d => d.kind === 'EnumTypeDefinition' && d.name.value === 'ModelSortDirection');
expect(sortDirection).toBeDefined();
const stringInputType = getInputType(schema, 'ModelStringFilterInput');
expect(stringInputType).toBeDefined();
const booleanInputType = getInputType(schema, 'ModelBooleanFilterInput');
expect(booleanInputType).toBeDefined();
const intInputType = getInputType(schema, 'ModelIntFilterInput');
expect(intInputType).toBeDefined();
const floatInputType = getInputType(schema, 'ModelFloatFilterInput');
expect(floatInputType).toBeDefined();
const idInputType = getInputType(schema, 'ModelIDFilterInput');
expect(idInputType).toBeDefined();
const todoInputType = getInputType(schema, 'ModelTodoFilterInput');
expect(todoInputType).toBeDefined();
});

function getInputType(doc: DocumentNode, type: string): InputObjectTypeDefinitionNode | undefined {
return doc.definitions.find((def: DefinitionNode) => def.kind === Kind.INPUT_OBJECT_TYPE_DEFINITION && def.name.value === type) as
| InputObjectTypeDefinitionNode
| undefined;
}

0 comments on commit 1dd373b

Please sign in to comment.