Search in sources :

Example 11 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 12 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 13 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 14 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)

Example 15 with GraphQLSchemaElement

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();
}
Also used : GraphQLSchemaElement(graphql.schema.GraphQLSchemaElement) GraphQLNamedType(graphql.schema.GraphQLNamedType) TraversalControl(graphql.util.TraversalControl) BiFunction(java.util.function.BiFunction) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) GraphQLInterfaceType(graphql.schema.GraphQLInterfaceType) GraphQLInputObjectField(graphql.schema.GraphQLInputObjectField) GraphQLUnionType(graphql.schema.GraphQLUnionType) GraphQLType(graphql.schema.GraphQLType) TraverserContext(graphql.util.TraverserContext) GraphQLNamedSchemaElement(graphql.schema.GraphQLNamedSchemaElement) HashSet(java.util.HashSet) GraphQLSchema(graphql.schema.GraphQLSchema) GraphQLTypeUtil(graphql.schema.GraphQLTypeUtil) SchemaTraverser(graphql.schema.SchemaTraverser) GraphQLObjectType(graphql.schema.GraphQLObjectType) GraphQLDirective(graphql.schema.GraphQLDirective) GraphQLNamedOutputType(graphql.schema.GraphQLNamedOutputType) CONTINUE(graphql.util.TraversalControl.CONTINUE) GraphQLArgument(graphql.schema.GraphQLArgument) GraphQLTypeVisitorStub(graphql.schema.GraphQLTypeVisitorStub) List(java.util.List) PublicApi(graphql.PublicApi) Assert.assertNotNull(graphql.Assert.assertNotNull) GraphQLTypeVisitor(graphql.schema.GraphQLTypeVisitor) GraphQLUnionType(graphql.schema.GraphQLUnionType) GraphQLType(graphql.schema.GraphQLType) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) GraphQLNamedOutputType(graphql.schema.GraphQLNamedOutputType) GraphQLInterfaceType(graphql.schema.GraphQLInterfaceType) GraphQLTypeVisitorStub(graphql.schema.GraphQLTypeVisitorStub) TraverserContext(graphql.util.TraverserContext) GraphQLSchemaElement(graphql.schema.GraphQLSchemaElement) GraphQLNamedSchemaElement(graphql.schema.GraphQLNamedSchemaElement) List(java.util.List) GraphQLNamedType(graphql.schema.GraphQLNamedType) HashSet(java.util.HashSet) GraphQLArgument(graphql.schema.GraphQLArgument) GraphQLDirective(graphql.schema.GraphQLDirective) GraphQLTypeVisitor(graphql.schema.GraphQLTypeVisitor) GraphQLInputObjectField(graphql.schema.GraphQLInputObjectField) GraphQLObjectType(graphql.schema.GraphQLObjectType) SchemaTraverser(graphql.schema.SchemaTraverser)

Aggregations

GraphQLSchemaElement (graphql.schema.GraphQLSchemaElement)16 GraphQLNamedType (graphql.schema.GraphQLNamedType)10 GraphQLObjectType (graphql.schema.GraphQLObjectType)10 GraphQLSchema (graphql.schema.GraphQLSchema)10 GraphQLType (graphql.schema.GraphQLType)10 ArrayList (java.util.ArrayList)9 GraphQLDirective (graphql.schema.GraphQLDirective)8 GraphQLFieldDefinition (graphql.schema.GraphQLFieldDefinition)8 GraphQLNamedOutputType (graphql.schema.GraphQLNamedOutputType)8 List (java.util.List)8 PublicApi (graphql.PublicApi)7 GraphQLArgument (graphql.schema.GraphQLArgument)7 GraphQLInputObjectField (graphql.schema.GraphQLInputObjectField)7 GraphQLInterfaceType (graphql.schema.GraphQLInterfaceType)7 GraphQLUnionType (graphql.schema.GraphQLUnionType)7 LinkedHashMap (java.util.LinkedHashMap)7 Map (java.util.Map)7 ValuesResolver (graphql.execution.ValuesResolver)6 AstPrinter (graphql.language.AstPrinter)6 Document (graphql.language.Document)6