public static Value astFromValue()

in src/main/com/intellij/lang/jsgraphql/types/language/AstValueHelper.java [59:117]


  public static Value<?> astFromValue(Object value, GraphQLType type) {
    if (value == null) {
      return null;
    }

    if (isNonNull(type)) {
      return handleNonNull(value, (GraphQLNonNull)type);
    }

    // Convert JavaScript array to GraphQL list. If the GraphQLType is a list, but
    // the value is not an array, convert the value using the list's item type.
    if (isList(type)) {
      return handleList(value, (GraphQLList)type);
    }

    // Populate the fields of the input object by creating ASTs from each value
    // in the JavaScript object according to the fields in the input type.
    if (type instanceof GraphQLInputObjectType) {
      return handleInputObject(value, (GraphQLInputObjectType)type);
    }

    if (!(type instanceof GraphQLScalarType || type instanceof GraphQLEnumType)) {
      throw new AssertException("Must provide Input Type, cannot use: " + type.getClass());
    }

    // Since value is an internally represented value, it must be serialized
    // to an externally represented value before converting into an AST.
    final Object serialized = serialize(type, value);
    if (isNullish(serialized)) {
      return null;
    }

    // Others serialize based on their corresponding JavaScript scalar types.
    if (serialized instanceof Boolean) {
      return BooleanValue.newBooleanValue().value((Boolean)serialized).build();
    }

    String stringValue = serialized.toString();
    // numbers can be Int or Float values.
    if (serialized instanceof Number) {
      return handleNumber(stringValue);
    }

    if (serialized instanceof String) {
      // Enum types use Enum literals.
      if (type instanceof GraphQLEnumType) {
        return EnumValue.newEnumValue().name(stringValue).build();
      }

      // ID types can use Int literals.
      if (type == Scalars.GraphQLID && stringValue.matches("^[0-9]+$")) {
        return IntValue.newIntValue().value(new BigInteger(stringValue)).build();
      }

      return StringValue.newStringValue().value(stringValue).build();
    }

    throw new AssertException("'Cannot convert value to AST: " + serialized);
  }