Search in sources :

Example 16 with GraphQLSchemaElement

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

the class StronglyConnectedComponentsTopologicallySorted method stronglyConnect.

private void stronglyConnect(GraphQLSchemaElement v) {
    nodeToIndex.put(v, index);
    nodeToLowLink.put(v, index);
    index++;
    stack.push(v);
    nodeToOnStack.put(v, true);
    List<GraphQLSchemaElement> dependencies = reverseDependencies.get(v);
    if (dependencies == null) {
        dependencies = new ArrayList<>();
    }
    if (v instanceof GraphQLNamedType) {
        String name = ((GraphQLNamedType) v).getName();
        if (typeRefReverseDependencies.containsKey(name)) {
            dependencies = new ArrayList<>(dependencies);
            dependencies.addAll(typeRefReverseDependencies.get(name));
        }
    }
    for (GraphQLSchemaElement w : dependencies) {
        if (nodeToIndex.get(w) == null) {
            stronglyConnect(w);
            nodeToLowLink.put(v, Math.min(nodeToLowLink.get(v), nodeToLowLink.get(w)));
        } else if (Boolean.TRUE.equals(nodeToOnStack.get(w))) {
            nodeToLowLink.put(v, Math.min(nodeToLowLink.get(v), nodeToIndex.get(w)));
        }
    }
    if (nodeToLowLink.get(v).equals(nodeToIndex.get(v))) {
        Set<GraphQLSchemaElement> newSCC = new LinkedHashSet<>();
        GraphQLSchemaElement w;
        do {
            w = stack.pop();
            nodeToOnStack.put(w, false);
            newSCC.add(w);
        } while (w != v);
        result.add(topologicallySort(newSCC));
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) GraphQLSchemaElement(graphql.schema.GraphQLSchemaElement) GraphQLNamedType(graphql.schema.GraphQLNamedType)

Example 17 with GraphQLSchemaElement

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;
        }
    };
}
Also used : GraphQLSchemaElement(graphql.schema.GraphQLSchemaElement) GraphQLObjectType(graphql.schema.GraphQLObjectType) GraphQLTypeVisitorStub(graphql.schema.GraphQLTypeVisitorStub) TraverserContext(graphql.util.TraverserContext)

Example 18 with GraphQLSchemaElement

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();
}
Also used : GraphQLSchemaElement(graphql.schema.GraphQLSchemaElement) StringWriter(java.io.StringWriter) GraphQLType(graphql.schema.GraphQLType) GraphQLDirective(graphql.schema.GraphQLDirective) PrintWriter(java.io.PrintWriter)

