graphql.schema.GraphQLSchemaElement Java Examples

The following examples show how to use graphql.schema.GraphQLSchemaElement. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FederationSdlPrinter.java    From federation-jvm with MIT License 6 votes vote down vote up
private void printFieldDefinitions(PrintWriter out, Comparator<? super GraphQLSchemaElement> comparator, List<GraphQLFieldDefinition> fieldDefinitions) {
    if (fieldDefinitions.size() == 0) {
        return;
    }

    out.format(" {\n");
    fieldDefinitions
            .stream()
            .sorted(comparator)
            .forEach(fd -> {
                printComments(out, fd, "  ");
                List<GraphQLDirective> fieldDirectives = fd.getDirectives();
                if (fd.isDeprecated()) {
                    fieldDirectives = addDeprecatedDirectiveIfNeeded(fieldDirectives);
                }

                out.format("  %s%s: %s%s\n",
                        fd.getName(), argsString(GraphQLFieldDefinition.class, fd.getArguments()), typeString(fd.getType()),
                        directivesString(GraphQLFieldDefinition.class, fieldDirectives));
            });
    out.format("}");
}
 
Example #2
Source File: FederationSdlPrinter.java    From federation-jvm with MIT License 6 votes vote down vote up
private TypePrinter<GraphQLInterfaceType> interfacePrinter() {
    return (out, type, visibility) -> {
        if (isIntrospectionType(type)) {
            return;
        }

        GraphqlTypeComparatorEnvironment environment = GraphqlTypeComparatorEnvironment.newEnvironment()
                .parentType(GraphQLInterfaceType.class)
                .elementType(GraphQLFieldDefinition.class)
                .build();
        Comparator<? super GraphQLSchemaElement> comparator = options.comparatorRegistry.getComparator(environment);

        if (shouldPrintAsAst(type.getDefinition())) {
            printAsAst(out, type.getDefinition(), type.getExtensionDefinitions());
        } else {
            printComments(out, type, "");
            out.format("interface %s%s", type.getName(), directivesString(GraphQLInterfaceType.class, type.getDirectives()));
            printFieldDefinitions(out, comparator, visibility.getFieldDefinitions(type));
            out.format("\n\n");
        }
    };
}
 
Example #3
Source File: DefaultTypeInfoGenerator.java    From graphql-spqr with Apache License 2.0 6 votes vote down vote up
@Override
public GraphqlTypeComparatorRegistry generateComparatorRegistry(AnnotatedType type, MessageBundle messageBundle) {
    if (!isOrdered(type)) {
        return DEFAULT_REGISTRY;
    }
    return new GraphqlTypeComparatorRegistry() {
        @Override
        public <T extends GraphQLSchemaElement> Comparator<? super T> getComparator(GraphqlTypeComparatorEnvironment env) {
            if (env.getElementType().equals(GraphQLFieldDefinition.class)) {
                return comparator(getFieldOrder(type, messageBundle), env);
            }
            if (env.getElementType().equals(GraphQLInputObjectField.class)
                    || env.getElementType().equals(GraphQLEnumValueDefinition.class)) {
                return comparator(getInputFieldOrder(type, messageBundle), env);
            }
            return DEFAULT_REGISTRY.getComparator(env);
        }
    };
}
 
Example #4
Source File: FederationSdlPrinter.java    From federation-jvm with MIT License 5 votes vote down vote up
private TypePrinter<GraphQLEnumType> enumPrinter() {
    return (out, type, visibility) -> {
        if (isIntrospectionType(type)) {
            return;
        }

        GraphqlTypeComparatorEnvironment environment = GraphqlTypeComparatorEnvironment.newEnvironment()
                .parentType(GraphQLEnumType.class)
                .elementType(GraphQLEnumValueDefinition.class)
                .build();
        Comparator<? super GraphQLSchemaElement> comparator = options.comparatorRegistry.getComparator(environment);

        if (shouldPrintAsAst(type.getDefinition())) {
            printAsAst(out, type.getDefinition(), type.getExtensionDefinitions());
        } else {
            printComments(out, type, "");
            out.format("enum %s%s", type.getName(), directivesString(GraphQLEnumType.class, type.getDirectives()));
            List<GraphQLEnumValueDefinition> values = type.getValues()
                    .stream()
                    .sorted(comparator)
                    .collect(toList());
            if (values.size() > 0) {
                out.format(" {\n");
                for (GraphQLEnumValueDefinition enumValueDefinition : values) {
                    printComments(out, enumValueDefinition, "  ");
                    List<GraphQLDirective> enumValueDirectives = enumValueDefinition.getDirectives();
                    if (enumValueDefinition.isDeprecated()) {
                        enumValueDirectives = addDeprecatedDirectiveIfNeeded(enumValueDirectives);
                    }
                    out.format("  %s%s\n", enumValueDefinition.getName(), directivesString(GraphQLEnumValueDefinition.class, enumValueDirectives));
                }
                out.format("}");
            }
            out.format("\n\n");
        }
    };
}
 
