Search in sources :

Example 1 with SelectionSet

use of graphql.language.SelectionSet 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 2 with SelectionSet

use of graphql.language.SelectionSet in project graphql-java by graphql-java.

the class QueryTraversal method visitImpl.

private void visitImpl(FieldVisitor visitFieldCallback, SelectionSet selectionSet, GraphQLCompositeType type, boolean preOrder) {
    Map<Class<?>, Object> rootVars = new LinkedHashMap<>();
    rootVars.put(QueryTraversalContext.class, new QueryTraversalContext(type, null));
    FieldVisitor noOp = notUsed -> {
    };
    FieldVisitor preOrderCallback = preOrder ? visitFieldCallback : noOp;
    FieldVisitor postOrderCallback = !preOrder ? visitFieldCallback : noOp;
    NodeTraverser nodeTraverser = new NodeTraverser(rootVars, this::childrenOf);
    nodeTraverser.depthFirst(new NodeVisitorImpl(preOrderCallback, postOrderCallback), selectionSet.getSelections());
}
Also used : OperationDefinition(graphql.language.OperationDefinition) Internal(graphql.Internal) Node(graphql.language.Node) NodeUtil(graphql.language.NodeUtil) TraversalControl(graphql.util.TraversalControl) ValuesResolver(graphql.execution.ValuesResolver) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) FragmentSpread(graphql.language.FragmentSpread) LeaveOrEnter(graphql.language.NodeTraverser.LeaveOrEnter) TraverserContext(graphql.util.TraverserContext) Introspection(graphql.introspection.Introspection) LinkedHashMap(java.util.LinkedHashMap) Selection(graphql.language.Selection) Assert.assertShouldNeverHappen(graphql.Assert.assertShouldNeverHappen) NodeTraverser(graphql.language.NodeTraverser) Map(java.util.Map) GraphQLCompositeType(graphql.schema.GraphQLCompositeType) GraphQLSchema(graphql.schema.GraphQLSchema) TypeName(graphql.language.TypeName) SelectionSet(graphql.language.SelectionSet) GraphQLObjectType(graphql.schema.GraphQLObjectType) NodeVisitorStub(graphql.language.NodeVisitorStub) GraphQLUnmodifiedType(graphql.schema.GraphQLUnmodifiedType) Field(graphql.language.Field) SchemaUtil(graphql.schema.SchemaUtil) Document(graphql.language.Document) List(java.util.List) LEAVE(graphql.language.NodeTraverser.LeaveOrEnter.LEAVE) Assert.assertNotNull(graphql.Assert.assertNotNull) ConditionalNodes(graphql.execution.ConditionalNodes) FragmentDefinition(graphql.language.FragmentDefinition) InlineFragment(graphql.language.InlineFragment) NodeTraverser(graphql.language.NodeTraverser) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with SelectionSet

use of graphql.language.SelectionSet in project structr by structr.

the class GraphQLQuery method init.

// ----- private methods -----
private void init(final SecurityContext securityContext, final Class type, final Field field, final String path) throws FrameworkException {
    final QueryConfig config = getConfig(path);
    config.handleTypeArguments(securityContext, type, field.getArguments());
    final SelectionSet selectionSet = field.getSelectionSet();
    if (selectionSet != null) {
        for (final Selection selection : selectionSet.getSelections()) {
            if (selection instanceof Field) {
                final Field childField = (Field) selection;
                final SelectionSet childSet = childField.getSelectionSet();
                final PropertyKey key = StructrApp.getConfiguration().getPropertyKeyForJSONName(type, childField.getName());
                final Class relatedType = key.relatedType() != null ? key.relatedType() : type;
                // add field to property set
                config.addPropertyKey(key);
                config.handleFieldArguments(securityContext, relatedType, field, childField);
                // recurse
                if (childSet != null) {
                    init(securityContext, relatedType, childField, path + "/" + childField.getName());
                }
            }
        }
    }
}
Also used : Field(graphql.language.Field) SelectionSet(graphql.language.SelectionSet) Selection(graphql.language.Selection) PropertyKey(org.structr.core.property.PropertyKey)

Example 4 with SelectionSet

use of graphql.language.SelectionSet in project graphql-java by graphql-java.

the class GraphqlAntlrToLanguage method visitSelectionSet.

@Override
public Void visitSelectionSet(GraphqlParser.SelectionSetContext ctx) {
    SelectionSet newSelectionSet = new SelectionSet();
    newNode(newSelectionSet, ctx);
    newSelectionSet(newSelectionSet);
    addContextProperty(ContextProperty.SelectionSet, newSelectionSet);
    super.visitSelectionSet(ctx);
    popContext();
    return null;
}
Also used : SelectionSet(graphql.language.SelectionSet)

Example 5 with SelectionSet

use of graphql.language.SelectionSet in project structr by structr.

the class GraphQLRequest method initialize.

// ----- private methods -----
private void initialize(final SecurityContext securityContext, Document document) throws FrameworkException {
    for (final Node child : document.getChildren()) {
        if (child instanceof OperationDefinition) {
            final OperationDefinition operationDefinition = (OperationDefinition) child;
            final SelectionSet selectionSet = operationDefinition.getSelectionSet();
            for (final Selection selection : selectionSet.getSelections()) {
                if (selection instanceof Field) {
                    queries.add(new GraphQLQuery(securityContext, (Field) selection));
                } else {
                    logger.warn("Unknown selection set element {} in GraphQL query, ignoring.", selection.getClass());
                }
            }
        } else {
            logger.warn("Unknown document element {} in GraphQL query, ignoring.", child.getClass());
        }
    }
}
Also used : Field(graphql.language.Field) SelectionSet(graphql.language.SelectionSet) Selection(graphql.language.Selection) Node(graphql.language.Node) OperationDefinition(graphql.language.OperationDefinition)

Aggregations

SelectionSet (graphql.language.SelectionSet)5 Field (graphql.language.Field)4 Selection (graphql.language.Selection)3 Node (graphql.language.Node)2 OperationDefinition (graphql.language.OperationDefinition)2 GraphQLObjectType (graphql.schema.GraphQLObjectType)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 Assert.assertNotNull (graphql.Assert.assertNotNull)1 Assert.assertShouldNeverHappen (graphql.Assert.assertShouldNeverHappen)1 Internal (graphql.Internal)1 ConditionalNodes (graphql.execution.ConditionalNodes)1 ValuesResolver (graphql.execution.ValuesResolver)1 Introspection (graphql.introspection.Introspection)1 Document (graphql.language.Document)1 FragmentDefinition (graphql.language.FragmentDefinition)1 FragmentSpread (graphql.language.FragmentSpread)1 InlineFragment (graphql.language.InlineFragment)1 NodeTraverser (graphql.language.NodeTraverser)1 LeaveOrEnter (graphql.language.NodeTraverser.LeaveOrEnter)1