public void Field_DectectsProperType(string fieldName, Type expectedGraphType) { var graphType = new AutoRegisteringObjectGraphType <FieldTests>(); var fieldType = graphType.Fields.Find(fieldName).ShouldNotBeNull(); fieldType.Type.ShouldBe(expectedGraphType); }
public void Field_RecognizesOutputTypeAttribute() { var graphType = new AutoRegisteringObjectGraphType <FieldTests>(); var fieldType = graphType.Fields.Find("Field7").ShouldNotBeNull(); fieldType.Type.ShouldBe(typeof(IdGraphType)); }
public void Field_IgnoresDefaultValueAttribute() { var graphType = new AutoRegisteringObjectGraphType <FieldTests>(); var fieldType = graphType.Fields.Find("Field8").ShouldNotBeNull(); fieldType.DefaultValue.ShouldBeNull(); }
public void Field_RecognizesObsoleteAttribute() { var graphType = new AutoRegisteringObjectGraphType <FieldTests>(); var fieldType = graphType.Fields.Find("Field3").ShouldNotBeNull(); fieldType.DeprecationReason.ShouldBe("Test deprecation reason"); }
public void Field_IgnoresInputTypeAttribute() { var graphType = new AutoRegisteringObjectGraphType <FieldTests>(); var fieldType = graphType.Fields.Find("Field6").ShouldNotBeNull(); fieldType.Type.ShouldBe(typeof(GraphQLClrOutputTypeReference <int>)); }
public void Field_RecognizesCustomGraphQLAttributes() { var graphType = new AutoRegisteringObjectGraphType <FieldTests>(); var fieldType = graphType.Fields.Find("Field4").ShouldNotBeNull(); fieldType.Description.ShouldBe("Test custom description for field"); }
public void AutoOutputGraphType() { var graph = new AutoRegisteringObjectGraphType <Class1>(); graph.IsAuthorizationRequired().ShouldBeTrue(); graph.IsAnonymousAllowed().ShouldBeFalse(); graph.GetPolicies().ShouldBe(new string[] { "Policy1", "Policy2", "Policy3" }); graph.GetRoles().ShouldBe(new string[] { "Role1", "Role2", "Role3" }); graph.Fields.Find("Id").IsAuthorizationRequired().ShouldBeFalse(); var field = graph.Fields.Find("Name"); field.IsAuthorizationRequired().ShouldBeTrue(); field.IsAnonymousAllowed().ShouldBeFalse(); field.GetPolicies().ShouldBe(new string[] { "Policy1", "Policy2", "Policy3" }); field.GetRoles().ShouldBe(new string[] { "Role1", "Role2", "Role3" }); field = graph.Fields.Find("Value"); field.IsAuthorizationRequired().ShouldBeTrue(); field.IsAnonymousAllowed().ShouldBeFalse(); field = graph.Fields.Find("Public"); field.IsAuthorizationRequired().ShouldBeFalse(); field.IsAnonymousAllowed().ShouldBeTrue(); }
public void Field_RecognizesDescriptionAttribute() { var graphType = new AutoRegisteringObjectGraphType <FieldTests>(); var fieldType = graphType.Fields.Find("Field2").ShouldNotBeNull(); fieldType.Description.ShouldBe("Test description"); }
public void auto_register_object_graph_type() { GraphTypeTypeRegistry.Register <Money, AutoRegisteringObjectGraphType <Money> >(); var type = new AutoRegisteringObjectGraphType <TestObject>(o => o.valuePair, o => o.someEnumerable); type.Name.ShouldBe(nameof(TestObject)); type.Description.ShouldBe("Object for test"); type.DeprecationReason.ShouldBe("Obsolete for test"); type.Fields.Count.ShouldBe(18); type.Fields.First(f => f.Name == nameof(TestObject.someString)).Description.ShouldBe("Super secret"); type.Fields.First(f => f.Name == nameof(TestObject.someString)).Type.ShouldBe(typeof(StringGraphType)); type.Fields.First(f => f.Name == nameof(TestObject.someRequiredString)).Type.ShouldBe(typeof(NonNullGraphType <StringGraphType>)); type.Fields.First(f => f.Name == nameof(TestObject.someInt)).Type.ShouldBe(typeof(IntGraphType)); type.Fields.First(f => f.Name == nameof(TestObject.someNotNullInt)).Type.ShouldBe(typeof(NonNullGraphType <IntGraphType>)); type.Fields.First(f => f.Name == nameof(TestObject.someBoolean)).DeprecationReason.ShouldBe("Use someInt"); type.Fields.First(f => f.Name == nameof(TestObject.someDate)).DefaultValue.ShouldBe(new DateTime(2019, 3, 14)); // disabled to make tests stable without touch GlobalSwitches //type.Fields.First(f => f.Name == nameof(TestObject.someShort)).Description.ShouldBe("Description from xml comment"); type.Fields.First(f => f.Name == nameof(TestObject.someEnumerableOfString)).Type.ShouldBe(typeof(ListGraphType <StringGraphType>)); type.Fields.First(f => f.Name == nameof(TestObject.someEnum)).Type.ShouldBe(typeof(NonNullGraphType <EnumerationGraphType <Direction> >)); type.Fields.First(f => f.Name == nameof(TestObject.someNullableEnum)).Type.ShouldBe(typeof(EnumerationGraphType <Direction>)); type.Fields.First(f => f.Name == nameof(TestObject.someList)).Type.ShouldBe(typeof(ListGraphType <NonNullGraphType <IntGraphType> >)); type.Fields.First(f => f.Name == nameof(TestObject.someListWithNullable)).Type.ShouldBe(typeof(ListGraphType <IntGraphType>)); type.Fields.First(f => f.Name == nameof(TestObject.someRequiredList)).Type.ShouldBe(typeof(NonNullGraphType <ListGraphType <NonNullGraphType <IntGraphType> > >)); type.Fields.First(f => f.Name == nameof(TestObject.someRequiredListWithNullable)).Type.ShouldBe(typeof(NonNullGraphType <ListGraphType <IntGraphType> >)); type.Fields.First(f => f.Name == nameof(TestObject.someMoney)).Type.ShouldBe(typeof(AutoRegisteringObjectGraphType <Money>)); var enumType = new EnumerationGraphType <Direction>(); // disabled to make tests stable without touch GlobalSwitches //enumType.Values["DESC"].Description.ShouldBe("Descending Order"); enumType.Values["RANDOM"].DeprecationReason.ShouldBe("Do not use Random. This makes no sense!"); }
public async Task ScopedMethodWorks() { Class1.DisposedCount = 0; var serviceCollection = new ServiceCollection(); serviceCollection.AddScoped <Class1>(); serviceCollection.AddScoped <Class2>(); var rootServiceProvider = serviceCollection.BuildServiceProvider(false); var graphType = new AutoRegisteringObjectGraphType <TestClass>(); var context = new ResolveFieldContext { Source = new TestClass(), RequestServices = rootServiceProvider, }; var unscopedFieldResolver = graphType.Fields.Find(nameof(TestClass.UnscopedField)) !.Resolver !; var scopedFieldResolver = graphType.Fields.Find(nameof(TestClass.ScopedField)) !.Resolver !; var scopedAsyncFieldResolver = graphType.Fields.Find(nameof(TestClass.ScopedAsyncField)) !.Resolver !; (await unscopedFieldResolver.ResolveAsync(context).ConfigureAwait(false)).ShouldBe("0 1"); (await unscopedFieldResolver.ResolveAsync(context).ConfigureAwait(false)).ShouldBe("1 2"); (await unscopedFieldResolver.ResolveAsync(context).ConfigureAwait(false)).ShouldBe("2 3"); Class1.DisposedCount.ShouldBe(0); (await scopedFieldResolver.ResolveAsync(context).ConfigureAwait(false)).ShouldBe("0 1"); Class1.DisposedCount.ShouldBe(1); (await scopedFieldResolver.ResolveAsync(context).ConfigureAwait(false)).ShouldBe("0 1"); Class1.DisposedCount.ShouldBe(2); (await scopedAsyncFieldResolver.ResolveAsync(context).ConfigureAwait(false)).ShouldBe("0 1"); Class1.DisposedCount.ShouldBe(3); (await scopedAsyncFieldResolver.ResolveAsync(context).ConfigureAwait(false)).ShouldBe("0 1"); Class1.DisposedCount.ShouldBe(4); (await unscopedFieldResolver.ResolveAsync(context).ConfigureAwait(false)).ShouldBe("3 4"); rootServiceProvider.Dispose(); Class1.DisposedCount.ShouldBe(5); }
public async Task Argument_ResolverTests_WithNonNullString(string fieldName, string arg1Name, object?arg1Value, int?arg2Value, object?expected) { var graphType = new AutoRegisteringObjectGraphType <ArgumentTests>(); var fieldType = graphType.Fields.Find(fieldName).ShouldNotBeNull(); using var cts = new CancellationTokenSource(); cts.Cancel(); var context = new ResolveFieldContext { Arguments = new Dictionary <string, ArgumentValue>(), CancellationToken = cts.Token, Source = new ArgumentTests(), }; if (arg1Name != null) { context.Arguments.Add(arg1Name, new ArgumentValue(arg1Value, ArgumentSource.Variable)); } if (arg2Value.HasValue) { context.Arguments.Add("arg2", new ArgumentValue(arg2Value, ArgumentSource.Variable)); } var serviceCollection = new ServiceCollection(); serviceCollection.AddSingleton <string>("testService"); using var provider = serviceCollection.BuildServiceProvider(); context.RequestServices = provider; fieldType.Resolver.ShouldNotBeNull(); (await fieldType.Resolver !.ResolveAsync(context).ConfigureAwait(false)).ShouldBe(expected); }
public void auto_register() { GraphTypeTypeRegistry.Register <Direction, EnumerationGraphType <Direction> >(); GraphTypeTypeRegistry.Register <Money, AutoRegisteringObjectGraphType <Money> >(); var type = new AutoRegisteringObjectGraphType <TestObject>(o => o.valuePair, o => o.someEnumerable); type.Fields.Count().ShouldBe(18); type.Fields.First(f => f.Name == nameof(TestObject.someString)).Description.ShouldBe("Super secret"); type.Fields.First(f => f.Name == nameof(TestObject.someString)).Type.ShouldBe(typeof(StringGraphType)); type.Fields.First(f => f.Name == nameof(TestObject.someRequiredString)).Type.ShouldBe(typeof(NonNullGraphType <StringGraphType>)); type.Fields.First(f => f.Name == nameof(TestObject.someInt)).Type.ShouldBe(typeof(IntGraphType)); type.Fields.First(f => f.Name == nameof(TestObject.someNotNullInt)).Type.ShouldBe(typeof(NonNullGraphType <IntGraphType>)); type.Fields.First(f => f.Name == nameof(TestObject.someBoolean)).DeprecationReason.ShouldBe("Use someInt"); type.Fields.First(f => f.Name == nameof(TestObject.someDate)).DefaultValue.ShouldBe(new DateTime(2019, 3, 14)); type.Fields.First(f => f.Name == nameof(TestObject.someEnumerableOfString)).Type.ShouldBe(typeof(ListGraphType <StringGraphType>)); type.Fields.First(f => f.Name == nameof(TestObject.someEnum)).Type.ShouldBe(typeof(NonNullGraphType <EnumerationGraphType <Direction> >)); type.Fields.First(f => f.Name == nameof(TestObject.someNullableEnum)).Type.ShouldBe(typeof(EnumerationGraphType <Direction>)); type.Fields.First(f => f.Name == nameof(TestObject.someList)).Type.ShouldBe(typeof(ListGraphType <NonNullGraphType <IntGraphType> >)); type.Fields.First(f => f.Name == nameof(TestObject.someListWithNullable)).Type.ShouldBe(typeof(ListGraphType <IntGraphType>)); type.Fields.First(f => f.Name == nameof(TestObject.someRequiredList)).Type.ShouldBe(typeof(NonNullGraphType <ListGraphType <NonNullGraphType <IntGraphType> > >)); type.Fields.First(f => f.Name == nameof(TestObject.someRequiredListWithNullable)).Type.ShouldBe(typeof(NonNullGraphType <ListGraphType <IntGraphType> >)); type.Fields.First(f => f.Name == nameof(TestObject.someMoney)).Type.ShouldBe(typeof(AutoRegisteringObjectGraphType <Money>)); var enumType = new EnumerationGraphType <Direction>(); enumType.Values["RANDOM"].DeprecationReason.ShouldBe("Do not use Random. This makes no sense!"); }
public void TestBasicRecordStructNoExtraFields() { var graphType = new AutoRegisteringObjectGraphType <TestBasicRecordStruct>(); graphType.Fields.Find("Id").ShouldNotBeNull(); graphType.Fields.Find("Name").ShouldNotBeNull(); graphType.Fields.Count.ShouldBe(2); }
public void Class_RecognizesMultipleAttributes() { var graphType = new AutoRegisteringObjectGraphType <TestClass_WithMultipleAttributes>(); graphType.Description.ShouldBe("Test description"); graphType.GetMetadata <string>("key1").ShouldBe("value1"); graphType.GetMetadata <string>("key2").ShouldBe("value2"); }
public void Field_RecognizesMultipleAttributes() { var graphType = new AutoRegisteringObjectGraphType <FieldTests>(); var fieldType = graphType.Fields.Find("Field5").ShouldNotBeNull(); fieldType.Description.ShouldBe("Test description"); fieldType.GetMetadata <string>("key1").ShouldBe("value1"); fieldType.GetMetadata <string>("key2").ShouldBe("value2"); }
public void auto_register() { var type = new AutoRegisteringObjectGraphType <TestObject>(o => o.valuePair); type.Fields.Count().ShouldBe(4); type.Fields.First(f => f.Name == "someString").Description.ShouldBe("Super secret"); type.Fields.First(f => f.Name == "someBoolean").DeprecationReason.ShouldBe("Use someInt"); type.Fields.First(f => f.Name == "someDate").DefaultValue.ShouldBe(new DateTime(2019, 3, 14)); }
public void SkipsSpecifiedProperties() { var outputType = new AutoRegisteringObjectGraphType <TestClass>(x => x.Field1); outputType.Fields.Find("Field1").ShouldBeNull(); outputType.Fields.Find("Field2").ShouldNotBeNull(); outputType.Fields.Find("Field3").ShouldBeNull(); outputType.Fields.Find("Field4").ShouldNotBeNull(); outputType.Fields.Find("Field5").ShouldBeNull(); }
public void RegistersReadablePropertiesAndMethodsOnly() { var outputType = new AutoRegisteringObjectGraphType <TestClass>(); outputType.Fields.Find("Field1").ShouldNotBeNull(); outputType.Fields.Find("Field2").ShouldNotBeNull(); outputType.Fields.Find("Field3").ShouldBeNull(); outputType.Fields.Find("Field4").ShouldNotBeNull(); outputType.Fields.Find("Field5").ShouldBeNull(); }
public SubscriptionSchemaWithAutoGraphType() { Query = new AutoRegisteringObjectGraphType <QueryType>(); Mutation = new AutoRegisteringObjectGraphType <MutationType>(); Subscription = new AutoRegisteringObjectGraphType <SubscriptionType>(); this.AutoRegister <Message>(); this.AutoRegister <MessageFrom>(); this.AutoRegister <ReceivedMessage>(); }
public async Task CustomHardcodedArgumentAttributesWork() { var graphType = new AutoRegisteringObjectGraphType <CustomHardcodedArgumentAttributeTestClass>(); var fieldType = graphType.Fields.Find(nameof(CustomHardcodedArgumentAttributeTestClass.FieldWithHardcodedValue)) !; var resolver = fieldType.Resolver !; (await resolver.ResolveAsync(new ResolveFieldContext { Source = new CustomHardcodedArgumentAttributeTestClass(), }).ConfigureAwait(false)).ShouldBe("85"); }
public void Argument_VerifyType(string fieldName, string argumentName, Type?argumentGraphType) { var graphType = new AutoRegisteringObjectGraphType <ArgumentTests>(); var fieldType = graphType.Fields.Find(fieldName).ShouldNotBeNull(); var argument = fieldType.Arguments?.Find(argumentName); if (argumentGraphType != null) { argument.ShouldNotBeNull($"Argument '{argumentName}' of field '{fieldName}' could not be found."); argument.Type.ShouldBe(argumentGraphType); } else { argument.ShouldBeNull($"Argument '{argumentName}' of field '{fieldName}' should not exist but does."); } }
public async Task Uses_ExecutionStrategySelector() { var queryStrategy = new TestQueryExecutionStrategy(); var mutationStrategy = new TestMutationExecutionStrategy(); var selector = new DefaultExecutionStrategySelector( new[] { new ExecutionStrategyRegistration(queryStrategy, GraphQLParser.AST.OperationType.Query), new ExecutionStrategyRegistration(mutationStrategy, GraphQLParser.AST.OperationType.Mutation), }); var executer = new DocumentExecuter( new GraphQLDocumentBuilder(), new DocumentValidator(), new ComplexityAnalyzer(), DefaultDocumentCache.Instance, new IConfigureExecutionOptions[] { }, selector); var schema = new Schema(); var graphType = new AutoRegisteringObjectGraphType <SampleGraph>(); schema.Query = graphType; schema.Mutation = graphType; schema.Initialize(); var ret = await executer.ExecuteAsync(new ExecutionOptions() { Schema = schema, Query = "{hero}", Root = new SampleGraph(), }).ConfigureAwait(false); ret.Errors.ShouldBeNull(); queryStrategy.Executed.ShouldBeTrue(); ret = await executer.ExecuteAsync(new ExecutionOptions() { Schema = schema, Query = "mutation{hero}", Root = new SampleGraph(), }).ConfigureAwait(false); ret.Errors.ShouldBeNull(); mutationStrategy.Executed.ShouldBeTrue(); }
public void auto_register_object_graph_type() { try { GlobalSwitches.EnableReadDescriptionFromXmlDocumentation = true; var schema = new Schema(); var type = new AutoRegisteringObjectGraphType <TestObject>(o => o.valuePair, o => o.someEnumerable); schema.Query = type; schema.Initialize(); type.Name.ShouldBe(nameof(TestObject)); type.Description.ShouldBe("Object for test"); type.DeprecationReason.ShouldBe("Obsolete for test"); type.Fields.Count.ShouldBe(18); type.Fields.First(f => f.Name == nameof(TestObject.someString)).Description.ShouldBe("Super secret"); type.Fields.First(f => f.Name == nameof(TestObject.someString)).Type.ShouldBe(typeof(StringGraphType)); type.Fields.First(f => f.Name == nameof(TestObject.someRequiredString)).Type.ShouldBe(typeof(NonNullGraphType <StringGraphType>)); type.Fields.First(f => f.Name == nameof(TestObject.someInt)).Type.ShouldBe(typeof(IntGraphType)); type.Fields.First(f => f.Name == nameof(TestObject.someNotNullInt)).Type.ShouldBe(typeof(NonNullGraphType <IntGraphType>)); type.Fields.First(f => f.Name == nameof(TestObject.someBoolean)).DeprecationReason.ShouldBe("Use someInt"); type.Fields.First(f => f.Name == nameof(TestObject.someShort)).Description.ShouldBe("Description from XML comment"); type.Fields.First(f => f.Name == nameof(TestObject.someEnumerableOfString)).Type.ShouldBe(typeof(ListGraphType <StringGraphType>)); type.Fields.First(f => f.Name == nameof(TestObject.someEnum)).Type.ShouldBe(typeof(NonNullGraphType <EnumerationGraphType <Direction> >)); type.Fields.First(f => f.Name == nameof(TestObject.someNullableEnum)).Type.ShouldBe(typeof(EnumerationGraphType <Direction>)); type.Fields.First(f => f.Name == nameof(TestObject.someList)).Type.ShouldBe(typeof(ListGraphType <NonNullGraphType <IntGraphType> >)); type.Fields.First(f => f.Name == nameof(TestObject.someListWithNullable)).Type.ShouldBe(typeof(ListGraphType <IntGraphType>)); type.Fields.First(f => f.Name == nameof(TestObject.someRequiredList)).Type.ShouldBe(typeof(NonNullGraphType <ListGraphType <NonNullGraphType <IntGraphType> > >)); type.Fields.First(f => f.Name == nameof(TestObject.someRequiredListWithNullable)).Type.ShouldBe(typeof(NonNullGraphType <ListGraphType <IntGraphType> >)); type.Fields.First(f => f.Name == nameof(TestObject.someMoney)).Type.ShouldBe(typeof(AutoRegisteringObjectGraphType <Money>)); var enumType = new EnumerationGraphType <Direction>(); enumType.Values["DESC"].Description.ShouldBe("Descending Order"); enumType.Values["RANDOM"].DeprecationReason.ShouldBe("Do not use Random. This makes no sense!"); } finally { GlobalSwitches.EnableReadDescriptionFromXmlDocumentation = false; } }
public void Field_RecognizesNameAttribute() { var graphType = new AutoRegisteringObjectGraphType <FieldTests>(); graphType.Fields.Find("Test1").ShouldNotBeNull(); }
public void Class_RecognizesInheritedAttributes() { var graphType = new AutoRegisteringObjectGraphType <DerivedClass>(); graphType.Fields.Find("Field1CustomName").ShouldNotBeNull(); }
public void Class_RecognizesCustomGraphQLAttributes() { var graphType = new AutoRegisteringObjectGraphType <TestClass_WithCustomAttributes>(); graphType.Description.ShouldBe("Test custom description"); }
public void Field_RemovesAsyncSuffix() { var graphType = new AutoRegisteringObjectGraphType <FieldTests>(); var fieldType = graphType.Fields.Find("TaskIntField").ShouldNotBeNull(); }
public void Class_RecognizesObsoleteAttribute() { var graphType = new AutoRegisteringObjectGraphType <TestClass_WithCustomDeprecationReason>(); graphType.DeprecationReason.ShouldBe("Test deprecation reason"); }
public void Class_RecognizesDescriptionAttribute() { var graphType = new AutoRegisteringObjectGraphType <TestClass_WithCustomDescription>(); graphType.Description.ShouldBe("Test description"); }
public void Class_RecognizesOutputNameAttribute() { var graphType = new AutoRegisteringObjectGraphType <TestClass_WithCustomOutputName>(); graphType.Name.ShouldBe("TestWithCustomName"); }