diff --git a/graphql-java-support/src/main/java/com/apollographql/federation/graphqljava/FederationSdlPrinter.java b/graphql-java-support/src/main/java/com/apollographql/federation/graphqljava/FederationSdlPrinter.java index 739ffe50..1491a93b 100644 --- a/graphql-java-support/src/main/java/com/apollographql/federation/graphqljava/FederationSdlPrinter.java +++ b/graphql-java-support/src/main/java/com/apollographql/federation/graphqljava/FederationSdlPrinter.java @@ -1,8 +1,11 @@ package com.apollographql.federation.graphqljava; import static graphql.Directives.DeprecatedDirective; +import static graphql.com.google.common.base.Predicates.not; +import static graphql.introspection.Introspection.DirectiveLocation.ARGUMENT_DEFINITION; import static graphql.introspection.Introspection.DirectiveLocation.ENUM_VALUE; import static graphql.introspection.Introspection.DirectiveLocation.FIELD_DEFINITION; +import static graphql.introspection.Introspection.DirectiveLocation.INPUT_FIELD_DEFINITION; import static graphql.schema.visibility.DefaultGraphqlFieldVisibility.DEFAULT_FIELD_VISIBILITY; import static graphql.util.EscapeUtil.escapeJsonString; import static java.util.Optional.ofNullable; @@ -11,8 +14,8 @@ import graphql.Assert; import graphql.PublicApi; +import graphql.execution.ValuesResolver; import graphql.language.AstPrinter; -import graphql.language.AstValueHelper; import graphql.language.Description; import graphql.language.Document; import graphql.language.EnumTypeDefinition; @@ -47,6 +50,8 @@ import graphql.schema.GraphQLUnionType; import graphql.schema.GraphqlTypeComparatorEnvironment; import graphql.schema.GraphqlTypeComparatorRegistry; +import graphql.schema.InputValueWithState; +import graphql.schema.idl.DirectiveInfo; import graphql.schema.idl.ScalarInfo; import graphql.schema.idl.SchemaParser; import graphql.schema.idl.TypeDefinitionRegistry; @@ -74,9 +79,16 @@ public class FederationSdlPrinter { private static final GraphQLDirective DeprecatedDirective4Printing = GraphQLDirective.newDirective() .name("deprecated") - .validLocations(FIELD_DEFINITION, ENUM_VALUE) + .validLocations(FIELD_DEFINITION, ENUM_VALUE, ARGUMENT_DEFINITION, INPUT_FIELD_DEFINITION) .build(); + /** + * This predicate excludes all directives which are specified bt the GraphQL Specification. + * Printing these directives is optional. + */ + public static final Predicate ExcludeGraphQLSpecifiedDirectivesPredicate = + not(DirectiveInfo::isGraphqlSpecifiedDirective); + /** Options to use when printing a schema */ public static class Options { @@ -390,7 +402,7 @@ public Options includeSchemaElement(Predicate includeSchem this.includeDirectiveDefinitions, this.useAstDefinitions, this.descriptionsAsHashComments, - this.includeDirective, + includeDirective, this.includeDirectiveDefinition, this.includeTypeDefinition, includeSchemaElement, @@ -398,9 +410,9 @@ public Options includeSchemaElement(Predicate includeSchem } /** - * This flag controls whether schema printer will use the {@link graphql.schema.GraphQLType}'s - * original Ast {@link graphql.language.TypeDefinition}s when printing the type. This allows - * access to any `extend type` declarations that might have been originally made. + * This flag controls whether schema printer will use the {@link GraphQLType}'s original Ast + * {@link TypeDefinition}s when printing the type. This allows access to any `extend type` + * declarations that might have been originally made. * * @param flag whether to print via AST type definitions * @return new instance of options @@ -492,9 +504,8 @@ public FederationSdlPrinter(Options options) { /** * This can print an in memory GraphQL IDL document back to a logical schema definition. If you * want to turn a Introspection query result into a Document (and then into a printed schema) then - * use {@link - * graphql.introspection.IntrospectionResultToSchema#createSchemaDefinition(java.util.Map)} first - * to get the {@link graphql.language.Document} and then print that. + * use {@link graphql.introspection.IntrospectionResultToSchema#createSchemaDefinition(Map)} first + * to get the {@link Document} and then print that. * * @param schemaIDL the parsed schema IDL * @return the logical schema definition @@ -810,8 +821,8 @@ private TypePrinter inputObjectPrinter() { fd -> { printComments(out, fd, " "); out.format(" %s: %s", fd.getName(), typeString(fd.getType())); - Object defaultValue = fd.getDefaultValue(); - if (defaultValue != null) { + if (fd.hasSetDefaultValue()) { + InputValueWithState defaultValue = fd.getInputFieldDefaultValue(); String astValue = printAst(defaultValue, fd.getType()); out.format(" = %s", astValue); } @@ -854,12 +865,13 @@ private void printAsAst( out.println(); } - private static String printAst(Object value, GraphQLInputType type) { - return AstPrinter.printAst(AstValueHelper.astFromValue(value, type)); + private static String printAst(InputValueWithState value, GraphQLInputType type) { + return AstPrinter.printAst(ValuesResolver.valueToLiteral(value, type)); } private TypePrinter schemaPrinter() { return (out, schema, visibility) -> { + List schemaDirectives = schema.getSchemaDirectives(); GraphQLObjectType queryType = schema.getQueryType(); GraphQLObjectType mutationType = schema.getMutationType(); GraphQLObjectType subscriptionType = schema.getSubscriptionType(); @@ -881,7 +893,7 @@ private TypePrinter schemaPrinter() { } if (needsSchemaPrinted) { - out.format("schema {\n"); + out.format("schema %s{\n", directivesString(GraphQLSchemaElement.class, schemaDirectives)); if (queryType != null) { out.format(" query: %s\n", queryType.getName()); } @@ -954,8 +966,8 @@ String argsString(Class parent, List args = directive.getArguments(); - args = args.stream().sorted(comparator).collect(toList()); + args = + args.stream() + .filter(arg -> arg.getArgumentValue().isSet() || arg.getArgumentDefaultValue().isSet()) + .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 (arg.hasSetValue()) { + argValue = printAst(arg.getArgumentValue(), arg.getType()); + } else if (arg.hasSetDefaultValue()) { + argValue = printAst(arg.getArgumentDefaultValue(), arg.getType()); } if (!isNullOrEmpty(argValue)) { sb.append(arg.getName()); @@ -1154,6 +1172,10 @@ public String print(GraphQLType type) { return sw.toString(); } + public String print(GraphQLDirective graphQLDirective) { + return directiveDefinition(graphQLDirective); + } + private void printType( PrintWriter out, List typesAsList, @@ -1204,7 +1226,11 @@ private void printMultiLineHashDescription(PrintWriter out, String prefix, List< private void printMultiLineDescription(PrintWriter out, String prefix, List lines) { out.printf("%s\"\"\"\n", prefix); - lines.forEach(l -> out.printf("%s%s\n", prefix, l)); + lines.forEach( + l -> { + String escapedTripleQuotes = l.replaceAll("\"\"\"", "\\\\\"\"\""); + out.printf("%s%s\n", prefix, escapedTripleQuotes); + }); out.printf("%s\"\"\"\n", prefix); } diff --git a/pom.xml b/pom.xml index a7e87aef..dbaab4df 100644 --- a/pom.xml +++ b/pom.xml @@ -76,11 +76,10 @@ --> false 2.7 - 16.1 - 11.0.0 + 17.3 1.8 17.0.0 - 5.7.1 + 5.7.2 3.1.2 true - 2.12.0 + 2.12.5 + 2.5.4 + + com.graphql-java-kickstart + graphql-spring-boot-starter + ${graphql-java-kickstart.version} + + + com.graphql-java-kickstart + graphiql-spring-boot-starter + ${graphql-java-kickstart.version} + runtime + + + com.graphql-java-kickstart + graphql-spring-boot-starter-test + ${graphql-java-kickstart.version} + test + + + com.graphql-java-kickstart + graphql-java-tools + ${graphql-java-tools.version} + + + org.springframework.boot + spring-boot-starter-actuator + ${spring-boot.version} + + + org.springframework.boot + spring-boot-starter-web + ${spring-boot.version} + + + org.springframework.boot + spring-boot-starter-test + ${spring-boot.version} + test + - org.springframework - spring-aop - 5.2.11.RELEASE + com.graphql-java + graphql-java-extended-scalars + 17.0