use of graphql.schema.GraphQLSchemaElement in project graphql-java by graphql-java.
the class SchemaTransformExamples method example_commands.
void example_commands() {
GraphQLSchemaElement updatedElement = null;
GraphQLSchemaElement newElement = null;
GraphQLTypeVisitorStub visitor = new GraphQLTypeVisitorStub() {
@Override
public TraversalControl visitGraphQLObjectType(GraphQLObjectType objectType, TraverserContext<GraphQLSchemaElement> context) {
// changes the current element in the schema
changeNode(context, updatedElement);
// inserts a new element after the current one in the schema
insertAfter(context, newElement);
// inserts a new element before the current one in teh schema
insertBefore(context, newElement);
// deletes the current element from the schema
deleteNode(context);
return CONTINUE;
}
};
}
use of graphql.schema.GraphQLSchemaElement in project graphql-java by graphql-java.
the class SchemaPrinter method print.
public String print(List<GraphQLSchemaElement> elements) {
StringWriter sw = new StringWriter();
PrintWriter out = new PrintWriter(sw);
for (GraphQLSchemaElement element : elements) {
if (element instanceof GraphQLDirective) {
out.print(print(((GraphQLDirective) element)));
} else if (element instanceof GraphQLType) {
printSchemaElement(out, element, DEFAULT_FIELD_VISIBILITY);
} else {
Assert.assertShouldNeverHappen("How did we miss a %s", element.getClass());
}
}
return sw.toString();
}
use of graphql.schema.GraphQLSchemaElement in project graphql-java by graphql-java.
the class SchemaPrinter method directiveString.
private String directiveString(GraphQLAppliedDirective directive) {
if (!options.getIncludeSchemaElement().test(directive)) {
return "";
}
if (!options.getIncludeDirective().test(directive.getName())) {
// @deprecated is special - we always print it if something is deprecated
if (!isDeprecatedDirective(directive)) {
return "";
}
}
StringBuilder sb = new StringBuilder();
sb.append("@").append(directive.getName());
Comparator<? super GraphQLSchemaElement> comparator = getComparator(GraphQLAppliedDirective.class, GraphQLAppliedDirectiveArgument.class);
List<GraphQLAppliedDirectiveArgument> args = directive.getArguments();
args = args.stream().filter(arg -> arg.getArgumentValue().isSet()).sorted(comparator).collect(toList());
if (!args.isEmpty()) {
sb.append("(");
for (int i = 0; i < args.size(); i++) {
GraphQLAppliedDirectiveArgument arg = args.get(i);
String argValue = null;
if (arg.hasSetValue()) {
argValue = printAst(arg.getArgumentValue(), arg.getType());
}
if (!isNullOrEmpty(argValue)) {
sb.append(arg.getName());
sb.append(" : ");
sb.append(argValue);
if (i < args.size() - 1) {
sb.append(", ");
}
}
}
sb.append(")");
}
return sb.toString();
}
use of graphql.schema.GraphQLSchemaElement in project graphql-java by graphql-java.
the class SchemaPrinter method argsString.
String argsString(Class<? extends GraphQLSchemaElement> parent, List<GraphQLArgument> arguments) {
boolean hasDescriptions = arguments.stream().anyMatch(this::hasDescription);
String halfPrefix = hasDescriptions ? " " : "";
String prefix = hasDescriptions ? " " : "";
int count = 0;
StringBuilder sb = new StringBuilder();
Comparator<? super GraphQLSchemaElement> comparator = getComparator(parent, GraphQLArgument.class);
arguments = arguments.stream().sorted(comparator).filter(options.getIncludeSchemaElement()).collect(toList());
for (GraphQLArgument argument : arguments) {
if (count == 0) {
sb.append("(");
} else {
sb.append(", ");
}
if (hasDescriptions) {
sb.append("\n");
}
sb.append(printComments(argument, prefix));
sb.append(prefix).append(argument.getName()).append(": ").append(typeString(argument.getType()));
if (argument.hasSetDefaultValue()) {
InputValueWithState defaultValue = argument.getArgumentDefaultValue();
sb.append(" = ");
sb.append(printAst(defaultValue, argument.getType()));
}
DirectivesUtil.toAppliedDirectives(argument).stream().filter(options.getIncludeSchemaElement()).map(this::directiveString).filter(it -> !it.isEmpty()).forEach(directiveString -> sb.append(" ").append(directiveString));
count++;
}
if (count > 0) {
if (hasDescriptions) {
sb.append("\n");
}
sb.append(halfPrefix).append(")");
}
return sb.toString();
}
use of graphql.schema.GraphQLSchemaElement in project graphql-java by graphql-java.
the class SchemaUsageSupport method getSchemaUsage.
/**
* This builds out {@link SchemaUsage} statistics about the usage of types and directives within a schema
*
* @param schema the schema to check
*
* @return usage stats
*/
public static SchemaUsage getSchemaUsage(GraphQLSchema schema) {
assertNotNull(schema);
SchemaUsage.Builder builder = new SchemaUsage.Builder();
GraphQLTypeVisitor visitor = new GraphQLTypeVisitorStub() {
private BiFunction<String, Integer, Integer> incCount() {
return (k, v) -> v == null ? 1 : v + 1;
}
private void recordBackReference(GraphQLNamedSchemaElement referencedElement, GraphQLSchemaElement referencingElement) {
String referencedElementName = referencedElement.getName();
if (referencingElement instanceof GraphQLType) {
String typeName = (GraphQLTypeUtil.unwrapAll((GraphQLType) referencingElement)).getName();
builder.elementBackReferences.computeIfAbsent(referencedElementName, k -> new HashSet<>()).add(typeName);
}
if (referencingElement instanceof GraphQLDirective) {
String typeName = ((GraphQLDirective) referencingElement).getName();
builder.elementBackReferences.computeIfAbsent(referencedElementName, k -> new HashSet<>()).add(typeName);
}
}
private void memberInterfaces(GraphQLNamedType containingType, List<GraphQLNamedOutputType> members) {
for (GraphQLNamedOutputType member : members) {
builder.interfaceReferenceCount.compute(member.getName(), incCount());
builder.interfaceImplementors.computeIfAbsent(member.getName(), k -> new HashSet<>()).add(containingType.getName());
recordBackReference(containingType, member);
}
}
@Override
public TraversalControl visitGraphQLArgument(GraphQLArgument node, TraverserContext<GraphQLSchemaElement> context) {
GraphQLNamedType inputType = GraphQLTypeUtil.unwrapAll(node.getType());
builder.argReferenceCount.compute(inputType.getName(), incCount());
GraphQLSchemaElement parentElement = context.getParentNode();
if (parentElement instanceof GraphQLFieldDefinition) {
parentElement = context.getParentContext().getParentNode();
}
recordBackReference(inputType, parentElement);
return CONTINUE;
}
@Override
public TraversalControl visitGraphQLFieldDefinition(GraphQLFieldDefinition node, TraverserContext<GraphQLSchemaElement> context) {
GraphQLNamedType fieldType = GraphQLTypeUtil.unwrapAll(node.getType());
builder.fieldReferenceCounts.compute(fieldType.getName(), incCount());
builder.outputFieldReferenceCounts.compute(fieldType.getName(), incCount());
recordBackReference(fieldType, context.getParentNode());
return CONTINUE;
}
@Override
public TraversalControl visitGraphQLInputObjectField(GraphQLInputObjectField node, TraverserContext<GraphQLSchemaElement> context) {
GraphQLNamedType fieldType = GraphQLTypeUtil.unwrapAll(node.getType());
builder.fieldReferenceCounts.compute(fieldType.getName(), incCount());
builder.inputFieldReferenceCounts.compute(fieldType.getName(), incCount());
recordBackReference(fieldType, context.getParentNode());
return CONTINUE;
}
@Override
public TraversalControl visitGraphQLDirective(GraphQLDirective directive, TraverserContext<GraphQLSchemaElement> context) {
GraphQLSchemaElement parentElement = context.getParentNode();
if (parentElement != null) {
// a null parent is a directive definition
// we record a count if the directive is applied to something - not just defined
builder.directiveReferenceCount.compute(directive.getName(), incCount());
}
if (parentElement instanceof GraphQLArgument) {
context = context.getParentContext();
parentElement = context.getParentNode();
}
if (parentElement instanceof GraphQLFieldDefinition) {
context = context.getParentContext();
parentElement = context.getParentNode();
}
if (parentElement instanceof GraphQLInputObjectField) {
context = context.getParentContext();
parentElement = context.getParentNode();
}
recordBackReference(directive, parentElement);
return CONTINUE;
}
@Override
public TraversalControl visitGraphQLUnionType(GraphQLUnionType unionType, TraverserContext<GraphQLSchemaElement> context) {
List<GraphQLNamedOutputType> members = unionType.getTypes();
for (GraphQLNamedOutputType member : members) {
builder.unionReferenceCount.compute(member.getName(), incCount());
recordBackReference(unionType, member);
}
return CONTINUE;
}
@Override
public TraversalControl visitGraphQLInterfaceType(GraphQLInterfaceType interfaceType, TraverserContext<GraphQLSchemaElement> context) {
memberInterfaces(interfaceType, interfaceType.getInterfaces());
return CONTINUE;
}
@Override
public TraversalControl visitGraphQLObjectType(GraphQLObjectType objectType, TraverserContext<GraphQLSchemaElement> context) {
memberInterfaces(objectType, objectType.getInterfaces());
return CONTINUE;
}
};
new SchemaTraverser().depthFirstFullSchema(visitor, schema);
return builder.build();
}
Aggregations