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)); } }
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)); } }
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); } }
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); }
/** * 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; }
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 ExecutionStepInfo newExecutionStepInfoForListElement(ExecutionStepInfo executionInfo, int index) { GraphQLList fieldType = (GraphQLList) executionInfo.getUnwrappedNonNullType(); GraphQLOutputType typeInList = (GraphQLOutputType) fieldType.getWrappedType(); ExecutionPath indexedPath = executionInfo.getPath().segment(index); return executionInfo.transform(builder -> builder .parentInfo(executionInfo) .type(typeInList) .path(indexedPath)); }
@SuppressWarnings("unchecked") private List<ExecutionNode> handleList(ExecutionContext executionContext, Map<String, Object> argumentValues, FetchedValues fetchedValues, String fieldName, MergedField fields, ExecutionStepInfo executionStepInfo) { GraphQLList listType = (GraphQLList) executionStepInfo.getUnwrappedNonNullType(); List<FetchedValue> flattenedValues = new ArrayList<>(); for (FetchedValue value : fetchedValues.getValues()) { MapOrList mapOrList = value.getParentResult(); if (value.getValue() == null) { mapOrList.putOrAdd(fieldName, null); continue; } MapOrList listResult = mapOrList.createAndPutList(fieldName); for (Object rawValue : toIterable(value.getValue())) { rawValue = unboxPossibleOptional(rawValue); flattenedValues.add(new FetchedValue(listResult, rawValue)); } } GraphQLOutputType innerSubType = (GraphQLOutputType) listType.getWrappedType(); ExecutionStepInfo newExecutionStepInfo = executionStepInfo.changeTypeWithPreservedNonNull((GraphQLOutputType) GraphQLTypeUtil.unwrapNonNull(innerSubType)); FetchedValues flattenedFetchedValues = new FetchedValues(flattenedValues, newExecutionStepInfo, fetchedValues.getPath()); return completeValues(executionContext, flattenedFetchedValues, newExecutionStepInfo, fieldName, fields, argumentValues); }
private GraphQLOutputType unwrapList(GraphQLOutputType type) { if (type instanceof GraphQLList) { return (GraphQLOutputType) ((GraphQLList) type).getWrappedType(); } return type; } }
private boolean isConnection(AccessibleObject obj, Class<?> klass, GraphQLOutputType type) { return obj.isAnnotationPresent(GraphQLConnection.class) && type instanceof GraphQLList && ((GraphQLList) type).getWrappedType() instanceof GraphQLObjectType; }
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)); } }
private static boolean isNullable(final graphql.schema.GraphQLType type){ if(type instanceof GraphQLList){ graphql.schema.GraphQLType wrappedType = ((GraphQLList) type).getWrappedType(); return isNullable(wrappedType); } return !(type instanceof GraphQLNonNull); }
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)); } }
private void assertIsGraphListOfListOfString(GraphQLType type) { assertTrue(type instanceof GraphQLList); GraphQLList subtype = (GraphQLList) ((GraphQLList) type).getWrappedType(); assertTrue(subtype.getWrappedType() instanceof graphql.schema.GraphQLNonNull); graphql.schema.GraphQLNonNull wrappedType = (graphql.schema.GraphQLNonNull) subtype.getWrappedType(); assertEquals(wrappedType.getWrappedType(), GraphQLString); }
private GraphQLOutputType internalGetGraphQLConnection(AccessibleObject field, GraphQLList listType, Relay relay, Map<String, graphql.schema.GraphQLType> typeRegistry) { GraphQLOutputType wrappedType = (GraphQLOutputType) listType.getWrappedType(); String connectionName = field.getAnnotation(GraphQLConnection.class).name(); connectionName = connectionName.isEmpty() ? wrappedType.getName() : connectionName; GraphQLObjectType edgeType = getActualType(relay.edgeType(connectionName, wrappedType, null, Collections.emptyList()), typeRegistry); return getActualType(relay.connectionType(connectionName, edgeType, Collections.emptyList()), typeRegistry); }
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()); } }
@Test public void optional() throws NoSuchMethodException { ProcessingElementsContainer container = testedProcessingElementsContainer(); graphql.schema.GraphQLType type = container.getDefaultTypeFunction().buildType(getClass().getMethod("optionalMethod").getReturnType(), getClass().getMethod("listMethod").getAnnotatedReturnType(),null); assertTrue(type instanceof GraphQLList); GraphQLType subtype = ((GraphQLList) type).getWrappedType(); assertTrue(subtype instanceof graphql.schema.GraphQLNonNull); GraphQLType wrappedType = (((graphql.schema.GraphQLNonNull) subtype).getWrappedType()); assertEquals(wrappedType, GraphQLString); }
@Test public void parametrizedArg() { GraphQLObjectType object = GraphQLAnnotations.object(ParametrizedArgsTest.class); GraphQLInputType t = object.getFieldDefinition("first").getArguments().get(0).getType(); assertTrue(t instanceof GraphQLList); assertEquals(((GraphQLList) t).getWrappedType(), Scalars.GraphQLString); }
public ExecutionStepInfo newExecutionStepInfoForListElement(ExecutionStepInfo executionInfo, int index) { GraphQLList fieldType = (GraphQLList) executionInfo.getUnwrappedNonNullType(); GraphQLOutputType typeInList = (GraphQLOutputType) fieldType.getWrappedType(); ExecutionPath indexedPath = executionInfo.getPath().segment(index); return executionInfo.transform(builder -> builder .parentInfo(executionInfo) .type(typeInList) .path(indexedPath)); }
private void assertGenericMapTypeMapping(String name, GraphQLOutputType expectedValueType, GraphQLOutputType graphQLOutputType) { GraphQLList listType = (GraphQLList) graphQLOutputType; // verify we contstructed an object from the map with the enum values as fields // and their type the expected value type assertEquals(GraphQLList.class, listType.getClass()); GraphQLObjectType objectType = (GraphQLObjectType) listType.getWrappedType(); assertEquals(name, objectType.getName()); assertNotNull(objectType.getFieldDefinition(MapMapper.KEY_NAME)); assertNotNull(objectType.getFieldDefinition(MapMapper.VALUE_NAME)); assertEquals(expectedValueType.getClass(), objectType.getFieldDefinition(MapMapper.KEY_NAME).getType().getClass()); assertEquals(expectedValueType.getClass(), objectType.getFieldDefinition(MapMapper.VALUE_NAME).getType().getClass()); }