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;
}
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());
}
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());
}
}
}
}
}
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;
}
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());
}
}
}
Aggregations