/** * A factory method for creating list types so that when used with static imports allows * more readable code such as * {@code .type(list(GraphQLString)) } * * @param wrappedType the type to wrap as being a list * * @return a GraphQLList of that wrapped type */ public static GraphQLList list(GraphQLType wrappedType) { return new GraphQLList(wrappedType); }
private List coerceValueForList(GraphqlFieldVisibility fieldVisibility, VariableDefinition variableDefinition, String inputName, GraphQLList graphQLList, Object value) { if (value instanceof Iterable) { List<Object> result = new ArrayList<>(); for (Object val : (Iterable) value) { result.add(coerceValue(fieldVisibility, variableDefinition, inputName, graphQLList.getWrappedType(), val)); } return result; } else { return Collections.singletonList(coerceValue(fieldVisibility, variableDefinition, inputName, graphQLList.getWrappedType(), value)); } }
/** * This will decorate a graphql type with the original hierarchy of non null and list'ness * it originally contained in its definition type * * @param objectType this should be a graphql type that was originally built from this raw type * @param <T> the type * * @return the decorated type */ @SuppressWarnings("TypeParameterUnusedInFormals") public <T extends GraphQLType> T decorate(GraphQLType objectType) { GraphQLType out = objectType; Stack<Class<?>> wrappingStack = new Stack<>(); wrappingStack.addAll(this.decoration); while (!wrappingStack.isEmpty()) { Class<?> clazz = wrappingStack.pop(); if (clazz.equals(NonNullType.class)) { out = nonNull(out); } if (clazz.equals(ListType.class)) { out = list(out); } } // we handle both input and output graphql types //noinspection unchecked return (T) out; }
@Override public TraversalControl visitGraphQLList(GraphQLList node, TraverserContext<GraphQLType> context) { node.replaceType(resolvedType); return super.visitGraphQLList(node, context); }
@Override public TraversalControl visitGraphQLList(GraphQLList node, TraverserContext<GraphQLType> context) { node.replaceType(resolvedType); return super.visitGraphQLList(node, context); }
.field(GraphQLFieldDefinition.newFieldDefinition() .name("stoptimes") .type(new GraphQLList(stoptimeType)) .dataFetcher(environment -> ((StopTimesInPattern) environment.getSource()).times) .build()) .field(GraphQLFieldDefinition.newFieldDefinition() .name("stops") .type(new GraphQLList(new GraphQLNonNull(stopType))) .dataFetcher(environment -> ((StopCluster) environment.getSource()).children) .build()) .field(GraphQLFieldDefinition.newFieldDefinition() .name("routes") .type(new GraphQLList(new GraphQLNonNull(routeType))) .dataFetcher(environment -> index.patternsForStop .get((Stop) environment.getSource()) .field(GraphQLFieldDefinition.newFieldDefinition() .name("patterns") .type(new GraphQLList(patternType)) .dataFetcher(environment -> index.patternsForStop .get((Stop) environment.getSource())) .field(GraphQLFieldDefinition.newFieldDefinition() .name("transfers") //TODO: add max distance as parameter? .type(new GraphQLList(stopAtDistanceType)) .dataFetcher(environment -> index.stopVertexForStop .get(environment.getSource())
private Object coerceValueAstForList(GraphqlFieldVisibility fieldVisibility, GraphQLList graphQLList, Value value, Map<String, Object> variables) { if (value instanceof ArrayValue) { ArrayValue arrayValue = (ArrayValue) value; List<Object> result = new ArrayList<>(); for (Value singleValue : arrayValue.getValues()) { result.add(coerceValueAst(fieldVisibility, graphQLList.getWrappedType(), singleValue, variables)); } return result; } else { return Collections.singletonList(coerceValueAst(fieldVisibility, graphQLList.getWrappedType(), value, variables)); } }
/** * We support the basic types as directive types * * @param value the value to use * * @return a graphql input type */ public GraphQLInputType buildDirectiveInputType(Value value) { if (value instanceof NullValue) { return Scalars.GraphQLString; } if (value instanceof FloatValue) { return Scalars.GraphQLFloat; } if (value instanceof StringValue) { return Scalars.GraphQLString; } if (value instanceof IntValue) { return Scalars.GraphQLInt; } if (value instanceof BooleanValue) { return Scalars.GraphQLBoolean; } if (value instanceof ArrayValue) { ArrayValue arrayValue = (ArrayValue) value; return list(buildDirectiveInputType(getArrayValueWrappedType(arrayValue))); } return assertShouldNeverHappen("Directive values of type '%s' are not supported yet", value.getClass().getSimpleName()); }
/** * A factory method for creating list types so that when used with static imports allows * more readable code such as * {@code .type(list(GraphQLString)) } * * @param wrappedType the type to wrap as being a list * * @return a GraphQLList of that wrapped type */ public static GraphQLList list(GraphQLType wrappedType) { return new GraphQLList(wrappedType); }
private boolean isValidLiteralValue(Value value, GraphQLList type, GraphQLSchema schema) { GraphQLType wrappedType = type.getWrappedType(); if (value instanceof ArrayValue) { List<Value> values = ((ArrayValue) value).getValues(); for (int i = 0; i < values.size(); i++) { if (!isValidLiteralValue(values.get(i), wrappedType, schema)) { handleFieldNotValidError(values.get(i), wrappedType, i); return false; } } return true; } else { return isValidLiteralValue(value, wrappedType, schema); } }
public static GraphQLList list (GraphQLType aWrappedType) { return new GraphQLList(aWrappedType); }
private static Value handleList(Object _value, GraphQLList type) { GraphQLType itemType = type.getWrappedType(); boolean isIterable = _value instanceof Iterable; if (isIterable || (_value != null && _value.getClass().isArray())) { Iterable iterable = isIterable ? (Iterable) _value : FpKit.toCollection(_value); List<Value> valuesNodes = new ArrayList<>(); for (Object item : iterable) { Value itemNode = astFromValue(item, itemType); if (itemNode != null) { valuesNodes.add(itemNode); } } return ArrayValue.newArrayValue().values(valuesNodes).build(); } return astFromValue(_value, itemType); }
public static GraphQLType getTypeFromAST(GraphQLSchema schema, Type type) { GraphQLType innerType; if (type instanceof ListType) { innerType = getTypeFromAST(schema, ((ListType) type).getType()); return innerType != null ? list(innerType) : null; } else if (type instanceof NonNullType) { innerType = getTypeFromAST(schema, ((NonNullType) type).getType()); return innerType != null ? nonNull(innerType) : null; } return schema.getType(((TypeName) type).getName()); } }
public static GraphQLList list (GraphQLType aWrappedType) { return new GraphQLList(aWrappedType); }
/** * Unwraps one layer of the type or just returns the type again if its not a wrapped type * * @param type the type to unwrapOne * * @return the unwrapped type or the same type again if its not wrapped */ public static GraphQLType unwrapOne(GraphQLType type) { if (isNonNull(type)) { return ((GraphQLNonNull) type).getWrappedType(); } else if (isList(type)) { return ((GraphQLList) type).getWrappedType(); } return type; }
void recursiveTypes() { GraphQLObjectType person = newObject() .name("Person") .field(newFieldDefinition() .name("friends") .type(GraphQLList.list(GraphQLTypeReference.typeRef("Person")))) .build(); }
@Override public GraphQLType wrapType(GraphQLType interiorType, TypeSpecContainer originalObject) { if (!canWrapList(originalObject)) { return interiorType; } return new GraphQLList(interiorType); }
private GraphQLType unwrapNonNull(GraphQLNonNull type) { if (isList(type.getWrappedType())) { //we only care about [type!]! i.e. non-null lists of non-nulls GraphQLList listType = (GraphQLList) type.getWrappedType(); if (isNonNull(listType.getWrappedType())) { return unwrapAll(listType.getWrappedType()); } else { return type.getWrappedType(); } } else { return unwrapAll(type.getWrappedType()); } }
public GraphQLObjectType connectionType(String name, GraphQLObjectType edgeType, List<GraphQLFieldDefinition> connectionFields) { return newObject() .name(name + "Connection") .description("A connection to a list of items.") .field(newFieldDefinition() .name("edges") .description("a list of edges") .type(list(edgeType))) .field(newFieldDefinition() .name("pageInfo") .description("details about this specific page") .type(nonNull(pageInfoType))) .fields(connectionFields) .build(); }