Search in sources :

Example 1 with GraphQLSchemaElement

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

the class SchemaPrinter method directivesString.

private String directivesString(Class<? extends GraphQLSchemaElement> parentType, boolean isDeprecated, List<GraphQLAppliedDirective> directives) {
    if (isDeprecated) {
        directives = addDeprecatedDirectiveIfNeeded(directives);
    }
    directives = directives.stream().filter(directive -> options.getIncludeDirective().test(directive.getName()) || isDeprecatedDirective(directive)).filter(options.getIncludeSchemaElement()).collect(toList());
    if (directives.isEmpty()) {
        return "";
    }
    StringBuilder sb = new StringBuilder();
    if (parentType != GraphQLSchemaElement.class) {
        sb.append(" ");
    }
    Comparator<? super GraphQLSchemaElement> comparator = getComparator(parentType, GraphQLAppliedDirective.class);
    directives = directives.stream().sorted(comparator).collect(toList());
    for (int i = 0; i < directives.size(); i++) {
        GraphQLAppliedDirective directive = directives.get(i);
        sb.append(directiveString(directive));
        if (i < directives.size() - 1) {
            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) GraphQLAppliedDirective(graphql.schema.GraphQLAppliedDirective)

Example 2 with GraphQLSchemaElement

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

the class SchemaPrinter method print.

/**
 * This can print an in memory GraphQL schema back to a logical schema definition
 *
 * @param schema the schema in play
 *
 * @return the logical schema definition
 */
public String print(GraphQLSchema schema) {
    StringWriter sw = new StringWriter();
    PrintWriter out = new PrintWriter(sw);
    GraphqlFieldVisibility visibility = schema.getCodeRegistry().getFieldVisibility();
    printer(schema.getClass()).print(out, schema, visibility);
    Comparator<? super GraphQLSchemaElement> comparator = getComparator(GraphQLSchemaElement.class, null);
    Stream<? extends GraphQLSchemaElement> directivesAndTypes = Stream.concat(schema.getAllTypesAsList().stream(), getSchemaDirectives(schema).stream());
    List<GraphQLSchemaElement> elements = directivesAndTypes.map(e -> (GraphQLSchemaElement) e).filter(options.getIncludeSchemaElement()).sorted(comparator).collect(toList());
    for (GraphQLSchemaElement element : elements) {
        printSchemaElement(out, element, visibility);
    }
    String result = sw.toString();
    if (result.endsWith("\n\n")) {
        result = result.substring(0, result.length() - 1);
    }
    return result;
}
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) GraphqlFieldVisibility(graphql.schema.visibility.GraphqlFieldVisibility) GraphQLSchemaElement(graphql.schema.GraphQLSchemaElement) StringWriter(java.io.StringWriter) EscapeUtil.escapeJsonString(graphql.util.EscapeUtil.escapeJsonString) PrintWriter(java.io.PrintWriter)

Example 3 with GraphQLSchemaElement

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

the class SchemaUtil method replaceTypeReferences.

public static void replaceTypeReferences(GraphQLSchema schema) {
    final Map<String, GraphQLNamedType> typeMap = schema.getTypeMap();
    List<GraphQLSchemaElement> roots = new ArrayList<>(typeMap.values());
    roots.addAll(schema.getDirectives());
    SchemaTraverser schemaTraverser = new SchemaTraverser(schemaElement -> schemaElement.getChildrenWithTypeReferences().getChildrenAsList());
    schemaTraverser.depthFirst(new GraphQLTypeResolvingVisitor(typeMap), roots);
}
Also used : GraphQLTypeResolvingVisitor(graphql.schema.GraphQLTypeResolvingVisitor) GraphQLSchemaElement(graphql.schema.GraphQLSchemaElement) ArrayList(java.util.ArrayList) GraphQLNamedType(graphql.schema.GraphQLNamedType) SchemaTraverser(graphql.schema.SchemaTraverser)

Example 4 with GraphQLSchemaElement

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

the class SchemaUtil method visitPartiallySchema.

/**
 * Called to visit a partially build schema (during {@link GraphQLSchema} build phases) with a set of visitors
 *
 * Each visitor is expected to hold its own side effects that might be last used to construct a full schema
 *
 * @param partiallyBuiltSchema the partially built schema
 * @param visitors             the visitors to call
 */
public static void visitPartiallySchema(final GraphQLSchema partiallyBuiltSchema, GraphQLTypeVisitor... visitors) {
    List<GraphQLSchemaElement> roots = new ArrayList<>();
    roots.add(partiallyBuiltSchema.getQueryType());
    if (partiallyBuiltSchema.isSupportingMutations()) {
        roots.add(partiallyBuiltSchema.getMutationType());
    }
    if (partiallyBuiltSchema.isSupportingSubscriptions()) {
        roots.add(partiallyBuiltSchema.getSubscriptionType());
    }
    if (partiallyBuiltSchema.getAdditionalTypes() != null) {
        roots.addAll(partiallyBuiltSchema.getAdditionalTypes());
    }
    if (partiallyBuiltSchema.getDirectives() != null) {
        roots.addAll(partiallyBuiltSchema.getDirectives());
    }
    roots.add(partiallyBuiltSchema.getIntrospectionSchemaType());
    GraphQLTypeVisitor visitor = new MultiReadOnlyGraphQLTypeVisitor(Arrays.asList(visitors));
    SchemaTraverser traverser;
    traverser = new SchemaTraverser(schemaElement -> schemaElement.getChildrenWithTypeReferences().getChildrenAsList());
    traverser.depthFirst(visitor, roots);
}
Also used : GraphQLObjectType(graphql.schema.GraphQLObjectType) GraphQLSchemaElement(graphql.schema.GraphQLSchemaElement) GraphQLNamedType(graphql.schema.GraphQLNamedType) Arrays(java.util.Arrays) Internal(graphql.Internal) GraphQLNamedOutputType(graphql.schema.GraphQLNamedOutputType) ImmutableMap(com.google.common.collect.ImmutableMap) GraphQLTypeResolvingVisitor(graphql.schema.GraphQLTypeResolvingVisitor) GraphQLType(graphql.schema.GraphQLType) GraphQLImplementingType(graphql.schema.GraphQLImplementingType) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) List(java.util.List) TreeMap(java.util.TreeMap) GraphQLTypeVisitor(graphql.schema.GraphQLTypeVisitor) Map(java.util.Map) GraphQLSchema(graphql.schema.GraphQLSchema) SchemaTraverser(graphql.schema.SchemaTraverser) GraphQLSchemaElement(graphql.schema.GraphQLSchemaElement) GraphQLTypeVisitor(graphql.schema.GraphQLTypeVisitor) ArrayList(java.util.ArrayList) SchemaTraverser(graphql.schema.SchemaTraverser)

