Beispiel #1
0
        private void Initialize(Action <IObjectTypeDescriptor> configure)
        {
            if (configure == null)
            {
                throw new ArgumentNullException(nameof(configure));
            }

            ObjectTypeDescriptor descriptor = CreateDescriptor();

            configure(descriptor);

            ObjectTypeDescription description = descriptor.CreateDescription();

            InitializeFields(description);

            _isOfType   = description.IsOfType;
            _interfaces = description.Interfaces;

            SyntaxNode = description.SyntaxNode;

            Initialize(description.Name, description.Description,
                       new DirectiveCollection(
                           this,
                           DirectiveLocation.Object,
                           description.Directives));
        }
        protected override ObjectTypeDefinition CreateDefinition(
            ITypeDiscoveryContext context)
        {
            var descriptor = ObjectTypeDescriptor.New(
                context.DescriptorContext);

            _configure(descriptor);
            return(descriptor.CreateDefinition());
        }
Beispiel #3
0
        protected override ObjectTypeDefinition CreateDefinition(
            IInitializationContext context)
        {
            var descriptor = ObjectTypeDescriptor.New <T>(
                context.DescriptorContext);

            _configure(descriptor);
            return(descriptor.CreateDefinition());
        }
Beispiel #4
0
        public void IgnoreOverridenMethodField()
        {
            // arrange
            var descriptor = new ObjectTypeDescriptor <Foo>(Context);

            // act
            IObjectTypeDescriptor <Foo> desc = descriptor;

            desc.Field(t => t.Equals(default)).Ignore();
Beispiel #5
0
        protected override ObjectTypeDefinition CreateDefinition(
            IInitializationContext context)
        {
            var descriptor = ObjectTypeDescriptor.New(
                DescriptorContext.Create(context.Services),
                GetType());

            _configure(descriptor);
            return(descriptor.CreateDefinition());
        }
        protected override ObjectTypeDefinition CreateDefinition(
            IInitializationContext context)
        {
            var descriptor = ObjectTypeDescriptor.FromSchemaType(
                context.DescriptorContext,
                GetType());

            _configure(descriptor);
            _configure = null;
            return(descriptor.CreateDefinition());
        }
Beispiel #7
0
        public void InferNameFromType()
        {
            // arrange
            var descriptor = new ObjectTypeDescriptor <Foo>(Context);

            // act
            IObjectTypeDescriptor <Foo> desc = descriptor;

            // assert
            Assert.Equal("Foo", descriptor.CreateDefinition().Name);
        }
Beispiel #8
0
        public void GetNameFromAttribute()
        {
            // arrange
            var descriptor = new ObjectTypeDescriptor <Foo2>(Context);

            // act
            IObjectTypeDescriptor <Foo2> desc = descriptor;

            // assert
            Assert.Equal("FooAttr", descriptor.CreateDefinition().Name);
        }
Beispiel #9
0
        public void OverwriteDefaultName()
        {
            // arrange
            var descriptor = new ObjectTypeDescriptor <Foo>(Context);

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

            // assert
            Assert.Equal("FooBar", descriptor.CreateDefinition().Name);
        }
Beispiel #10
0
        protected override ObjectTypeDefinition CreateDefinition(
            ITypeDiscoveryContext context)
        {
            ObjectTypeExtensionDescriptor <T> descriptor =
                ObjectTypeDescriptor.NewExtension <T>(context.DescriptorContext);

            _configure !(descriptor);
            _configure = null;

            return(descriptor.CreateDefinition());
        }
Beispiel #11
0
        private void Initialize(Action <IObjectTypeDescriptor> configure)
        {
            if (configure == null)
            {
                throw new ArgumentNullException(nameof(configure));
            }

            ObjectTypeDescriptor descriptor = CreateDescriptor();

            configure(descriptor);
            Initialize(descriptor);
        }
        public void OverwriteDefaultName()
        {
            // arrange
            var descriptor = new ObjectTypeDescriptor <Foo>();

            // act
            IObjectTypeDescriptor <Foo> desc = descriptor;

            desc.Name("FooBar");

            // assert
            Assert.Equal("FooBar", descriptor.CreateDescription().Name);
        }
Beispiel #13
0
        public void OverwriteAttributeName()
        {
            // arrange
            var descriptor = new ObjectTypeDescriptor <Foo2>(Context);

            // act
            IObjectTypeDescriptor <Foo2> desc = descriptor;

            desc.Name("FooBar");

            // assert
            Assert.Equal("FooBar", descriptor.CreateDefinition().Name);
        }
Beispiel #14
0
        public ObjectType()
        {
            ObjectTypeDescriptor descriptor = CreateDescriptor();

            Configure(descriptor);

            if (string.IsNullOrEmpty(descriptor.Name))
            {
                throw new ArgumentException(
                          "The type name must not be null or empty.");
            }

            if (descriptor.Fields.Count == 0)
            {
                throw new ArgumentException(
                          $"The object type `{Name}` has no fields.");
            }

            List <FieldBinding> fieldBindings = new List <FieldBinding>();

            foreach (FieldDescriptor fieldDescriptor in descriptor.Fields)
            {
                Field field = fieldDescriptor.CreateField();
                _fieldMap[fieldDescriptor.Name] = field;

                if (fieldDescriptor.Member != null)
                {
                    fieldBindings.Add(new FieldBinding(
                                          fieldDescriptor.Name, fieldDescriptor.Member, field));
                }
            }

            if (descriptor.NativeType != null)
            {
                _typeBinding = new ObjectTypeBinding(
                    descriptor.Name, descriptor.NativeType, this, fieldBindings);
            }

            _isOfType         = descriptor.IsOfType;
            _interfaceFactory = r => descriptor.Interfaces
                                .Select(t => t.TypeFactory(r))
                                .Cast <InterfaceType>();
            _interfaceTypeInfos = descriptor.Interfaces;

            Name            = descriptor.Name;
            Description     = descriptor.Description;
            IsIntrospection = descriptor.IsIntrospection;
        }
        public void InferFieldsFromType()
        {
            // arrange
            var descriptor = new ObjectTypeDescriptor <Foo>();

            // act
            IObjectTypeDescriptor <Foo> desc = descriptor;

            // assert
            Assert.Collection(
                descriptor.CreateDescription().Fields.Select(t => t.Name),
                t => Assert.Equal("a", t),
                t => Assert.Equal("b", t),
                t => Assert.Equal("c", t),
                t => Assert.Equal("equals", t));
        }
Beispiel #16
0
        public void IgnoreOverridenPropertyField()
        {
            // arrange
            var descriptor = new ObjectTypeDescriptor <Foo>(Context);

            // act
            descriptor.Field(t => t.B).Ignore();

            // assert
            Assert.Collection(
                descriptor.CreateDefinition().Fields
                .Select(t => t.Name)
                .OrderBy(t => t),
                t => Assert.Equal("a", t),
                t => Assert.Equal("c", t));
        }
        public void IgnoreFields()
        {
            // arrange
            var descriptor = new ObjectTypeDescriptor <Foo>();

            // act
            IObjectTypeDescriptor <Foo> desc = descriptor;

            desc.Field(t => t.A);
            desc.Field(t => t.B).Ignore();

            // assert
            Assert.Collection(
                descriptor.CreateDescription().Fields.Select(t => t.Name),
                t => Assert.Equal("a", t),
                t => Assert.Equal("c", t));
        }
Beispiel #18
0
        private void Initialize(ObjectTypeDescriptor descriptor)
        {
            if (string.IsNullOrEmpty(descriptor.Name))
            {
                throw new ArgumentException(
                          "The type name must not be null or empty.");
            }

            InitializeFields(descriptor);

            _isOfType   = descriptor.IsOfType;
            _interfaces = descriptor.Interfaces;

            SyntaxNode      = descriptor.SyntaxNode;
            Name            = descriptor.Name;
            Description     = descriptor.Description;
            IsIntrospection = descriptor.IsIntrospection;
        }
        public void IgnoreOverridenPropertyField()
        {
            // arrange
            var descriptor = new ObjectTypeDescriptor <Foo>();

            // act
            IObjectTypeDescriptor <Foo> desc = descriptor;

            desc.Field(t => t.B).Ignore();

            // assert
            Assert.Collection(
                descriptor.CreateDescription().Fields
                .Select(t => t.Name)
                .OrderBy(t => t),
                t => Assert.Equal("a", t),
                t => Assert.Equal("c", t),
                t => Assert.Equal("equals", t),
                t => Assert.Equal("hashCode", t));
        }
Beispiel #20
0
        protected override ObjectTypeDefinition CreateDefinition(
            ITypeDiscoveryContext context)
        {
            try
            {
                if (Definition is null)
                {
                    var descriptor = ObjectTypeDescriptor.FromSchemaType(
                        context.DescriptorContext,
                        GetType());
                    _configure !.Invoke(descriptor);
                    return(descriptor.CreateDefinition());
                }

                return(Definition);
            }
            finally
            {
                _configure = null;
            }
        }
Beispiel #21
0
        private void AddLateBoundResolverFields(IEnumerable <Type> resolverTypes)
        {
            if (resolverTypes.Any())
            {
                Dictionary <string, ObjectFieldDescription> descriptions =
                    _description.Fields.ToDictionary(t => t.Name);
                var processed = new HashSet <string>();

                foreach (Type resolverType in resolverTypes)
                {
                    ObjectTypeDescriptor.AddResolverType(
                        descriptions,
                        processed,
                        _description.ClrType ?? typeof(object),
                        resolverType);
                }

                _description.Fields.Clear();
                _description.Fields.AddRange(descriptions.Values);
            }
        }
Beispiel #22
0
        private void InitializeFields(ObjectTypeDescriptor descriptor)
        {
            List <FieldBinding> fieldBindings = new List <FieldBinding>();

            foreach (FieldDescriptor fieldDescriptor in descriptor
                     .GetFieldDescriptors())
            {
                Field field = new Field(fieldDescriptor);
                _fieldMap[fieldDescriptor.Name] = field;

                if (fieldDescriptor.Member != null)
                {
                    fieldBindings.Add(new FieldBinding(
                                          fieldDescriptor.Name, fieldDescriptor.Member, field));
                }
            }

            if (descriptor.NativeType != null)
            {
                _typeBinding = new ObjectTypeBinding(
                    descriptor.Name, descriptor.NativeType, this, fieldBindings);
            }
        }