Example #5
Source File: FederationSdlPrinter.java    From federation-jvm with MIT License 5 votes vote down vote up
private TypePrinter<GraphQLUnionType> unionPrinter() {
    return (out, type, visibility) -> {
        if (isIntrospectionType(type)) {
            return;
        }

        GraphqlTypeComparatorEnvironment environment = GraphqlTypeComparatorEnvironment.newEnvironment()
                .parentType(GraphQLUnionType.class)
                .elementType(GraphQLOutputType.class)
                .build();
        Comparator<? super GraphQLSchemaElement> comparator = options.comparatorRegistry.getComparator(environment);

        if (shouldPrintAsAst(type.getDefinition())) {
            printAsAst(out, type.getDefinition(), type.getExtensionDefinitions());
        } else {
            printComments(out, type, "");
            out.format("union %s%s = ", type.getName(), directivesString(GraphQLUnionType.class, type.getDirectives()));
            List<GraphQLNamedOutputType> types = type.getTypes()
                    .stream()
                    .sorted(comparator)
                    .collect(toList());
            for (int i = 0; i < types.size(); i++) {
                GraphQLNamedOutputType objectType = types.get(i);
                if (i > 0) {
                    out.format(" | ");
                }
                out.format("%s", objectType.getName());
            }
            out.format("\n\n");
        }
    };
}
 
Example #6
Source File: FederationSdlPrinter.java    From federation-jvm with MIT License 5 votes vote down vote up
private TypePrinter<GraphQLInputObjectType> inputObjectPrinter() {
    return (out, type, visibility) -> {
        if (isIntrospectionType(type)) {
            return;
        }
        if (shouldPrintAsAst(type.getDefinition())) {
            printAsAst(out, type.getDefinition(), type.getExtensionDefinitions());
        } else {
            printComments(out, type, "");
            GraphqlTypeComparatorEnvironment environment = GraphqlTypeComparatorEnvironment.newEnvironment()
                    .parentType(GraphQLInputObjectType.class)
                    .elementType(GraphQLInputObjectField.class)
                    .build();
            Comparator<? super GraphQLSchemaElement> comparator = options.comparatorRegistry.getComparator(environment);

            out.format("input %s%s", type.getName(), directivesString(GraphQLInputObjectType.class, type.getDirectives()));
            List<GraphQLInputObjectField> inputObjectFields = visibility.getFieldDefinitions(type);
            if (inputObjectFields.size() > 0) {
                out.format(" {\n");
                inputObjectFields
                        .stream()
                        .sorted(comparator)
                        .forEach(fd -> {
                            printComments(out, fd, "  ");
                            out.format("  %s: %s",
                                    fd.getName(), typeString(fd.getType()));
                            Object defaultValue = fd.getDefaultValue();
                            if (defaultValue != null) {
                                String astValue = printAst(defaultValue, fd.getType());
                                out.format(" = %s", astValue);
                            }
                            out.format(directivesString(GraphQLInputObjectField.class, fd.getDirectives()));
                            out.format("\n");
                        });
                out.format("}");
            }
            out.format("\n\n");
        }
    };
}
 
