private static void CompileMiddleware(
            ITypeCompletionContext context,
            ObjectFieldDefinition definition,
            ITypeReference argumentTypeReference,
            FieldMiddleware placeholder,
            string?scope)
        {
            IType resolvedType = context.GetType <IType>(argumentTypeReference);

            if (!(resolvedType.ElementType().NamedType() is ISortInputType type))
            {
                throw Sorting_TypeOfInvalidFormat(resolvedType);
            }

            ISortConvention convention = context.DescriptorContext.GetSortConvention(scope);

            var fieldDescriptor = ObjectFieldDescriptor.From(context.DescriptorContext, definition);

            convention.ConfigureField(fieldDescriptor);

            MethodInfo factory    = _factoryTemplate.MakeGenericMethod(type.EntityType.Source);
            var        middleware = (FieldMiddleware)factory.Invoke(null, new object[] { convention }) !;
            var        index      = definition.MiddlewareComponents.IndexOf(placeholder);

            definition.MiddlewareComponents[index] = middleware;
        }
        public void SetResolverAndInferTypeFromResolver()
        {
            // arrange
            var descriptor =
                ObjectFieldDescriptor.New(
                    Context,
                    typeof(ObjectField).GetProperty("Arguments"));

            // act
            descriptor.Resolver(() => "ThisIsAString");

            // assert
            ObjectFieldDefinition description = descriptor.CreateDefinition();
            ITypeReference        typeRef     = description.Type;

            Assert.Equal(
                typeof(NativeType <string>),
                Assert.IsType <ClrTypeReference>(typeRef).Type);

            Assert.NotNull(description.Resolver);

            var context = new Mock <IResolverContext>(MockBehavior.Strict);

            Assert.Equal("ThisIsAString",
                         description.Resolver(context.Object).Result);
        }
        protected ObjectFieldDescriptor Field(NameString name)
        {
            var fieldDescriptor = new ObjectFieldDescriptor(
                name.EnsureNotEmpty(nameof(name)));

            Fields.Add(fieldDescriptor);
            return(fieldDescriptor);
        }
        public void OverwriteName()
        {
            // arrange
            var descriptor = ObjectFieldDescriptor.New(
                Context,
                typeof(ObjectField).GetProperty("Arguments"));

            // act
            descriptor.Name("args");

            // assert
            Assert.Equal("args", descriptor.CreateDefinition().Name);
        }
        protected static ObjectFieldDescriptor CreateFieldDescriptor(
            Type sourceType, Type resolverType, MemberInfo member)
        {
            var fieldDescriptor = new ObjectFieldDescriptor(
                member, sourceType);

            if (resolverType != sourceType)
            {
                fieldDescriptor.ResolverType(resolverType);
            }

            return(fieldDescriptor);
        }
Esempio n. 6
0
        public void OverwriteName()
        {
            // arrange
            var descriptor = new ObjectFieldDescriptor(
                typeof(ObjectField).GetProperty("Arguments"),
                typeof(ObjectField));

            // act
            ((IObjectFieldDescriptor)descriptor)
            .Name("args");

            // assert
            Assert.Equal("args", descriptor.CreateDescription().Name);
        }
Esempio n. 7
0
        private static ObjectFieldDescription ExecuteConfigure(
            NameString fieldName,
            Action <IObjectFieldDescriptor> configure)
        {
            if (configure == null)
            {
                throw new ArgumentNullException(nameof(configure));
            }

            var descriptor = new ObjectFieldDescriptor(fieldName);

            configure(descriptor);
            return(descriptor.CreateDescription());
        }
        public void OverwriteName()
        {
            // arrange
            var descriptor = new ObjectFieldDescriptor(
                "Field", typeof(ObjectField).GetProperty("Arguments"),
                typeof(IReadOnlyDictionary <string, InputField>));

            // act
            ((IObjectFieldDescriptor)descriptor)
            .Name("args");

            // assert
            Assert.Equal("args", descriptor.CreateDescription().Name);
        }
        public void SetDescription()
        {
            // arrange
            string expectedDescription = Guid.NewGuid().ToString();
            var    descriptor          = ObjectFieldDescriptor.New(
                Context,
                typeof(ObjectField).GetProperty("Arguments"));

            // act
            descriptor.Description(expectedDescription);

            // assert
            Assert.Equal(expectedDescription,
                         descriptor.CreateDefinition().Description);
        }
        public void ResolverTypeIsSet()
        {
            // arrange
            // act
            var descriptor =
                ObjectFieldDescriptor.New(
                    Context,
                    typeof(ObjectField).GetProperty("Arguments"),
                    typeof(string));

            // assert
            ObjectFieldDefinition description = descriptor.CreateDefinition();

            Assert.Equal(typeof(string), description.ResolverType);
        }
