Search in sources :

Example 1 with AssertException

use of graphql.AssertException in project graphql-java by graphql-java.

the class GraphQLEnumType method buildMap.

private void buildMap(List<GraphQLEnumValueDefinition> values) {
    for (GraphQLEnumValueDefinition valueDefinition : values) {
        String name = valueDefinition.getName();
        if (valueDefinitionMap.containsKey(name))
            throw new AssertException("value " + name + " redefined");
        valueDefinitionMap.put(name, valueDefinition);
    }
}
Also used : AssertException(graphql.AssertException)

Example 2 with AssertException

use of graphql.AssertException in project graphql-java by graphql-java.

the class GraphQLInterfaceType method buildDefinitionMap.

private void buildDefinitionMap(List<GraphQLFieldDefinition> fieldDefinitions) {
    for (GraphQLFieldDefinition fieldDefinition : fieldDefinitions) {
        String name = fieldDefinition.getName();
        if (fieldDefinitionsByName.containsKey(name))
            throw new AssertException(format("Duplicated definition for field '%s' in interface '%s'", name, this.name));
        fieldDefinitionsByName.put(name, fieldDefinition);
    }
}
Also used : AssertException(graphql.AssertException)

Example 3 with AssertException

use of graphql.AssertException in project graphql-java by graphql-java.

the class GraphQLObjectType method buildDefinitionMap.

private void buildDefinitionMap(List<GraphQLFieldDefinition> fieldDefinitions) {
    for (GraphQLFieldDefinition fieldDefinition : fieldDefinitions) {
        String name = fieldDefinition.getName();
        if (fieldDefinitionsByName.containsKey(name))
            throw new AssertException(format("Duplicated definition for field '%s' in type '%s'", name, this.name));
        fieldDefinitionsByName.put(name, fieldDefinition);
    }
}
Also used : AssertException(graphql.AssertException)

Example 4 with AssertException

use of graphql.AssertException in project graphql-java by graphql-java.

the class ExecutionPath method parse.

/**
 * Parses an execution path from the provided path string in the format /segment1/segment2[index]/segmentN
 *
 * @param pathString the path string
 *
 * @return a parsed execution path
 */
public static ExecutionPath parse(String pathString) {
    pathString = pathString == null ? "" : pathString;
    pathString = pathString.trim();
    StringTokenizer st = new StringTokenizer(pathString, "/[]", true);
    ExecutionPath path = ExecutionPath.rootPath();
    while (st.hasMoreTokens()) {
        String token = st.nextToken();
        if ("/".equals(token)) {
            assertTrue(st.hasMoreTokens(), mkErrMsg(), pathString);
            path = path.segment(st.nextToken());
        } else if ("[".equals(token)) {
            assertTrue(st.countTokens() >= 2, mkErrMsg(), pathString);
            path = path.segment(Integer.parseInt(st.nextToken()));
            String closingBrace = st.nextToken();
            assertTrue(closingBrace.equals("]"), mkErrMsg(), pathString);
        } else {
            throw new AssertException(format(mkErrMsg(), pathString));
        }
    }
    return path;
}
Also used : StringTokenizer(java.util.StringTokenizer) AssertException(graphql.AssertException)

Example 5 with AssertException

use of graphql.AssertException in project graphql-java by graphql-java.

the class AstValueHelper method astFromValue.

/**
 * Produces a GraphQL Value AST given a Java value.
 *
 * A GraphQL type must be provided, which will be used to interpret different
 * Java values.
 *
 * <pre>
 * |      Value    | GraphQL Value        |
 * | ------------- | -------------------- |
 * | Object        | Input Object         |
 * | Array         | List                 |
 * | Boolean       | Boolean              |
 * | String        | String / Enum Value  |
 * | Number        | Int / Float          |
 * | Mixed         | Enum Value           |
 * </pre>
 *
 * @param value - the java value to be converted into graphql ast
 * @param type  the graphql type of the object
 *
 * @return a grapql language ast {@link Value}
 */
public static Value astFromValue(Object value, GraphQLType type) {
    if (value == null) {
        return null;
    }
    if (type instanceof GraphQLNonNull) {
        return handleNonNull(value, (GraphQLNonNull) type);
    }
    // the value is not an array, convert the value using the list's item type.
    if (type instanceof GraphQLList) {
        return handleList(value, (GraphQLList) type);
    }
    // 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 new BooleanValue((Boolean) serialized);
    }
    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 new EnumValue(stringValue);
        }
        // ID types can use Int literals.
        if (type == Scalars.GraphQLID && stringValue.matches("^[0-9]+$")) {
            return new IntValue(new BigInteger(stringValue));
        }
        // String types are just strings but JSON'ised
        return new StringValue(jsonStringify(stringValue));
    }
    throw new AssertException("'Cannot convert value to AST: " + serialized);
}
Also used : GraphQLList(graphql.schema.GraphQLList) AssertException(graphql.AssertException) GraphQLEnumType(graphql.schema.GraphQLEnumType) GraphQLScalarType(graphql.schema.GraphQLScalarType) GraphQLInputObjectType(graphql.schema.GraphQLInputObjectType) GraphQLNonNull(graphql.schema.GraphQLNonNull) BigInteger(java.math.BigInteger)

Aggregations

AssertException (graphql.AssertException)6 GraphQLEnumType (graphql.schema.GraphQLEnumType)1 GraphQLInputObjectType (graphql.schema.GraphQLInputObjectType)1 GraphQLList (graphql.schema.GraphQLList)1 GraphQLNonNull (graphql.schema.GraphQLNonNull)1 GraphQLScalarType (graphql.schema.GraphQLScalarType)1 BigInteger (java.math.BigInteger)1 StringTokenizer (java.util.StringTokenizer)1