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();
}
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;
}
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);
}
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);
}
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;
}
Aggregations