Java Code Examples for graphql.language.StringValue

The following examples show how to use graphql.language.StringValue. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: besu   Source File: Scalars.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Object parseLiteral(final Object input) throws CoercingParseLiteralException {
  try {
    if (input instanceof IntValue) {
      return ((IntValue) input).getValue().longValue();
    } else if (input instanceof StringValue) {
      final String value = ((StringValue) input).getValue().toLowerCase();
      if (value.startsWith("0x")) {
        return Bytes.fromHexStringLenient(value).toLong();
      } else {
        return Long.parseLong(value);
      }
    }
  } catch (final NumberFormatException e) {
    // fall through
  }
  throw new CoercingParseLiteralException("Value is not any Long : '" + input + "'");
}
 
Example 2
Source Project: besu   Source File: Scalars.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Address parseLiteral(final Object input) throws CoercingParseLiteralException {
  if (!(input instanceof StringValue)) {
    throw new CoercingParseLiteralException("Value is not any Address : '" + input + "'");
  }
  try {
    return Address.fromHexStringStrict(((StringValue) input).getValue());
  } catch (final IllegalArgumentException e) {
    throw new CoercingParseLiteralException("Value is not any Address : '" + input + "'");
  }
}
 
Example 3
Source Project: besu   Source File: Scalars.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public UInt256 parseLiteral(final Object input) throws CoercingParseLiteralException {
  try {
    if (input instanceof StringValue) {
      return UInt256.fromHexString(((StringValue) input).getValue());
    } else if (input instanceof IntValue) {
      return UInt256.valueOf(((IntValue) input).getValue());
    }
  } catch (final IllegalArgumentException e) {
    // fall through
  }
  throw new CoercingParseLiteralException("Value is not any BigInt : '" + input + "'");
}
 
Example 4
Source Project: besu   Source File: Scalars.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Bytes parseLiteral(final Object input) throws CoercingParseLiteralException {
  if (!(input instanceof StringValue)) {
    throw new CoercingParseLiteralException("Value is not any Bytes : '" + input + "'");
  }
  try {
    return Bytes.fromHexStringLenient(((StringValue) input).getValue());
  } catch (final IllegalArgumentException e) {
    throw new CoercingParseLiteralException("Value is not any Bytes : '" + input + "'");
  }
}
 
Example 5
Source Project: besu   Source File: Scalars.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Bytes32 parseLiteral(final Object input) throws CoercingParseLiteralException {
  if (!(input instanceof StringValue)) {
    throw new CoercingParseLiteralException("Value is not any Bytes32 : '" + input + "'");
  }
  try {
    return Bytes32.fromHexStringLenient(((StringValue) input).getValue());
  } catch (final IllegalArgumentException e) {
    throw new CoercingParseLiteralException("Value is not any Bytes32 : '" + input + "'");
  }
}
 
Example 6
Source Project: federation-jvm   Source File: _Any.java    License: MIT License 5 votes vote down vote up
@Nullable
@Override
public Object parseLiteral(Object input) throws CoercingParseLiteralException {
    if (input instanceof NullValue) {
        return null;
    } else if (input instanceof FloatValue) {
        return ((FloatValue) input).getValue();
    } else if (input instanceof StringValue) {
        return ((StringValue) input).getValue();
    } else if (input instanceof IntValue) {
        return ((IntValue) input).getValue();
    } else if (input instanceof BooleanValue) {
        return ((BooleanValue) input).isValue();
    } else if (input instanceof EnumValue) {
        return ((EnumValue) input).getName();
    } else if (input instanceof ArrayValue) {
        return ((ArrayValue) input).getValues()
                .stream()
                .map(this::parseLiteral)
                .collect(Collectors.toList());
    } else if (input instanceof ObjectValue) {
        return ((ObjectValue) input).getObjectFields()
                .stream()
                .collect(Collectors.toMap(ObjectField::getName, f -> parseLiteral(f.getValue())));
    }
    return Assert.assertShouldNeverHappen();
}
 
Example 7
Source Project: smallrye-graphql   Source File: DateCoercing.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Object parseLiteral(Object input) {
    if (input == null)
        return null;

    if (input instanceof StringValue) {
        // We need to get a String value of this date
        return ((StringValue) input).getValue();
    } else {
        throw msg.coercingParseLiteralException(input.getClass().getSimpleName());
    }

}
 
