public void GraphQLQueryGeneratorFromFields_Should_Throw_When_No_Matching_Argument_Found_Two_Arguments() { // Arrange var fieldGenerator = new GraphQLFieldBuilder(); var queryGenerator = new GraphQLQueryGeneratorFromFields(); var argument1 = new GraphQLQueryArgument("hello1Argument", "Value1"); var argument2 = new GraphQLQueryArgument("hello2Argument", "Value2"); // Act / Assert var exception = Assert.Throws <GraphQLArgumentVariableNotFoundException>(() => { // Get the query var query = queryGenerator.GenerateQuery(GraphQLOperationType.Query, fieldGenerator.GenerateSelectionSet(typeof(SampleQuery)), argument1, argument2); }); // Assert // Should contain two element Assert.Equal(2, exception.Arguments.Count()); // Argument should contain helloArgument and value Assert.Contains(argument1, exception.Arguments); Assert.Contains(argument2, exception.Arguments); }
public void Should_Throw_When_Duplicate_GraphQLUnionOrInterfaceAttributes_Defined() { // Arrange var fieldBuilder = new GraphQLFieldBuilder(); // Act / Assert Assert.Throws <GraphQLDuplicateTypeNameException>(() => { var fields = fieldBuilder.GenerateSelectionSet(typeof(Query4)); }); }
public void Test_Case_Hero() { // Arrange var expected = "{\"query\":\"query{hero{friends{name} name}}\"}"; var fieldBuilder = new GraphQLFieldBuilder(); var queryBuilder = new GraphQLQueryGeneratorFromFields(); // Act // Get fields var fields = fieldBuilder.GenerateSelectionSet(typeof(Query_Test_Case_Hero)); var actual = queryBuilder.GenerateQuery(GraphQLOperationType.Query, fields); // Assert Assert.Equal(expected, actual); }
public void Test_Single_Field_Should_Not_Generate_Seperate_Field_And_Alias() { // Arrange var expected = "{\"query\":\"query{field}\"}"; var fieldBuilder = new GraphQLFieldBuilder(); var queryBuilder = new GraphQLQueryGeneratorFromFields(); // Act // Get fields var fields = fieldBuilder.GenerateSelectionSet(typeof(Query_Test_Single_Field_Should_Not_Generate_Seperate_Field_And_Alias)); var actual = queryBuilder.GenerateQuery(GraphQLOperationType.Query, fields); // Assert Assert.Equal(expected, actual); }
public void Test_Variable_Complex_Dynamic_Type() { // Arrange var expected = "{\"query\":\"query($variableName:Type!){field(argumentName:$variableName)}\",\"variables\":{\"variableName\":{\"field1\":\"value1\",\"field2\":\"value2\"}}}"; var fieldBuilder = new GraphQLFieldBuilder(); var queryBuilder = new GraphQLQueryGeneratorFromFields(); // Act // Get fields var fields = fieldBuilder.GenerateSelectionSet(typeof(Query)); var actual = queryBuilder.GenerateQuery(GraphQLOperationType.Query, fields, new GraphQLQueryArgument("variableName", new { field1 = "value1", field2 = "value2" })); // Assert Assert.Equal(expected, actual); }
public void Property_With_UnionOrInterface_On_Property_Should_Be_Included_In_Fields() { // Arrange var fieldBuilder = new GraphQLFieldBuilder(); // Act var fields = fieldBuilder.GenerateSelectionSet(typeof(Query1)); // Assert Assert.Equal(1, fields.Count()); // Test target types Assert.Equal(1, fields.First().TargetTypes.Count); Assert.Equal("Obj", fields.First().TargetTypes.First().Key); Assert.Equal(typeof(Object), fields.First().TargetTypes.First().Value.Type); // Test default type Assert.Equal(typeof(String), fields.First().BaseType); }
public void Property_With_UnionOrInterface_On_Property_Should_Be_Included_In_Fields() { // Arrange var fieldBuilder = new GraphQLFieldBuilder(); // Act var fields = fieldBuilder.GenerateSelectionSet(typeof(Query1)); // Assert Assert.Equal(1, fields.Count()); // Test field __typename is added Assert.True(fields.First().SelectionSet.Any(field => field.Field == "__typename")); // Test target types Assert.Equal(2, fields.First().TargetTypes.Count); Assert.Equal("subclass1", fields.First().TargetTypes.First().Key); Assert.Equal("subclass2", fields.First().TargetTypes.Last().Key); // Test default type Assert.Equal(typeof(SubClass), fields.First().BaseType); }
public void GraphQLQueryGeneratorFromFields_Should_Throw_When_No_Matching_Argument_Found() { // Arrange var fieldGenerator = new GraphQLFieldBuilder(); var queryGenerator = new GraphQLQueryGeneratorFromFields(); // Act / Assert var exception = Assert.Throws <GraphQLArgumentVariableNotFoundException>(() => { // Get the query var query = queryGenerator.GenerateQuery(GraphQLOperationType.Query, fieldGenerator.GenerateSelectionSet(typeof(SampleQuery)), new GraphQLQueryArgument("helloArgument", "Value")); }); // Assert // Should contain one element Assert.Equal(1, exception.Arguments.Count()); // Argument should contain helloArgument and value Assert.Equal("helloArgument", exception.Arguments.First().VariableName); Assert.Equal("Value", exception.Arguments.First().ArgumentValue); }
public void Property_With_2_UnionOrInterface_On_Property_And_Class_Should_Be_Included_In_Fields() { // Arrange var fieldBuilder = new GraphQLFieldBuilder(); // Act var fields = fieldBuilder.GenerateSelectionSet(typeof(Query3)); // Assert Assert.Equal(1, fields.Count()); // Test target types Assert.Equal(2, fields.First().TargetTypes.Count); Assert.Equal("Obj3", fields.First().TargetTypes.First(e => e.Key == "Obj3").Key); Assert.Equal(typeof(Object), fields.First().TargetTypes.First(e => e.Key == "Obj3").Value.Type); Assert.Equal("Obj4", fields.First().TargetTypes.First(e => e.Key == "Obj4").Key); Assert.Equal(typeof(IEnumerable <string>), fields.First().TargetTypes.First(e => e.Key == "Obj4").Value.Type); // Test default type Assert.Equal(typeof(SubQuery3_1), fields.First().BaseType); }