Esempio n. 11
0
        public void SourceTypeIsSet()
        {
            // arrange
            var descriptor = new ObjectFieldDescriptor(
                typeof(ObjectField).GetProperty("Arguments"),
                typeof(ObjectField));

            // act
            ((IObjectFieldDescriptor)descriptor)
            .Name("args");

            // assert
            ObjectFieldDescription description = descriptor.CreateDescription();

            Assert.Equal(typeof(ObjectField), description.SourceType);
        }
Esempio n. 12
0
        public void ResolverTypeIsSet()
        {
            // arrange
            var descriptor = new ObjectFieldDescriptor(
                typeof(ObjectField).GetProperty("Arguments"),
                typeof(ObjectField));

            // act
            descriptor.ResolverType(typeof(string));

            // assert
            ObjectFieldDescription description = descriptor.CreateDescription();

            Assert.Equal(typeof(ObjectField), description.SourceType);
            Assert.Equal(typeof(string), description.ResolverType);
        }
        public void SchemaTypesOverwriteDotNetTypes()
        {
            // arrange
            var descriptor = new ObjectFieldDescriptor("Type", "field");

            // act
            ((IObjectFieldDescriptor)descriptor)
            .Type <NativeType <IReadOnlyDictionary <string, string> > >()
            .Type <ListType <StringType> >();

            // assert
            ObjectFieldDescription description = descriptor.CreateDescription();
            TypeReference          typeRef     = description.TypeReference;

            Assert.Equal(typeof(ListType <StringType>), typeRef.ClrType);
        }
Esempio n. 14
0
        public void SetDescription()
        {
            // arrange
            string expectedDescription = Guid.NewGuid().ToString();
            var    descriptor          = new ObjectFieldDescriptor(
                typeof(ObjectField).GetProperty("Arguments"),
                typeof(ObjectField));

            // act
            ((IObjectFieldDescriptor)descriptor)
            .Description(expectedDescription);

            // assert
            Assert.Equal(expectedDescription,
                         descriptor.CreateDescription().Description);
        }
        public void SetNonGenericType()
        {
            // arrange
            var descriptor =
                ObjectFieldDescriptor.New(Context, "field");

            // act
            descriptor.Type(typeof(StringType));

            // assert
            ObjectFieldDefinition description = descriptor.CreateDefinition();
            ITypeReference        typeRef     = description.Type;

            Assert.Equal(
                typeof(StringType),
                Assert.IsType <ClrTypeReference>(typeRef).Type);
        }
        public void SchemaTypesOverwriteDotNetTypes()
        {
            // arrange
            var descriptor =
                ObjectFieldDescriptor.New(Context, "field");

            // act
            descriptor
            .Type <NativeType <IReadOnlyDictionary <string, string> > >()
            .Type <ListType <StringType> >();

            // assert
            ObjectFieldDefinition description = descriptor.CreateDefinition();
            ITypeReference        typeRef     = description.Type;

            Assert.Equal(typeof(ListType <StringType>),
                         Assert.IsType <ClrTypeReference>(typeRef).Type);
        }
Esempio n. 17
0
        public void SetResolverAndInferTypeIsAlwaysRecognosedAsDotNetType()
        {
            // arrange
            var descriptor = new ObjectFieldDescriptor(
                typeof(ObjectField).GetProperty("Arguments"),
                typeof(ObjectField));

            // act
            ((IObjectFieldDescriptor)descriptor)
            .Type <__Type>()
            .Resolver(ctx => ctx.Schema
                      .GetType <INamedType>(ctx.Argument <string>("type")));

            // assert
            ObjectFieldDescription description = descriptor.CreateDescription();

            Assert.Equal(typeof(__Type), description.TypeReference.ClrType);
            Assert.NotNull(description.Resolver);
        }
Esempio n. 18
0
        public void ResolverTypesDoNotOverwriteSchemaTypes()
        {
            // arrange
            var descriptor = new ObjectFieldDescriptor(
                typeof(ObjectField).GetProperty("Arguments"),
                typeof(ObjectField));

            // act
            ((IObjectFieldDescriptor)descriptor)
            .Name("args")
            .Type <NonNullType <ListType <NonNullType <__InputValue> > > >()
            .Resolver(c => c.Parent <ObjectField>().Arguments);

            // assert
            ObjectFieldDescription description = descriptor.CreateDescription();
            TypeReference          typeRef     = description.TypeReference;

            Assert.Equal(typeof(NonNullType <ListType <NonNullType <__InputValue> > >), typeRef.ClrType);
        }
