Search in sources :

Example 6 with GraphQLType

use of graphql.schema.GraphQLType in project graphql-java by graphql-java.

the class OverlappingFieldsCanBeMerged method collectFieldsForInlineFragment.

private void collectFieldsForInlineFragment(Map<String, List<FieldAndType>> fieldMap, Set<String> visitedFragmentSpreads, GraphQLType parentType, InlineFragment inlineFragment) {
    GraphQLType graphQLType = inlineFragment.getTypeCondition() != null ? (GraphQLOutputType) TypeFromAST.getTypeFromAST(getValidationContext().getSchema(), inlineFragment.getTypeCondition()) : parentType;
    collectFields(fieldMap, inlineFragment.getSelectionSet(), graphQLType, visitedFragmentSpreads);
}
Also used : GraphQLType(graphql.schema.GraphQLType)

Example 7 with GraphQLType

use of graphql.schema.GraphQLType in project graphql-java by graphql-java.

the class OverlappingFieldsCanBeMerged method findConflict.

@SuppressWarnings("ConstantConditions")
private Conflict findConflict(String responseName, FieldAndType fieldAndType1, FieldAndType fieldAndType2) {
    Field field1 = fieldAndType1.field;
    Field field2 = fieldAndType2.field;
    GraphQLType type1 = fieldAndType1.graphQLType;
    GraphQLType type2 = fieldAndType2.graphQLType;
    String fieldName1 = field1.getName();
    String fieldName2 = field2.getName();
    if (isAlreadyChecked(field1, field2)) {
        return null;
    }
    alreadyChecked.add(new FieldPair(field1, field2));
    // thus may not safely diverge.
    if (!sameType(fieldAndType1.parentType, fieldAndType1.parentType) && fieldAndType1.parentType instanceof GraphQLObjectType && fieldAndType2.parentType instanceof GraphQLObjectType) {
        return null;
    }
    if (!fieldName1.equals(fieldName2)) {
        String reason = String.format("%s: %s and %s are different fields", responseName, fieldName1, fieldName2);
        return new Conflict(responseName, reason, field1, field2);
    }
    if (!sameType(type1, type2)) {
        String name1 = type1 != null ? type1.getName() : "null";
        String name2 = type2 != null ? type2.getName() : "null";
        String reason = String.format("%s: they return differing types %s and %s", responseName, name1, name2);
        return new Conflict(responseName, reason, field1, field2);
    }
    if (!sameArguments(field1.getArguments(), field2.getArguments())) {
        String reason = String.format("%s: they have differing arguments", responseName);
        return new Conflict(responseName, reason, field1, field2);
    }
    SelectionSet selectionSet1 = field1.getSelectionSet();
    SelectionSet selectionSet2 = field2.getSelectionSet();
    if (selectionSet1 != null && selectionSet2 != null) {
        Set<String> visitedFragmentSpreads = new LinkedHashSet<>();
        Map<String, List<FieldAndType>> subFieldMap = new LinkedHashMap<>();
        collectFields(subFieldMap, selectionSet1, type1, visitedFragmentSpreads);
        collectFields(subFieldMap, selectionSet2, type2, visitedFragmentSpreads);
        List<Conflict> subConflicts = findConflicts(subFieldMap);
        if (subConflicts.size() > 0) {
            String reason = String.format("%s: %s", responseName, joinReasons(subConflicts));
            List<Field> fields = new ArrayList<>();
            fields.add(field1);
            fields.add(field2);
            fields.addAll(collectFields(subConflicts));
            return new Conflict(responseName, reason, fields);
        }
    }
    return null;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SelectionSet(graphql.language.SelectionSet) GraphQLType(graphql.schema.GraphQLType) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Field(graphql.language.Field) FieldsConflict(graphql.validation.ValidationErrorType.FieldsConflict) GraphQLObjectType(graphql.schema.GraphQLObjectType) ArrayList(java.util.ArrayList) List(java.util.List)

Example 8 with GraphQLType

use of graphql.schema.GraphQLType in project graphql-java by graphql-java.

the class PossibleFragmentSpreads method checkFragmentSpread.

@Override
public void checkFragmentSpread(FragmentSpread fragmentSpread) {
    FragmentDefinition fragment = getValidationContext().getFragment(fragmentSpread.getName());
    if (fragment == null)
        return;
    GraphQLType typeCondition = TypeFromAST.getTypeFromAST(getValidationContext().getSchema(), fragment.getTypeCondition());
    GraphQLCompositeType parentType = getValidationContext().getParentType();
    if (typeCondition == null || parentType == null)
        return;
    if (!doTypesOverlap(typeCondition, parentType)) {
        String message = String.format("Fragment %s cannot be spread here as objects of " + "type %s can never be of type %s", fragmentSpread.getName(), parentType, typeCondition);
        addError(ValidationErrorType.InvalidFragmentType, fragmentSpread.getSourceLocation(), message);
    }
}
Also used : FragmentDefinition(graphql.language.FragmentDefinition) GraphQLCompositeType(graphql.schema.GraphQLCompositeType) GraphQLType(graphql.schema.GraphQLType)

Example 9 with GraphQLType

use of graphql.schema.GraphQLType in project graphql-java by graphql-java.

the class VariablesAreInputTypes method checkVariableDefinition.

@Override
public void checkVariableDefinition(VariableDefinition variableDefinition) {
    TypeName unmodifiedAstType = getValidationUtil().getUnmodifiedType(variableDefinition.getType());
    GraphQLType type = getValidationContext().getSchema().getType(unmodifiedAstType.getName());
    if (type == null)
        return;
    if (!schemaUtil.isInputType(type)) {
        String message = "Wrong type for a variable";
        addError(ValidationErrorType.NonInputTypeOnVariable, variableDefinition.getSourceLocation(), message);
    }
}
Also used : TypeName(graphql.language.TypeName) GraphQLType(graphql.schema.GraphQLType)

Example 10 with GraphQLType

use of graphql.schema.GraphQLType in project graphql-java by graphql-java.

the class TraversalContext method enterImpl.

private void enterImpl(VariableDefinition variableDefinition) {
    GraphQLType type = TypeFromAST.getTypeFromAST(schema, variableDefinition.getType());
    addInputType(type != null ? (GraphQLInputType) type : null);
}
Also used : GraphQLInputType(graphql.schema.GraphQLInputType) GraphQLType(graphql.schema.GraphQLType)

Aggregations

GraphQLType (graphql.schema.GraphQLType)24 GraphQLList (graphql.schema.GraphQLList)6 GraphQLNonNull (graphql.schema.GraphQLNonNull)5 GraphQLObjectType (graphql.schema.GraphQLObjectType)5 GraphQLInputObjectType (graphql.schema.GraphQLInputObjectType)4 GraphQLInputType (graphql.schema.GraphQLInputType)4 LinkedHashMap (java.util.LinkedHashMap)4 TypeName (graphql.language.TypeName)3 GraphQLEnumType (graphql.schema.GraphQLEnumType)3 GraphQLInputObjectField (graphql.schema.GraphQLInputObjectField)3 GraphQLScalarType (graphql.schema.GraphQLScalarType)3 GraphqlFieldVisibility (graphql.schema.visibility.GraphqlFieldVisibility)3 ArrayList (java.util.ArrayList)3 Stack (java.util.Stack)3 PublicApi (graphql.PublicApi)2 ArrayValue (graphql.language.ArrayValue)2 InputObjectTypeDefinition (graphql.language.InputObjectTypeDefinition)2 Value (graphql.language.Value)2 GraphQLCompositeType (graphql.schema.GraphQLCompositeType)2 HashMap (java.util.HashMap)2