Example #1
0
        /// <summary>
        /// Adds the given type to the schema as a graph type or a registered controller depending
        /// on the type.
        /// </summary>
        /// <param name="type">The concrete type to add.</param>
        /// <param name="kind">The kind of graph type to create from the supplied concrete type. If not supplied the concrete type will
        /// attempt to auto assign a type of scalar, enum or object as necessary.</param>
        public void EnsureGraphType(Type type, TypeKind?kind = null)
        {
            if (Validation.IsCastable <GraphController>(type))
            {
                if (GraphQLProviders.TemplateProvider.ParseType(type) is IGraphControllerTemplate controllerDefinition)
                {
                    this.AddController(controllerDefinition);
                }

                return;
            }

            type = GraphValidation.EliminateWrappersFromCoreType(type);

            // if the type is already registered, early exit no point in running it through again
            var actualKind = GraphValidation.ResolveTypeKindOrThrow(type, kind);

            if (this.Schema.KnownTypes.Contains(type, actualKind))
            {
                return;
            }

            var maker = GraphQLProviders.GraphTypeMakerProvider.CreateTypeMaker(this.Schema, actualKind);

            if (maker != null)
            {
                var result = maker.CreateGraphType(type);
                if (result != null)
                {
                    this.Schema.KnownTypes.EnsureGraphType(result.GraphType, result.ConcreteType);
                    this.EnsureDependents(result);
                }
            }
        }