Esempio n. 19
0
        private static void CompileMiddleware(
            ITypeCompletionContext context,
            ObjectFieldDefinition definition,
            ITypeReference argumentTypeReference,
            FieldMiddleware placeholder,
            string?scope)
        {
            IFilterInputType  type       = context.GetType <IFilterInputType>(argumentTypeReference);
            IFilterConvention convention = context.DescriptorContext.GetFilterConvention(scope);

            var fieldDescriptor = ObjectFieldDescriptor.From(context.DescriptorContext, definition);

            convention.ConfigureField(fieldDescriptor);

            MethodInfo factory    = _factoryTemplate.MakeGenericMethod(type.EntityType.Source);
            var        middleware = (FieldMiddleware)factory.Invoke(null, new object[] { convention }) !;
            var        index      = definition.MiddlewareComponents.IndexOf(placeholder);

            definition.MiddlewareComponents[index] = middleware;
        }
        public void SetResolverAndInferTypeFromResolver()
        {
            // arrange
            var descriptor = new ObjectFieldDescriptor(
                "Field", typeof(ObjectField).GetProperty("Arguments"),
                typeof(IReadOnlyDictionary <string, InputField>));

            // act
            ((IObjectFieldDescriptor)descriptor)
            .Resolver(() => "ThisIsAString");

            // assert
            ObjectFieldDescription description = descriptor.CreateDescription();

            Assert.Equal(typeof(NativeType <string>), description.TypeReference.NativeType);
            Assert.NotNull(description.Resolver);

            Mock <IResolverContext> context = new Mock <IResolverContext>(MockBehavior.Strict);

            Assert.Equal("ThisIsAString", description.Resolver(context.Object, default));
        }
        public void ResolverTypesDoNotOverwriteSchemaTypes()
        {
            // arrange
            var descriptor = ObjectFieldDescriptor.New(
                Context,
                typeof(ObjectField).GetProperty("Arguments"));

            // act
            descriptor
            .Name("args")
            .Type <NonNullType <ListType <NonNullType <__InputValue> > > >()
            .Resolver(c => c.Parent <ObjectField>().Arguments);

            // assert
            ObjectFieldDefinition description = descriptor.CreateDefinition();
            ITypeReference        typeRef     = description.Type;

            Assert.Equal(
                typeof(NonNullType <ListType <NonNullType <__InputValue> > >),
                Assert.IsType <ClrTypeReference>(typeRef).Type);
        }
        protected ObjectFieldDescriptor Field(string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentException(
                          "The field name cannot be null or empty.",
                          nameof(name));
            }

            if (!ValidationHelper.IsFieldNameValid(name))
            {
                throw new ArgumentException(
                          "The specified name is not a valid GraphQL field name.",
                          nameof(name));
            }

            var fieldDescriptor = new ObjectFieldDescriptor(
                ObjectDescription.Name, name);

            Fields.Add(fieldDescriptor);
            return(fieldDescriptor);
        }
        public void SetResolverAndInferTypeIsAlwaysRecognisedAsDotNetType()
        {
            // arrange
            var descriptor =
                ObjectFieldDescriptor.New(
                    Context,
                    typeof(ObjectField).GetProperty("Arguments"));

            // act
            descriptor
            .Type <__Type>()
            .Resolver(ctx => ctx.Schema
                      .GetType <INamedType>(ctx.Argument <string>("type")));

            // assert
            ObjectFieldDefinition description = descriptor.CreateDefinition();
            ITypeReference        typeRef     = description.Type;

            Assert.Equal(
                typeof(__Type),
                Assert.IsType <ClrTypeReference>(typeRef).Type);
            Assert.NotNull(description.Resolver);
        }
Esempio n. 24
0
        protected ObjectFieldDescriptor Field <TResolver>(
            Expression <Func <TResolver, object> > propertyOrMethod)
        {
            if (propertyOrMethod == null)
            {
                throw new ArgumentNullException(nameof(propertyOrMethod));
            }

            MemberInfo member = propertyOrMethod.ExtractMember();

            if (member is PropertyInfo || member is MethodInfo)
            {
                ObjectFieldDescriptor fieldDescriptor =
                    CreateFieldDescriptor(
                        ObjectDescription.ClrType,
                        typeof(TResolver), member);
                Fields.Add(fieldDescriptor);
                return(fieldDescriptor);
            }

            throw new ArgumentException(
                      "A field of an entity can only be a property or a method.",
                      nameof(member));
        }
        public void Type_Syntax_Type_Null()
        {
            void Error() => ObjectFieldDescriptor.New(Context, "foo").Type((string)null);

            Assert.Throws <ArgumentNullException>(Error);
        }