/// <summary> /// Private method to inject the scalar into the required local dictionaries. /// </summary> /// <param name="graphType">The scalar type to add.</param> private void AddScalar(IScalarGraphType graphType) { _scalarsByType.Add(graphType.ObjectType, graphType); _scalarsTypesByName.Add(graphType.Name, graphType.ObjectType); _scalarsByName.Add(graphType.Name, graphType); if (graphType.OtherKnownTypes != null && graphType.OtherKnownTypes.Count > 0) { foreach (var otherType in graphType.OtherKnownTypes) { _scalarsByType.Add(otherType, graphType); } } }
/// <summary> /// Adds a new scalar type to the global collection of recognized scalar values. These graph types are used as /// singleton instances across all schema's and all querys. /// </summary> /// <param name="graphType">The scalar type to register.</param> public void RegisterCustomScalar(IScalarGraphType graphType) { Validation.ThrowIfNull(graphType, nameof(graphType)); if (string.IsNullOrWhiteSpace(graphType.Name)) { throw new GraphTypeDeclarationException( "The scalar must supply a name that is not null or whitespace."); } if (!GraphValidation.IsValidGraphName(graphType.Name)) { throw new GraphTypeDeclarationException( $"The scalar must supply a name that that conforms to the standard rules for GraphQL. (Regex: {Constants.RegExPatterns.NameRegex})"); } if (graphType.Kind != TypeKind.SCALAR) { throw new GraphTypeDeclarationException( $"The scalar's type kind must be set to '{nameof(TypeKind.SCALAR)}'."); } if (graphType.ObjectType == null) { throw new GraphTypeDeclarationException( $"The scalar must supply a value for '{nameof(graphType.ObjectType)}', is cannot be null."); } if (graphType.SourceResolver == null) { throw new GraphTypeDeclarationException( $"The scalar must supply a value for '{nameof(graphType.SourceResolver)}' that can convert data from a " + $"query into the primary object type of '{graphType.ObjectType.FriendlyName()}'."); } if (graphType.ValueType == ScalarValueType.Unknown) { throw new GraphTypeDeclarationException( $"The scalar must supply a value for '{nameof(graphType.ValueType)}'. This lets the validation engine " + "know what data types submitted on a user query could be parsed into a value for this scale."); } if (graphType.OtherKnownTypes == null) { throw new GraphTypeDeclarationException( $"Custom scalars must supply a value for '{nameof(graphType.OtherKnownTypes)}', it cannot be null. " + "Use an empty list if there are no other known types."); } var isAScalarAlready = this.IsScalar(graphType.Name); if (isAScalarAlready) { throw new GraphTypeDeclarationException( $"A scalar named '{graphType.Name}' already exists in this graphql instance."); } isAScalarAlready = this.IsScalar(graphType.ObjectType); if (isAScalarAlready) { var scalar = this.RetrieveScalar(graphType.ObjectType); throw new GraphTypeDeclarationException( $"The scalar's primary object type of '{graphType.ObjectType.FriendlyName()}' is " + $"already reserved by the scalar '{scalar.Name}'. Scalar object types must be unique."); } foreach (var type in graphType.OtherKnownTypes) { isAScalarAlready = this.IsScalar(type); if (isAScalarAlready) { var scalar = this.RetrieveScalar(type); throw new GraphTypeDeclarationException( $"The scalar's other known type of '{type.FriendlyName()}' is " + $"already reserved by the scalar '{scalar.Name}'. Scalar object types must be unique."); } } this.AddScalar(graphType); }