use of graphql.schema.GraphQLNamedOutputType in project graphql-java by graphql-java.
the class SchemaGeneratorHelper method buildInterfaceTypeInterfaces.
private void buildInterfaceTypeInterfaces(BuildContext buildCtx, InterfaceTypeDefinition typeDefinition, GraphQLInterfaceType.Builder builder, List<InterfaceTypeExtensionDefinition> extensions) {
Map<String, GraphQLOutputType> interfaces = new LinkedHashMap<>();
typeDefinition.getImplements().forEach(type -> {
GraphQLNamedOutputType newInterfaceType = buildOutputType(buildCtx, type);
interfaces.put(newInterfaceType.getName(), newInterfaceType);
});
extensions.forEach(extension -> extension.getImplements().forEach(type -> {
GraphQLInterfaceType interfaceType = buildOutputType(buildCtx, type);
if (!interfaces.containsKey(interfaceType.getName())) {
interfaces.put(interfaceType.getName(), interfaceType);
}
}));
interfaces.values().forEach(interfaze -> {
if (interfaze instanceof GraphQLInterfaceType) {
builder.withInterface((GraphQLInterfaceType) interfaze);
return;
}
if (interfaze instanceof GraphQLTypeReference) {
builder.withInterface((GraphQLTypeReference) interfaze);
}
});
}
use of graphql.schema.GraphQLNamedOutputType in project graphql-java by graphql-java.
the class SchemaGeneratorHelper method buildObjectTypeInterfaces.
private void buildObjectTypeInterfaces(BuildContext buildCtx, ObjectTypeDefinition typeDefinition, GraphQLObjectType.Builder builder, List<ObjectTypeExtensionDefinition> extensions) {
Map<String, GraphQLOutputType> interfaces = new LinkedHashMap<>();
typeDefinition.getImplements().forEach(type -> {
GraphQLNamedOutputType newInterfaceType = buildOutputType(buildCtx, type);
interfaces.put(newInterfaceType.getName(), newInterfaceType);
});
extensions.forEach(extension -> extension.getImplements().forEach(type -> {
GraphQLInterfaceType interfaceType = buildOutputType(buildCtx, type);
if (!interfaces.containsKey(interfaceType.getName())) {
interfaces.put(interfaceType.getName(), interfaceType);
}
}));
interfaces.values().forEach(interfaze -> {
if (interfaze instanceof GraphQLInterfaceType) {
builder.withInterface((GraphQLInterfaceType) interfaze);
return;
}
if (interfaze instanceof GraphQLTypeReference) {
builder.withInterface((GraphQLTypeReference) interfaze);
}
});
}
use of graphql.schema.GraphQLNamedOutputType in project graphql-java by graphql-java.
the class SchemaGeneratorHelper method buildUnionType.
GraphQLUnionType buildUnionType(BuildContext buildCtx, UnionTypeDefinition typeDefinition) {
GraphQLUnionType.Builder builder = GraphQLUnionType.newUnionType();
builder.definition(buildCtx.isCaptureAstDefinitions() ? typeDefinition : null);
builder.name(typeDefinition.getName());
builder.description(buildDescription(buildCtx, typeDefinition, typeDefinition.getDescription()));
builder.comparatorRegistry(buildCtx.getComparatorRegistry());
List<UnionTypeExtensionDefinition> extensions = unionTypeExtensions(typeDefinition, buildCtx);
builder.extensionDefinitions(buildCtx.isCaptureAstDefinitions() ? extensions : emptyList());
typeDefinition.getMemberTypes().forEach(mt -> {
GraphQLOutputType outputType = buildOutputType(buildCtx, mt);
if (outputType instanceof GraphQLTypeReference) {
builder.possibleType((GraphQLTypeReference) outputType);
} else {
builder.possibleType((GraphQLObjectType) outputType);
}
});
Pair<List<GraphQLDirective>, List<GraphQLAppliedDirective>> appliedDirectives = buildAppliedDirectives(buildCtx, inputTypeFactory(buildCtx), typeDefinition.getDirectives(), directivesOf(extensions), UNION, buildCtx.getDirectives(), buildCtx.getComparatorRegistry());
buildAppliedDirectives(buildCtx, builder, appliedDirectives);
extensions.forEach(extension -> extension.getMemberTypes().forEach(mt -> {
GraphQLNamedOutputType outputType = buildOutputType(buildCtx, mt);
if (!builder.containType(outputType.getName())) {
if (outputType instanceof GraphQLTypeReference) {
builder.possibleType((GraphQLTypeReference) outputType);
} else {
builder.possibleType((GraphQLObjectType) outputType);
}
}
}));
GraphQLUnionType unionType = builder.build();
if (!buildCtx.getCodeRegistry().hasTypeResolver(unionType.getName())) {
TypeResolver typeResolver = getTypeResolverForUnion(buildCtx, typeDefinition);
buildCtx.getCodeRegistry().typeResolver(unionType, typeResolver);
}
return directivesObserve(buildCtx, unionType);
}
use of graphql.schema.GraphQLNamedOutputType 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();
}
use of graphql.schema.GraphQLNamedOutputType in project graphql-java by graphql-java.
the class TypeAndFieldRule method validateUnion.
private void validateUnion(GraphQLUnionType type, SchemaValidationErrorCollector errorCollector) {
List<GraphQLNamedOutputType> memberTypes = type.getTypes();
if (memberTypes.size() == 0) {
SchemaValidationError validationError = new SchemaValidationError(SchemaValidationErrorType.UnionTypeLackOfTypeError, String.format("Union type \"%s\" must include one or more unique member types.", type.getName()));
errorCollector.addError(validationError);
}
Set<String> typeNames = new HashSet<>();
for (GraphQLNamedOutputType memberType : type.getTypes()) {
String typeName = memberType.getName();
// GraphQLNamedType graphQLNamedType = schemaTypeHolder.get(typeName);
if (!(memberType instanceof GraphQLObjectType)) {
SchemaValidationError validationError = new SchemaValidationError(SchemaValidationErrorType.InvalidUnionMemberTypeError, String.format("The member types of a Union type must all be Object base types. member type \"%s\" in Union \"%s\" is invalid.", memberType.getName(), type.getName()));
errorCollector.addError(validationError);
}
if (typeNames.contains(typeName)) {
SchemaValidationError validationError = new SchemaValidationError(SchemaValidationErrorType.RepetitiveElementError, String.format("The member types of a Union type must be unique. member type \"%s\" in Union \"%s\" is not unique.", memberType.getName(), type.getName()));
errorCollector.addError(validationError);
}
typeNames.add(typeName);
}
}
Aggregations