/// <summary>
 /// Initializes a new instance of the <see cref="UnionGraphType" /> class.
 /// </summary>
 /// <param name="name">The name.</param>
 /// <param name="unionProxy">The proxy object which defined
 /// the union type at design t.</param>
 public UnionGraphType(string name, IGraphUnionProxy unionProxy)
 {
     this.Name    = Validation.ThrowIfNullWhiteSpaceOrReturn(name, nameof(name));
     this.Proxy   = Validation.ThrowIfNullOrReturn(unionProxy, nameof(unionProxy));
     _types       = new HashSet <Type>();
     _names       = new List <string>();
     this.Publish = true;
 }
Пример #2
0
        /// <summary>
        /// Retrieves proxy instance defined on this attribute that is used to generate the <see cref="UnionGraphType" /> metadata.
        /// </summary>
        private void BuildUnionProxyInstance()
        {
            var fieldAttribute = this.SingleAttributeOfTypeOrDefault <GraphFieldAttribute>();

            if (fieldAttribute == null)
            {
                return;
            }

            IGraphUnionProxy proxy = null;

            if (fieldAttribute.Types.Count == 1)
            {
                var proxyType = fieldAttribute.Types.FirstOrDefault();

                // declaring a proxy type overrides all other declarations for a union
                // use it regardless of any names or other types that may be supplied
                if (proxyType != null && Validation.IsCastable <IGraphUnionProxy>(proxyType))
                {
                    var paramlessConstructor = proxyType.GetConstructor(BindingFlags.Instance | BindingFlags.Public, null, Type.EmptyTypes, null);
                    if (paramlessConstructor == null)
                    {
                        throw new GraphTypeDeclarationException(
                                  $"The union proxy type '{proxyType.FriendlyName()}' could not be instantiated. " +
                                  "All union proxies must declare a parameterless constructor.");
                    }

                    proxy = InstanceFactory.CreateInstance(proxyType) as IGraphUnionProxy;
                }
            }

            // when no proxy type is declared attempt to construct the proxy from types supplied
            // if and only if a name was supplied for the union
            if (proxy == null && !string.IsNullOrWhiteSpace(fieldAttribute.UnionTypeName))
            {
                proxy = new GraphUnionProxy(fieldAttribute.UnionTypeName, fieldAttribute.Types);
            }

            this.UnionProxy = proxy;
        }
        /// <summary>
        /// Creates a union graph type from the given proxy.
        /// </summary>
        /// <param name="proxy">The proxy to convert to a union.</param>
        /// <param name="ownerKind">The typekind of the <see cref="IGraphType"/> that sponsors this union.</param>
        /// <returns>IUnionGraphType.</returns>
        public IUnionGraphType CreateGraphType(IGraphUnionProxy proxy, TypeKind ownerKind)
        {
            if (proxy == null)
            {
                return(null);
            }

            var formatter = _schema.Configuration.DeclarationOptions.GraphNamingFormatter;
            var name      = formatter.FormatGraphTypeName(proxy.Name);
            var union     = new UnionGraphType(name, proxy)
            {
                Description = proxy.Description,
                Publish     = proxy.Publish,
            };

            foreach (var type in proxy.Types)
            {
                union.AddPossibleGraphType(
                    formatter.FormatGraphTypeName(GraphTypeNames.ParseName(type, ownerKind)),
                    type);
            }

            return(union);
        }