예제 #1
0
        /// <summary>
        /// Parses the name of the type as it would exist in the object graph and returns the name.
        /// </summary>
        /// <param name="type">The concrete type to parse.</param>
        /// <param name="kind">The kind of graph type being created.</param>
        /// <returns>System.String.</returns>
        public static string ParseName(Type type, TypeKind kind)
        {
            Validation.ThrowIfNull(type, nameof(type));

            kind = GraphValidation.ResolveTypeKind(type, kind);
            var typeNameDictionary = RetrieveTypeDictionary(type);

            if (typeNameDictionary.TryGetValue(kind, out var typeName))
            {
                return(typeName);
            }

            type = GraphValidation.EliminateWrappersFromCoreType(type);
            if (GraphQLProviders.ScalarProvider.IsScalar(type))
            {
                typeName = GraphQLProviders.ScalarProvider.RetrieveScalar(type).Name;
            }
            else if (type.IsEnum)
            {
                // enums always are their declared name (no changes needed for input types)
                typeName = type.SingleAttributeOrDefault <GraphTypeAttribute>()?.Name;
            }
            else if (kind == TypeKind.INPUT_OBJECT)
            {
                var inputNameAttrib = type.SingleAttributeOrDefault <GraphTypeAttribute>();
                if (inputNameAttrib != null && !string.IsNullOrWhiteSpace(inputNameAttrib.InputName))
                {
                    if (type.IsGenericType)
                    {
                        throw new GraphTypeDeclarationException(
                                  $"Generic Types such as '{type.FriendlyName()}', cannot use the '{nameof(GraphTypeAttribute)}'. " +
                                  "Doing so would result in a single common name across multiple different instances of the type. " +
                                  "Remove the attribute and try again, the runtime will generate an acceptable name automatically.");
                    }

                    typeName = inputNameAttrib.InputName;
                }
                else
                {
                    typeName = GraphTypeNames.ParseName(type, TypeKind.OBJECT);
                    typeName = $"Input_{typeName}";
                }
            }
            else
            {
                var graphTypeNameAttrib = type.SingleAttributeOrDefault <GraphTypeAttribute>();
                if (graphTypeNameAttrib != null && !string.IsNullOrWhiteSpace(graphTypeNameAttrib.Name))
                {
                    if (type.IsGenericType)
                    {
                        throw new GraphTypeDeclarationException(
                                  $"Generic Types such as '{type.FriendlyName()}', cannot use the '{nameof(GraphTypeAttribute)}'. " +
                                  "Doing so would result in a single common name across multiple different instances of the type. " +
                                  "Remove the declared attribute and try again.");
                    }

                    typeName = graphTypeNameAttrib.Name;
                }
            }

            typeName = typeName ?? type.FriendlyName("_");
            typeName = typeName.Replace(Constants.Routing.CLASS_META_NAME, type.Name).Trim();
            if (kind == TypeKind.DIRECTIVE && typeName.EndsWith(Constants.CommonSuffix.DIRECTIVE_SUFFIX))
            {
                typeName = typeName.ReplaceLastInstanceOfCaseInvariant(Constants.CommonSuffix.DIRECTIVE_SUFFIX, string.Empty);
            }

            AssignName(type, kind, typeName);
            return(typeName);
        }