Example #1
0
        /// <summary>
        /// Ensures the provided <see cref="IGraphType" /> exists in this collection (adding it if it is missing)
        /// and that the given type reference is assigned to it. An exception will be thrown if the type reference is already assigned
        /// to a different <see cref="IGraphType" />. No dependents or additional types will be added.
        /// </summary>
        /// <param name="graphType">Type of the graph.</param>
        /// <param name="associatedType">The concrete type to associate to the graph type.</param>
        /// <returns><c>true</c> if type had to be added, <c>false</c> if it already existed in the collection.</returns>
        public bool EnsureGraphType(IGraphType graphType, Type associatedType = null)
        {
            Validation.ThrowIfNull(graphType, nameof(graphType));

            associatedType = GraphValidation.EliminateWrappersFromCoreType(associatedType);
            GraphValidation.EnsureValidGraphTypeOrThrow(associatedType);

            // attempt to create a relationship between the graph type and the associated type
            // the concrete type collection will throw an exception if that relationship fails or isnt
            // updated to the new data correctly.
            var justAdded = _graphTypesByName.TryAdd(graphType.Name, graphType);

            _concreteTypes.EnsureRelationship(graphType, associatedType);
            _extendableGraphTypeTracker.MonitorGraphType(graphType);

            // dequeue and add any extension fields if present
            if (associatedType != null)
            {
                var unregisteredFields = _typeQueue.DequeueFields(associatedType);
                if (graphType is IExtendableGraphType objType)
                {
                    foreach (var field in unregisteredFields)
                    {
                        _extendableGraphTypeTracker.AddFieldExtension(objType, field);
                    }
                }
            }

            return(justAdded);
        }