Example 8
Source Project: stream-registry   Source File: JsonCoercingUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static Object parseLiteral(Object input, Map<String, Object> variables) throws CoercingParseLiteralException {
  if (!(input instanceof Value)) {
    log.error("Expected 'Value', got: {}", input);
    throw new CoercingParseLiteralException("Expected 'Value', got: " + input);
  }
  Object result = null;
  if (input instanceof StringValue) {
    result = ((StringValue) input).getValue();
  } else if (input instanceof IntValue) {
    result = ((IntValue) input).getValue();
  } else if (input instanceof FloatValue) {
    result = ((FloatValue) input).getValue();
  } else if (input instanceof BooleanValue) {
    result = ((BooleanValue) input).isValue();
  } else if (input instanceof EnumValue) {
    result = ((EnumValue) input).getName();
  } else if (input instanceof VariableReference) {
    result = variables.get(((VariableReference) input).getName());
  } else if (input instanceof ArrayValue) {
    result = ((ArrayValue) input).getValues().stream()
        .map(v -> parseLiteral(v, variables))
        .collect(toList());
  } else if (input instanceof ObjectValue) {
    result = ((ObjectValue) input).getObjectFields().stream()
        .collect(toMap(ObjectField::getName, f -> parseLiteral(f.getValue(), variables)));
  }
  return result;
}
 
Example 9
Source Project: stream-registry   Source File: ObjectNodeCoercingTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void parseLiteral() {
  Value input = ObjectValue.newObjectValue()
      .objectField(ObjectField.newObjectField()
          .name("a")
          .value(StringValue.newStringValue("b").build())
          .build())
      .build();

  ObjectNodeCoercing spy = Mockito.spy(underTest);
  Object parsed = new Object();
  when(spy.parseLiteral(input, emptyMap())).thenReturn(parsed);
  Object result = spy.parseLiteral(input);
  assertThat(result, is(sameInstance(parsed)));
}
 
Example 10
Source Project: stream-registry   Source File: JsonCoercingUtilTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void arrayValue() {
  Value value = ArrayValue.newArrayValue()
      .value(StringValue.newStringValue("a").build())
      .build();
  Object result = JsonCoercingUtil.parseLiteral(value, emptyMap());
  assertThat(result, is(List.of("a")));
}
 
Example 11
Source Project: stream-registry   Source File: JsonCoercingUtilTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void objectValue() {
  Value value = ObjectValue.newObjectValue()
      .objectField(ObjectField.newObjectField()
          .name("a")
          .value(StringValue.newStringValue("b").build())
          .build())
      .build();
  Object result = JsonCoercingUtil.parseLiteral(value, emptyMap());
  assertThat(result, is(Map.of("a", "b")));
}
 
Example 12
Source Project: rejoiner   Source File: ProtoScalars.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public ByteString parseLiteral(Object input) throws CoercingParseLiteralException {
  if (input instanceof StringValue) {
    return ByteString.copyFromUtf8(((StringValue) input).getValue());
  }
  return null;
}
 
Example 13
Source Project: graphql-spqr   Source File: GsonScalars.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public JsonElement parseLiteral(Object input) {
    if (input instanceof ObjectValue || input instanceof ArrayValue) {
        throw literalParsingException(input, StringValue.class, BooleanValue.class, EnumValue.class,
                FloatValue.class, IntValue.class, NullValue.class);
    }
    return parseJsonValue(((Value) input));
}
 
Example 14
Source Project: graphql-spqr   Source File: GsonScalars.java    License: Apache License 2.0 5 votes vote down vote up
private static JsonElement parseJsonValue(Value value) {
    if (value instanceof BooleanValue) {
        return new JsonPrimitive(((BooleanValue) value).isValue());
    }
    if (value instanceof EnumValue) {
        return new JsonPrimitive(((EnumValue) value).getName());
    }
    if (value instanceof FloatValue) {
        return new JsonPrimitive(((FloatValue) value).getValue());
    }
    if (value instanceof IntValue) {
        return new JsonPrimitive(((IntValue) value).getValue());
    }
    if (value instanceof NullValue) {
        return JsonNull.INSTANCE;
    }
    if (value instanceof StringValue) {
        return new JsonPrimitive(((StringValue) value).getValue());
    }
    if (value instanceof ArrayValue) {
        List<Value> values = ((ArrayValue) value).getValues();
        JsonArray jsonArray = new JsonArray(values.size());
        values.forEach(v -> jsonArray.add(parseJsonValue(v)));
        return jsonArray;
    }
    if (value instanceof ObjectValue) {
        final JsonObject result = new JsonObject();
        ((ObjectValue) value).getObjectFields().forEach(objectField ->
                result.add(objectField.getName(), parseJsonValue(objectField.getValue())));
        return result;
    }
    //Should never happen, as it would mean the variable was not replaced by the parser
    throw new CoercingParseLiteralException("Unknown scalar AST type: " + value.getClass().getName());
}
 