Example 5 with GraphQLSchemaElement

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

the class Anonymizer method anonymizeSchemaAndQueries.

public static AnonymizeResult anonymizeSchemaAndQueries(GraphQLSchema schema, List<String> queries, Map<String, Object> variables) {
    assertNotNull(queries, () -> "queries can't be null");
    AtomicInteger defaultStringValueCounter = new AtomicInteger(1);
    AtomicInteger defaultIntValueCounter = new AtomicInteger(1);
    Map<GraphQLNamedSchemaElement, String> newNameMap = recordNewNamesForSchema(schema);
    // stores a reverse index of anonymized argument name to argument instance
    // this is to handle cases where the fields on implementing types MUST have the same exact argument and default
    // value definitions as the fields on the implemented interface. (argument default values must match exactly)
    Map<String, GraphQLArgument> renamedArgumentsMap = new HashMap<>();
    SchemaTransformer schemaTransformer = new SchemaTransformer();
    GraphQLSchema newSchema = schemaTransformer.transform(schema, new GraphQLTypeVisitorStub() {

        @Override
        public TraversalControl visitGraphQLTypeReference(GraphQLTypeReference graphQLTypeReference, TraverserContext<GraphQLSchemaElement> context) {
            GraphQLNamedSchemaElement type = (GraphQLNamedSchemaElement) schema.getType(graphQLTypeReference.getName());
            String newName = newNameMap.get(type);
            GraphQLTypeReference newReference = GraphQLTypeReference.typeRef(newName);
            return changeNode(context, newReference);
        }

        @Override
        public TraversalControl visitGraphQLArgument(GraphQLArgument graphQLArgument, TraverserContext<GraphQLSchemaElement> context) {
            String newName = assertNotNull(newNameMap.get(graphQLArgument));
            if (context.getParentNode() instanceof GraphQLFieldDefinition) {
                // arguments on field definitions must be identical across implementing types and interfaces.
                if (renamedArgumentsMap.containsKey(newName)) {
                    return changeNode(context, renamedArgumentsMap.get(newName).transform(b -> {
                    }));
                }
            }
            GraphQLArgument newElement = graphQLArgument.transform(builder -> {
                builder.name(newName).description(null).definition(null);
                if (graphQLArgument.hasSetDefaultValue()) {
                    Value<?> defaultValueLiteral = ValuesResolver.valueToLiteral(graphQLArgument.getArgumentDefaultValue(), graphQLArgument.getType());
                    builder.defaultValueLiteral(replaceValue(defaultValueLiteral, graphQLArgument.getType(), newNameMap, defaultStringValueCounter, defaultIntValueCounter));
                }
                if (graphQLArgument.hasSetValue()) {
                    Value<?> valueLiteral = ValuesResolver.valueToLiteral(graphQLArgument.getArgumentValue(), graphQLArgument.getType());
                    builder.valueLiteral(replaceValue(valueLiteral, graphQLArgument.getType(), newNameMap, defaultStringValueCounter, defaultIntValueCounter));
                }
            });
            renamedArgumentsMap.put(newName, newElement);
            return changeNode(context, newElement);
        }

        @Override
        public TraversalControl visitGraphQLAppliedDirectiveArgument(GraphQLAppliedDirectiveArgument graphQLArgument, TraverserContext<GraphQLSchemaElement> context) {
            String newName = assertNotNull(newNameMap.get(graphQLArgument));
            GraphQLAppliedDirectiveArgument newElement = graphQLArgument.transform(builder -> {
                builder.name(newName).description(null).definition(null);
                if (graphQLArgument.hasSetValue()) {
                    Value<?> valueLiteral = ValuesResolver.valueToLiteral(graphQLArgument.getArgumentValue(), graphQLArgument.getType());
                    builder.valueLiteral(replaceValue(valueLiteral, graphQLArgument.getType(), newNameMap, defaultStringValueCounter, defaultIntValueCounter));
                }
            });
            return changeNode(context, newElement);
        }

        @Override
        public TraversalControl visitGraphQLInterfaceType(GraphQLInterfaceType graphQLInterfaceType, TraverserContext<GraphQLSchemaElement> context) {
            if (Introspection.isIntrospectionTypes(graphQLInterfaceType)) {
                return TraversalControl.ABORT;
            }
            String newName = assertNotNull(newNameMap.get(graphQLInterfaceType));
            GraphQLInterfaceType newElement = graphQLInterfaceType.transform(builder -> {
                builder.name(newName).description(null).definition(null);
            });
            GraphQLCodeRegistry.Builder codeRegistry = assertNotNull(context.getVarFromParents(GraphQLCodeRegistry.Builder.class));
            TypeResolver typeResolver = codeRegistry.getTypeResolver(graphQLInterfaceType);
            codeRegistry.typeResolver(newName, typeResolver);
            return changeNode(context, newElement);
        }

        @Override
        public TraversalControl visitGraphQLEnumType(GraphQLEnumType graphQLEnumType, TraverserContext<GraphQLSchemaElement> context) {
            if (Introspection.isIntrospectionTypes(graphQLEnumType)) {
                return TraversalControl.ABORT;
            }
            String newName = assertNotNull(newNameMap.get(graphQLEnumType));
            GraphQLEnumType newElement = graphQLEnumType.transform(builder -> {
                builder.name(newName).description(null).definition(null);
            });
            return changeNode(context, newElement);
        }

        @Override
        public TraversalControl visitGraphQLEnumValueDefinition(GraphQLEnumValueDefinition enumValueDefinition, TraverserContext<GraphQLSchemaElement> context) {
            String newName = assertNotNull(newNameMap.get(enumValueDefinition));
            GraphQLEnumValueDefinition newElement = enumValueDefinition.transform(builder -> {
                builder.name(newName).description(null).definition(null);
            });
            return changeNode(context, newElement);
        }

        @Override
        public TraversalControl visitGraphQLFieldDefinition(GraphQLFieldDefinition graphQLFieldDefinition, TraverserContext<GraphQLSchemaElement> context) {
            String newName = assertNotNull(newNameMap.get(graphQLFieldDefinition));
            GraphQLFieldDefinition newElement = graphQLFieldDefinition.transform(builder -> {
                builder.name(newName).description(null).definition(null);
            });
            return changeNode(context, newElement);
        }

        @Override
        public TraversalControl visitGraphQLDirective(GraphQLDirective graphQLDirective, TraverserContext<GraphQLSchemaElement> context) {
            if (Directives.DEPRECATED_DIRECTIVE_DEFINITION.getName().equals(graphQLDirective.getName())) {
                GraphQLArgument reason = newArgument().name("reason").type(Scalars.GraphQLString).clearValue().build();
                GraphQLDirective newElement = graphQLDirective.transform(builder -> {
                    builder.description(null).argument(reason);
                });
                changeNode(context, newElement);
                return TraversalControl.ABORT;
            }
            if (DirectiveInfo.isGraphqlSpecifiedDirective(graphQLDirective.getName())) {
                return TraversalControl.ABORT;
            }
            String newName = assertNotNull(newNameMap.get(graphQLDirective));
            GraphQLDirective newElement = graphQLDirective.transform(builder -> {
                builder.name(newName).description(null).definition(null);
            });
            return changeNode(context, newElement);
        }

        @Override
        public TraversalControl visitGraphQLAppliedDirective(GraphQLAppliedDirective graphQLDirective, TraverserContext<GraphQLSchemaElement> context) {
            if (Directives.DEPRECATED_DIRECTIVE_DEFINITION.getName().equals(graphQLDirective.getName())) {
                GraphQLAppliedDirectiveArgument reason = GraphQLAppliedDirectiveArgument.newArgument().name("reason").type(Scalars.GraphQLString).clearValue().build();
                GraphQLAppliedDirective newElement = graphQLDirective.transform(builder -> {
                    builder.description(null).argument(reason);
                });
                changeNode(context, newElement);
                return TraversalControl.ABORT;
            }
            if (DirectiveInfo.isGraphqlSpecifiedDirective(graphQLDirective.getName())) {
                return TraversalControl.ABORT;
            }
            String newName = assertNotNull(newNameMap.get(graphQLDirective));
            GraphQLAppliedDirective newElement = graphQLDirective.transform(builder -> {
                builder.name(newName).description(null).definition(null);
            });
            return changeNode(context, newElement);
        }

        @Override
        public TraversalControl visitGraphQLInputObjectField(GraphQLInputObjectField graphQLInputObjectField, TraverserContext<GraphQLSchemaElement> context) {
            String newName = assertNotNull(newNameMap.get(graphQLInputObjectField));
            Value<?> defaultValue = null;
            if (graphQLInputObjectField.hasSetDefaultValue()) {
                defaultValue = ValuesResolver.valueToLiteral(graphQLInputObjectField.getInputFieldDefaultValue(), graphQLInputObjectField.getType());
                defaultValue = replaceValue(defaultValue, graphQLInputObjectField.getType(), newNameMap, defaultStringValueCounter, defaultIntValueCounter);
            }
            Value<?> finalDefaultValue = defaultValue;
            GraphQLInputObjectField newElement = graphQLInputObjectField.transform(builder -> {
                builder.name(newName);
                if (finalDefaultValue != null) {
                    builder.defaultValueLiteral(finalDefaultValue);
                }
                builder.description(null);
                builder.definition(null);
            });
            return changeNode(context, newElement);
        }

        @Override
        public TraversalControl visitGraphQLInputObjectType(GraphQLInputObjectType graphQLInputObjectType, TraverserContext<GraphQLSchemaElement> context) {
            if (Introspection.isIntrospectionTypes(graphQLInputObjectType)) {
                return TraversalControl.ABORT;
            }
            String newName = assertNotNull(newNameMap.get(graphQLInputObjectType));
            GraphQLInputObjectType newElement = graphQLInputObjectType.transform(builder -> {
                builder.name(newName).description(null).definition(null);
            });
            return changeNode(context, newElement);
        }

        @Override
        public TraversalControl visitGraphQLObjectType(GraphQLObjectType graphQLObjectType, TraverserContext<GraphQLSchemaElement> context) {
            if (Introspection.isIntrospectionTypes(graphQLObjectType)) {
                return TraversalControl.ABORT;
            }
            String newName = assertNotNull(newNameMap.get(graphQLObjectType));
            GraphQLObjectType newElement = graphQLObjectType.transform(builder -> {
                builder.name(newName).description(null).definition(null);
            });
            return changeNode(context, newElement);
        }

        @Override
        public TraversalControl visitGraphQLScalarType(GraphQLScalarType graphQLScalarType, TraverserContext<GraphQLSchemaElement> context) {
            if (ScalarInfo.isGraphqlSpecifiedScalar(graphQLScalarType)) {
                return TraversalControl.ABORT;
            }
            String newName = assertNotNull(newNameMap.get(graphQLScalarType));
            GraphQLScalarType newElement = graphQLScalarType.transform(builder -> {
                builder.name(newName).description(null).definition(null);
            });
            return changeNode(context, newElement);
        }

        @Override
        public TraversalControl visitGraphQLUnionType(GraphQLUnionType graphQLUnionType, TraverserContext<GraphQLSchemaElement> context) {
            if (Introspection.isIntrospectionTypes(graphQLUnionType)) {
                return TraversalControl.ABORT;
            }
            String newName = assertNotNull(newNameMap.get(graphQLUnionType));
            GraphQLUnionType newElement = graphQLUnionType.transform(builder -> {
                builder.name(newName).description(null).definition(null);
            });
            GraphQLCodeRegistry.Builder codeRegistry = assertNotNull(context.getVarFromParents(GraphQLCodeRegistry.Builder.class));
            TypeResolver typeResolver = codeRegistry.getTypeResolver(graphQLUnionType);
            codeRegistry.typeResolver(newName, typeResolver);
            return changeNode(context, newElement);
        }
    });
    List<String> newQueries = new ArrayList<>();
    for (String query : queries) {
        String newQuery = rewriteQuery(query, schema, newNameMap, variables);
        newQueries.add(newQuery);
    }
    AnonymizeResult result = new AnonymizeResult(newSchema, newQueries);
    return result;
}
Also used : OperationDefinition(graphql.language.OperationDefinition) Value(graphql.language.Value) QueryVisitorInlineFragmentEnvironment(graphql.analysis.QueryVisitorInlineFragmentEnvironment) ValuesResolver(graphql.execution.ValuesResolver) GraphQLInputObjectType(graphql.schema.GraphQLInputObjectType) QueryTraverser(graphql.analysis.QueryTraverser) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) FragmentSpread(graphql.language.FragmentSpread) GraphQLInterfaceType(graphql.schema.GraphQLInterfaceType) GraphQLInputObjectField(graphql.schema.GraphQLInputObjectField) GraphQLUnionType(graphql.schema.GraphQLUnionType) GraphQLEnumValueDefinition(graphql.schema.GraphQLEnumValueDefinition) GraphQLNamedSchemaElement(graphql.schema.GraphQLNamedSchemaElement) GraphQLTypeUtil.unwrapNonNullAs(graphql.schema.GraphQLTypeUtil.unwrapNonNullAs) Directives(graphql.Directives) GraphQLAppliedDirective(graphql.schema.GraphQLAppliedDirective) QueryVisitorFieldEnvironment(graphql.analysis.QueryVisitorFieldEnvironment) Type(graphql.language.Type) DirectiveInfo(graphql.schema.idl.DirectiveInfo) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) QueryVisitorFragmentSpreadEnvironment(graphql.analysis.QueryVisitorFragmentSpreadEnvironment) BigInteger(java.math.BigInteger) TypeName(graphql.language.TypeName) TypeResolver(graphql.schema.TypeResolver) GraphQLObjectType(graphql.schema.GraphQLObjectType) GraphQLDirective(graphql.schema.GraphQLDirective) GraphQLNamedOutputType(graphql.schema.GraphQLNamedOutputType) GraphQLNonNull(graphql.schema.GraphQLNonNull) ObjectField(graphql.language.ObjectField) GraphQLInputType(graphql.schema.GraphQLInputType) Set(java.util.Set) GraphQLArgument(graphql.schema.GraphQLArgument) String.format(java.lang.String.format) AstPrinter(graphql.language.AstPrinter) List(java.util.List) QueryVisitor(graphql.analysis.QueryVisitor) ArrayValue(graphql.language.ArrayValue) Optional(java.util.Optional) FragmentDefinition(graphql.language.FragmentDefinition) NonNullType(graphql.language.NonNullType) GraphQLEnumType(graphql.schema.GraphQLEnumType) ListType(graphql.language.ListType) AstTransformer(graphql.language.AstTransformer) ObjectValue(graphql.language.ObjectValue) GraphQLSchemaElement(graphql.schema.GraphQLSchemaElement) GraphQLCodeRegistry(graphql.schema.GraphQLCodeRegistry) GraphQLNamedType(graphql.schema.GraphQLNamedType) Node(graphql.language.Node) SchemaTransformer(graphql.schema.SchemaTransformer) TreeTransformerUtil.changeNode(graphql.util.TreeTransformerUtil.changeNode) GraphQLScalarType(graphql.schema.GraphQLScalarType) QueryVisitorFieldArgumentEnvironment(graphql.analysis.QueryVisitorFieldArgumentEnvironment) QueryVisitorFieldArgumentInputValue(graphql.analysis.QueryVisitorFieldArgumentInputValue) EnumValue(graphql.language.EnumValue) HashMap(java.util.HashMap) GraphQLType(graphql.schema.GraphQLType) ArrayList(java.util.ArrayList) Introspection(graphql.introspection.Introspection) LinkedHashMap(java.util.LinkedHashMap) Scalars(graphql.Scalars) Parser(graphql.parser.Parser) Definition(graphql.language.Definition) VariableReference(graphql.language.VariableReference) BiConsumer(java.util.function.BiConsumer) GraphQLSchema(graphql.schema.GraphQLSchema) GraphQLAppliedDirectiveArgument(graphql.schema.GraphQLAppliedDirectiveArgument) QueryVisitorFieldArgumentValueEnvironment(graphql.analysis.QueryVisitorFieldArgumentValueEnvironment) LinkedHashSet(java.util.LinkedHashSet) NodeVisitorStub(graphql.language.NodeVisitorStub) SchemaGenerator.createdMockedSchema(graphql.schema.idl.SchemaGenerator.createdMockedSchema) ScalarInfo(graphql.schema.idl.ScalarInfo) CONTINUE(graphql.util.TraversalControl.CONTINUE) GraphQLArgument.newArgument(graphql.schema.GraphQLArgument.newArgument) Field(graphql.language.Field) GraphQLImplementingType(graphql.schema.GraphQLImplementingType) GraphQLTypeVisitorStub(graphql.schema.GraphQLTypeVisitorStub) SchemaUtil(graphql.schema.impl.SchemaUtil) AssertException(graphql.AssertException) Directive(graphql.language.Directive) Consumer(java.util.function.Consumer) Argument(graphql.language.Argument) Document(graphql.language.Document) TypeUtil(graphql.schema.idl.TypeUtil) VariableDefinition(graphql.language.VariableDefinition) GraphQLList(graphql.schema.GraphQLList) StringValue(graphql.language.StringValue) GraphQLTypeReference(graphql.schema.GraphQLTypeReference) GraphQLTypeUtil.unwrapNonNull(graphql.schema.GraphQLTypeUtil.unwrapNonNull) PublicApi(graphql.PublicApi) IntValue(graphql.language.IntValue) Assert.assertNotNull(graphql.Assert.assertNotNull) GraphQLTypeVisitor(graphql.schema.GraphQLTypeVisitor) GraphQLTypeUtil.unwrapOneAs(graphql.schema.GraphQLTypeUtil.unwrapOneAs) InlineFragment(graphql.language.InlineFragment) Collections(java.util.Collections) GraphQLTypeReference(graphql.schema.GraphQLTypeReference) GraphQLUnionType(graphql.schema.GraphQLUnionType) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TypeResolver(graphql.schema.TypeResolver) ArrayList(java.util.ArrayList) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) GraphQLInterfaceType(graphql.schema.GraphQLInterfaceType) GraphQLTypeVisitorStub(graphql.schema.GraphQLTypeVisitorStub) GraphQLSchemaElement(graphql.schema.GraphQLSchemaElement) GraphQLAppliedDirectiveArgument(graphql.schema.GraphQLAppliedDirectiveArgument) GraphQLCodeRegistry(graphql.schema.GraphQLCodeRegistry) GraphQLNamedSchemaElement(graphql.schema.GraphQLNamedSchemaElement) GraphQLEnumType(graphql.schema.GraphQLEnumType) GraphQLEnumValueDefinition(graphql.schema.GraphQLEnumValueDefinition) GraphQLArgument(graphql.schema.GraphQLArgument) SchemaTransformer(graphql.schema.SchemaTransformer) GraphQLDirective(graphql.schema.GraphQLDirective) GraphQLAppliedDirective(graphql.schema.GraphQLAppliedDirective) GraphQLSchema(graphql.schema.GraphQLSchema) GraphQLScalarType(graphql.schema.GraphQLScalarType) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GraphQLInputObjectField(graphql.schema.GraphQLInputObjectField) GraphQLInputObjectType(graphql.schema.GraphQLInputObjectType) Value(graphql.language.Value) ArrayValue(graphql.language.ArrayValue) ObjectValue(graphql.language.ObjectValue) QueryVisitorFieldArgumentInputValue(graphql.analysis.QueryVisitorFieldArgumentInputValue) EnumValue(graphql.language.EnumValue) StringValue(graphql.language.StringValue) IntValue(graphql.language.IntValue) GraphQLObjectType(graphql.schema.GraphQLObjectType)

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