Example 19 with GraphQLSchemaElement

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();
}
Also used : Arrays(java.util.Arrays) GraphqlTypeComparatorRegistry(graphql.schema.GraphqlTypeComparatorRegistry) GraphQLDirectiveContainer(graphql.schema.GraphQLDirectiveContainer) ValuesResolver(graphql.execution.ValuesResolver) GraphQLInputObjectType(graphql.schema.GraphQLInputObjectType) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) GraphQLInterfaceType(graphql.schema.GraphQLInterfaceType) InputValueDefinition(graphql.language.InputValueDefinition) GraphQLInputObjectField(graphql.schema.GraphQLInputObjectField) GraphQLUnionType(graphql.schema.GraphQLUnionType) GraphQLEnumValueDefinition(graphql.schema.GraphQLEnumValueDefinition) GraphQLAppliedDirective(graphql.schema.GraphQLAppliedDirective) InputValueWithState(graphql.schema.InputValueWithState) EnumTypeDefinition(graphql.language.EnumTypeDefinition) Description(graphql.language.Description) EnumValueDefinition(graphql.language.EnumValueDefinition) Map(java.util.Map) DefaultGraphqlTypeComparatorRegistry(graphql.schema.DefaultGraphqlTypeComparatorRegistry) GraphQLObjectType(graphql.schema.GraphQLObjectType) PrintWriter(java.io.PrintWriter) GraphQLDirective(graphql.schema.GraphQLDirective) ObjectTypeDefinition(graphql.language.ObjectTypeDefinition) GraphQLNamedOutputType(graphql.schema.GraphQLNamedOutputType) Predicate(java.util.function.Predicate) GraphqlFieldVisibility(graphql.schema.visibility.GraphqlFieldVisibility) FieldDefinition(graphql.language.FieldDefinition) GraphQLInputType(graphql.schema.GraphQLInputType) TypeDefinition(graphql.language.TypeDefinition) GraphQLArgument(graphql.schema.GraphQLArgument) Collectors(java.util.stream.Collectors) Collectors.joining(java.util.stream.Collectors.joining) AstPrinter(graphql.language.AstPrinter) List(java.util.List) Stream(java.util.stream.Stream) InterfaceTypeDefinition(graphql.language.InterfaceTypeDefinition) GraphQLEnumType(graphql.schema.GraphQLEnumType) GraphqlTypeComparatorEnvironment(graphql.schema.GraphqlTypeComparatorEnvironment) GraphQLSchemaElement(graphql.schema.GraphQLSchemaElement) GraphQLNamedType(graphql.schema.GraphQLNamedType) GraphQLScalarType(graphql.schema.GraphQLScalarType) InputObjectTypeDefinition(graphql.language.InputObjectTypeDefinition) GraphQLType(graphql.schema.GraphQLType) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) UnionTypeDefinition(graphql.language.UnionTypeDefinition) GraphQLSchema(graphql.schema.GraphQLSchema) GraphQLTypeUtil(graphql.schema.GraphQLTypeUtil) DeprecatedDirective(graphql.Directives.DeprecatedDirective) GraphQLAppliedDirectiveArgument(graphql.schema.GraphQLAppliedDirectiveArgument) Assert(graphql.Assert) DirectivesUtil(graphql.DirectivesUtil) ScalarTypeDefinition(graphql.language.ScalarTypeDefinition) EscapeUtil.escapeJsonString(graphql.util.EscapeUtil.escapeJsonString) DEFAULT_FIELD_VISIBILITY(graphql.schema.visibility.DefaultGraphqlFieldVisibility.DEFAULT_FIELD_VISIBILITY) Optional.ofNullable(java.util.Optional.ofNullable) StringWriter(java.io.StringWriter) GraphQLOutputType(graphql.schema.GraphQLOutputType) Document(graphql.language.Document) Collectors.toList(java.util.stream.Collectors.toList) PublicApi(graphql.PublicApi) Comparator(java.util.Comparator) GraphQLAppliedDirectiveArgument(graphql.schema.GraphQLAppliedDirectiveArgument) EscapeUtil.escapeJsonString(graphql.util.EscapeUtil.escapeJsonString)