Example 15
Source Project: graphql-spqr   Source File: JacksonObjectScalars.java    License: Apache License 2.0 5 votes vote down vote up
private static JsonNode parseJsonValue(Value value, Map<String, Object> variables) {
    if (value instanceof BooleanValue) {
        return JsonNodeFactory.instance.booleanNode(((BooleanValue) value).isValue());
    }
    if (value instanceof EnumValue) {
        return JsonNodeFactory.instance.textNode(((EnumValue) value).getName());
    }
    if (value instanceof FloatValue) {
        return JsonNodeFactory.instance.numberNode(((FloatValue) value).getValue());
    }
    if (value instanceof IntValue) {
        return JsonNodeFactory.instance.numberNode(((IntValue) value).getValue());
    }
    if (value instanceof NullValue) {
        return JsonNodeFactory.instance.nullNode();
    }
    if (value instanceof StringValue) {
        return JsonNodeFactory.instance.textNode(((StringValue) value).getValue());
    }
    if (value instanceof ArrayValue) {
        List<Value> values = ((ArrayValue) value).getValues();
        ArrayNode jsonArray = JsonNodeFactory.instance.arrayNode(values.size());
        values.forEach(v -> jsonArray.add(parseJsonValue(v, variables)));
        return jsonArray;
    }
    if (value instanceof VariableReference) {
        return OBJECT_MAPPER.convertValue(variables.get(((VariableReference) value).getName()), JsonNode.class);
    }
    if (value instanceof ObjectValue) {
        final ObjectNode result = JsonNodeFactory.instance.objectNode();
        ((ObjectValue) value).getObjectFields().forEach(objectField ->
                result.set(objectField.getName(), parseJsonValue(objectField.getValue(), variables)));
        return result;
    }
    //Should never happen
    throw new CoercingParseLiteralException("Unknown scalar AST type: " + value.getClass().getName());
}
 
Example 16
Source Project: graphql-spqr   Source File: Scalars.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public URL parseLiteral(Object input) {
    StringValue string = literalOrException(input, StringValue.class);
    try {
        return new URL(string.getValue());
    } catch (MalformedURLException e) {
        throw new CoercingParseLiteralException("Input string \"" + input + "\" could not be parsed into a URL", e);
    }
}
 
Example 17
Source Project: graphql-spqr   Source File: Scalars.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public byte[] parseLiteral(Object input) {
    StringValue string = literalOrException(input, StringValue.class);
    try {
        return Base64.getDecoder().decode(string.getValue());
    } catch (IllegalArgumentException e) {
        throw new CoercingParseLiteralException("Input string \"" + input + "\" is not a valid Base64 value", e);
    }
}
 
Example 18
Source Project: graphql-spqr   Source File: TemporalScalarsTest.java    License: Apache License 2.0 5 votes vote down vote up
private void testStringTemporal(Class type, Coercing coercing, String expected, String stringLiteral) {
    Object parsed = coercing.parseLiteral(new StringValue(stringLiteral));
    assertTrue(type.isInstance(parsed));
    assertEquals(expected, coercing.serialize(parsed));

    parsed = coercing.parseValue(stringLiteral);
    assertTrue(type.isInstance(parsed));
    assertEquals(expected, coercing.serialize(parsed));
    
    Object same = coercing.parseValue(parsed);
    assertEquals(parsed, same);
}
 
Example 19
private List<Directive> createDeprecatedDirective(Map<String, Object> field) {
    if ((Boolean) field.get("isDeprecated")) {
        String reason = (String) field.get("deprecationReason");
        if (reason == null) {
            reason = "No longer supported"; // default according to spec
        }
        Argument reasonArg = new Argument("reason", new StringValue(reason));
        return Collections.singletonList(new Directive("deprecated", Collections.singletonList(reasonArg)));
    }
    return Collections.emptyList();
}
 