Example #7
Source File: FederationSdlPrinter.java    From federation-jvm with MIT License 5 votes vote down vote up
private String directiveDefinition(GraphQLDirective directive) {
    StringBuilder sb = new StringBuilder();

    StringWriter sw = new StringWriter();
    printComments(new PrintWriter(sw), directive, "");

    sb.append(sw.toString());

    sb.append("directive @").append(directive.getName());

    GraphqlTypeComparatorEnvironment environment = GraphqlTypeComparatorEnvironment.newEnvironment()
            .parentType(GraphQLDirective.class)
            .elementType(GraphQLArgument.class)
            .build();
    Comparator<? super GraphQLSchemaElement> comparator = options.comparatorRegistry.getComparator(environment);

    List<GraphQLArgument> args = directive.getArguments();
    args = args
            .stream()
            .sorted(comparator)
            .collect(toList());

    sb.append(argsString(GraphQLDirective.class, args));

    sb.append(" on ");

    String locations = directive.validLocations().stream().map(Enum::name).collect(Collectors.joining(" | "));
    sb.append(locations);

    return sb.toString();
}
 
Example #8
Source File: DefaultTypeInfoGenerator.java    From graphql-spqr with Apache License 2.0 5 votes vote down vote up
@Override
public <T extends GraphQLSchemaElement> Comparator<? super T> getComparator(GraphqlTypeComparatorEnvironment env) {
    //Leave the arguments in the declared order
    if (env.getElementType().equals(GraphQLArgument.class)) {
        return GraphqlTypeComparatorRegistry.AS_IS_REGISTRY.getComparator(env);
    }
    //Sort everything else by name
    return GraphqlTypeComparatorRegistry.BY_NAME_REGISTRY.getComparator(env);
}
 
Example #9
Source File: DefaultTypeInfoGenerator.java    From graphql-spqr with Apache License 2.0 5 votes vote down vote up
private <T extends GraphQLSchemaElement> Comparator<? super T> comparator(String[] givenOrder, GraphqlTypeComparatorEnvironment env) {
    if (givenOrder.length > 0) {
        return Comparator.comparingInt((T t) -> Utils.indexOf(givenOrder, name(t), Integer.MAX_VALUE))
                .thenComparing(GraphQLUtils::name);
    }
    return DEFAULT_REGISTRY.getComparator(env);
}
 
Example #10
Source File: FederationSdlPrinter.java    From federation-jvm with MIT License 4 votes vote down vote up
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();

    GraphqlTypeComparatorEnvironment environment = GraphqlTypeComparatorEnvironment.newEnvironment()
            .parentType(parent)
            .elementType(GraphQLArgument.class)
            .build();
    Comparator<? super GraphQLSchemaElement> comparator = options.comparatorRegistry.getComparator(environment);

    arguments = arguments
            .stream()
            .sorted(comparator)
            .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()));
        Object defaultValue = argument.getDefaultValue();
        if (defaultValue != null) {
            sb.append(" = ");
            sb.append(printAst(defaultValue, argument.getType()));
        }

        argument.getDirectives().stream()
                .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();
}
 
Example #11
Source File: FederationSdlPrinter.java    From federation-jvm with MIT License 4 votes vote down vote up
private String directiveString(GraphQLDirective directive) {
    if (!options.getIncludeDirective().test(directive)) {
        // @deprecated is special - we always print it if something is deprecated
        if (!isDeprecatedDirective(directive)) {
            return "";
        }
    }

    StringBuilder sb = new StringBuilder();
    sb.append("@").append(directive.getName());

    GraphqlTypeComparatorEnvironment environment = GraphqlTypeComparatorEnvironment.newEnvironment()
            .parentType(GraphQLDirective.class)
            .elementType(GraphQLArgument.class)
            .build();
    Comparator<? super GraphQLSchemaElement> comparator = options.comparatorRegistry.getComparator(environment);

    List<GraphQLArgument> args = directive.getArguments();
    args = args
            .stream()
            .sorted(comparator)
            .collect(toList());
    if (!args.isEmpty()) {
        sb.append("(");
        for (int i = 0; i < args.size(); i++) {
            GraphQLArgument arg = args.get(i);
            String argValue = null;
            if (arg.getValue() != null) {
                argValue = printAst(arg.getValue(), arg.getType());
            } else if (arg.getDefaultValue() != null) {
                argValue = printAst(arg.getDefaultValue(), 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();
}
 
Example #12
Source File: GraphQLUtils.java    From graphql-spqr with Apache License 2.0 4 votes vote down vote up
public static String name(GraphQLSchemaElement element) {
    return ((GraphQLNamedSchemaElement) element).getName();
}