Example 20 with GraphQLSchemaElement

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();
}
Also used : InputValueWithState(graphql.schema.InputValueWithState) Arrays(java.util.Arrays) GraphqlTypeComparatorRegistry(graphql.schema.GraphqlTypeComparatorRegistry) GraphQLDirectiveContainer(graphql.schema.GraphQLDirectiveContainer) ValuesResolver(graphql.execution.ValuesResolver) GraphQLInputObjectType(graphql.schema.GraphQLInputObjectType) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) GraphQLInterfaceType(graphql.schema.GraphQLInterfaceType) InputValueDefinition(graphql.language.InputValueDefinition) GraphQLInputObjectField(graphql.schema.GraphQLInputObjectField) GraphQLUnionType(graphql.schema.GraphQLUnionType) GraphQLEnumValueDefinition(graphql.schema.GraphQLEnumValueDefinition) GraphQLAppliedDirective(graphql.schema.GraphQLAppliedDirective) InputValueWithState(graphql.schema.InputValueWithState) EnumTypeDefinition(graphql.language.EnumTypeDefinition) Description(graphql.language.Description) EnumValueDefinition(graphql.language.EnumValueDefinition) Map(java.util.Map) DefaultGraphqlTypeComparatorRegistry(graphql.schema.DefaultGraphqlTypeComparatorRegistry) GraphQLObjectType(graphql.schema.GraphQLObjectType) PrintWriter(java.io.PrintWriter) GraphQLDirective(graphql.schema.GraphQLDirective) ObjectTypeDefinition(graphql.language.ObjectTypeDefinition) GraphQLNamedOutputType(graphql.schema.GraphQLNamedOutputType) Predicate(java.util.function.Predicate) GraphqlFieldVisibility(graphql.schema.visibility.GraphqlFieldVisibility) FieldDefinition(graphql.language.FieldDefinition) GraphQLInputType(graphql.schema.GraphQLInputType) TypeDefinition(graphql.language.TypeDefinition) GraphQLArgument(graphql.schema.GraphQLArgument) Collectors(java.util.stream.Collectors) Collectors.joining(java.util.stream.Collectors.joining) AstPrinter(graphql.language.AstPrinter) List(java.util.List) Stream(java.util.stream.Stream) InterfaceTypeDefinition(graphql.language.InterfaceTypeDefinition) GraphQLEnumType(graphql.schema.GraphQLEnumType) GraphqlTypeComparatorEnvironment(graphql.schema.GraphqlTypeComparatorEnvironment) GraphQLSchemaElement(graphql.schema.GraphQLSchemaElement) GraphQLNamedType(graphql.schema.GraphQLNamedType) GraphQLScalarType(graphql.schema.GraphQLScalarType) InputObjectTypeDefinition(graphql.language.InputObjectTypeDefinition) GraphQLType(graphql.schema.GraphQLType) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) UnionTypeDefinition(graphql.language.UnionTypeDefinition) GraphQLSchema(graphql.schema.GraphQLSchema) GraphQLTypeUtil(graphql.schema.GraphQLTypeUtil) DeprecatedDirective(graphql.Directives.DeprecatedDirective) GraphQLAppliedDirectiveArgument(graphql.schema.GraphQLAppliedDirectiveArgument) Assert(graphql.Assert) DirectivesUtil(graphql.DirectivesUtil) ScalarTypeDefinition(graphql.language.ScalarTypeDefinition) EscapeUtil.escapeJsonString(graphql.util.EscapeUtil.escapeJsonString) DEFAULT_FIELD_VISIBILITY(graphql.schema.visibility.DefaultGraphqlFieldVisibility.DEFAULT_FIELD_VISIBILITY) Optional.ofNullable(java.util.Optional.ofNullable) StringWriter(java.io.StringWriter) GraphQLOutputType(graphql.schema.GraphQLOutputType) Document(graphql.language.Document) Collectors.toList(java.util.stream.Collectors.toList) PublicApi(graphql.PublicApi) Comparator(java.util.Comparator) GraphQLArgument(graphql.schema.GraphQLArgument) EscapeUtil.escapeJsonString(graphql.util.EscapeUtil.escapeJsonString)

Aggregations

GraphQLSchemaElement (graphql.schema.GraphQLSchemaElement)23 GraphQLType (graphql.schema.GraphQLType)17 GraphQLObjectType (graphql.schema.GraphQLObjectType)15 GraphQLFieldDefinition (graphql.schema.GraphQLFieldDefinition)14 GraphQLNamedType (graphql.schema.GraphQLNamedType)14 List (java.util.List)13 GraphQLInputObjectField (graphql.schema.GraphQLInputObjectField)12 ArrayList (java.util.ArrayList)12 GraphQLDirective (graphql.schema.GraphQLDirective)11 GraphQLInputObjectType (graphql.schema.GraphQLInputObjectType)11 GraphQLInputType (graphql.schema.GraphQLInputType)11 GraphQLNamedOutputType (graphql.schema.GraphQLNamedOutputType)11 GraphQLScalarType (graphql.schema.GraphQLScalarType)11 GraphQLArgument (graphql.schema.GraphQLArgument)10 GraphQLSchema (graphql.schema.GraphQLSchema)10 Map (java.util.Map)10 Collectors (java.util.stream.Collectors)10 GraphQLInterfaceType (graphql.schema.GraphQLInterfaceType)9 GraphQLOutputType (graphql.schema.GraphQLOutputType)9 GraphQLUnionType (graphql.schema.GraphQLUnionType)9