Example 20
Source Project: graphql-jpa   Source File: JavaScalars.java    License: MIT License 5 votes vote down vote up
@Override
public Object parseLiteral(Object input) {
    if (input instanceof StringValue) {
        return parseStringToLocalDateTime(((StringValue) input).getValue());
    } else if (input instanceof IntValue) {
        BigInteger value = ((IntValue) input).getValue();
        return parseLongToLocalDateTime(value.longValue());
    }
    return null;
}
 
Example 21
Source Project: graphql-jpa   Source File: JavaScalars.java    License: MIT License 5 votes vote down vote up
@Override
public Object parseLiteral(Object input) {
    if (input instanceof StringValue) {
        return parseStringToLocalDate(((StringValue) input).getValue());
    } else if (input instanceof IntValue) {
        BigInteger value = ((IntValue) input).getValue();
        return parseLongToLocalDate(value.longValue());
    }
    return null;
}
 
Example 22
Source Project: graphql-jpa   Source File: JavaScalars.java    License: MIT License 5 votes vote down vote up
@Override
public Object parseLiteral(Object input) {
    if (input instanceof StringValue) {
        return parseStringToDate(((StringValue) input).getValue());
    } else if (input instanceof IntValue) {
        BigInteger value = ((IntValue) input).getValue();
        return new Date(value.longValue());
    }
    return null;
}
 
Example 23
Source Project: graphql-jpa   Source File: JavaScalars.java    License: MIT License 5 votes vote down vote up
@Override
public Object parseLiteral(Object input) {
    if (input instanceof StringValue) {
        return parseStringToUUID(((StringValue) input).getValue());
    }
    return null;
}
 
Example 24
/**
 * Extracts a scalar value, this is needed because of GraphQL strict types
 */
protected Object getRealValue(Value value, DataFetchingEnvironment env) {
    if (value instanceof BooleanValue) {
        return ((BooleanValue)value).isValue();
    } else if (value instanceof FloatValue) {
        return ((FloatValue) value).getValue();
    } else if (value instanceof IntValue) {
        return ((IntValue) value).getValue();
    } else if (value instanceof StringValue) {
        return ((StringValue) value).getValue();
    } else if (value instanceof VariableReference) {
        return env.getVariables().get(((VariableReference) value).getName());
    }
    return null;
}
 
Example 25
Source Project: timbuctoo   Source File: DirectiveRetriever.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static Optional<String> getDirectiveArgument(GraphQLObjectType parentType, String directiveName,
                                                    String argumentName) {
  return Optional.ofNullable(parentType.getDefinition().getDirective(directiveName))
    .map(d -> d.getArgument(argumentName))
    .map(v -> (StringValue) v.getValue())
    .map(StringValue::getValue);
}
 
Example 26
Source Project: timbuctoo   Source File: DirectiveRetriever.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static Optional<String> getDirectiveArgument(GraphQLFieldDefinition field, String directiveName,
                                                    String argumentName) {
  return Optional.ofNullable(field.getDefinition().getDirective(directiveName))
    .map(d -> d.getArgument(argumentName))
    .map(v -> (StringValue) v.getValue())
    .map(StringValue::getValue);
}
 
Example 27
Source Project: stream-registry   Source File: JsonCoercingUtilTest.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void stringValue() {
  Value value = StringValue.newStringValue("a").build();
  Object result = JsonCoercingUtil.parseLiteral(value, emptyMap());
  assertThat(result, is("a"));
}
 
Example 28
Source Project: graphql-spqr   Source File: JacksonScalars.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public TextNode parseLiteral(Object input) {
    return TextNode.valueOf(literalOrException(input, StringValue.class).getValue());
}
 
Example 29
Source Project: graphql-spqr   Source File: JacksonScalars.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public BinaryNode parseLiteral(Object input) {
    return new BinaryNode(decoder.decode(literalOrException(input, StringValue.class).getValue()));
}
 
Example 30
Source Project: graphql-spqr   Source File: Scalars.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public UUID parseLiteral(Object input) {
    StringValue string = literalOrException(input, StringValue.class);
    return UUID.fromString(string.getValue());
}