use of graphql.language.UnionTypeDefinition in project graphql-java by graphql-java.
the class GraphqlAntlrToLanguage method visitUnionTypeDefinition.
@Override
public Void visitUnionTypeDefinition(GraphqlParser.UnionTypeDefinitionContext ctx) {
UnionTypeDefinition def = new UnionTypeDefinition(ctx.name().getText());
newNode(def, ctx);
def.setDescription(newDescription(ctx.description()));
result.getDefinitions().add(def);
addContextProperty(ContextProperty.UnionTypeDefinition, def);
super.visitChildren(ctx);
popContext();
return null;
}
use of graphql.language.UnionTypeDefinition in project graphql-java by graphql-java.
the class TypeDefinitionRegistry method isPossibleType.
/**
* Returns true of the abstract type is in implemented by the object type
*
* @param abstractType the abstract type to check (interface or union)
* @param possibleObjectType the object type to check
*
* @return true if the object type implements the abstract type
*/
@SuppressWarnings("ConstantConditions")
public boolean isPossibleType(Type abstractType, Type possibleObjectType) {
if (!isInterfaceOrUnion(abstractType)) {
return false;
}
if (!isObjectType(possibleObjectType)) {
return false;
}
ObjectTypeDefinition targetObjectTypeDef = getType(possibleObjectType, ObjectTypeDefinition.class).get();
TypeDefinition abstractTypeDef = getType(abstractType).get();
if (abstractTypeDef instanceof UnionTypeDefinition) {
List<Type> memberTypes = ((UnionTypeDefinition) abstractTypeDef).getMemberTypes();
for (Type memberType : memberTypes) {
Optional<ObjectTypeDefinition> checkType = getType(memberType, ObjectTypeDefinition.class);
if (checkType.isPresent()) {
if (checkType.get().getName().equals(targetObjectTypeDef.getName())) {
return true;
}
}
}
return false;
} else {
InterfaceTypeDefinition iFace = (InterfaceTypeDefinition) abstractTypeDef;
List<ObjectTypeDefinition> objectTypeDefinitions = getImplementationsOf(iFace);
return objectTypeDefinitions.stream().anyMatch(od -> od.getName().equals(targetObjectTypeDef.getName()));
}
}
use of graphql.language.UnionTypeDefinition in project graphql-java by graphql-java.
the class SchemaGenerator method buildOutputType.
/**
* This is the main recursive spot that builds out the various forms of Output types
*
* @param buildCtx the context we need to work out what we are doing
* @param rawType the type to be built
*
* @return an output type
*/
@SuppressWarnings("unchecked")
private <T extends GraphQLOutputType> T buildOutputType(BuildContext buildCtx, Type rawType) {
TypeDefinition typeDefinition = buildCtx.getTypeDefinition(rawType);
TypeInfo typeInfo = TypeInfo.typeInfo(rawType);
GraphQLOutputType outputType = buildCtx.hasOutputType(typeDefinition);
if (outputType != null) {
return typeInfo.decorate(outputType);
}
if (buildCtx.stackContains(typeInfo)) {
// otherwise we will go into an infinite loop
return typeInfo.decorate(typeRef(typeInfo.getName()));
}
buildCtx.push(typeInfo);
if (typeDefinition instanceof ObjectTypeDefinition) {
outputType = buildObjectType(buildCtx, (ObjectTypeDefinition) typeDefinition);
} else if (typeDefinition instanceof InterfaceTypeDefinition) {
outputType = buildInterfaceType(buildCtx, (InterfaceTypeDefinition) typeDefinition);
} else if (typeDefinition instanceof UnionTypeDefinition) {
outputType = buildUnionType(buildCtx, (UnionTypeDefinition) typeDefinition);
} else if (typeDefinition instanceof EnumTypeDefinition) {
outputType = buildEnumType(buildCtx, (EnumTypeDefinition) typeDefinition);
} else if (typeDefinition instanceof ScalarTypeDefinition) {
outputType = buildScalar(buildCtx, (ScalarTypeDefinition) typeDefinition);
} else {
// typeDefinition is not a valid output type
throw new NotAnOutputTypeError(typeDefinition);
}
buildCtx.put(outputType);
buildCtx.pop();
return (T) typeInfo.decorate(outputType);
}
use of graphql.language.UnionTypeDefinition in project graphql-java by graphql-java.
the class SchemaTypeChecker method checkTypeResolversArePresent.
private void checkTypeResolversArePresent(List<GraphQLError> errors, TypeDefinitionRegistry typeRegistry, RuntimeWiring wiring) {
Predicate<InterfaceTypeDefinition> noDynamicResolverForInterface = interaceTypeDef -> !wiring.getWiringFactory().providesTypeResolver(new InterfaceWiringEnvironment(typeRegistry, interaceTypeDef));
Predicate<UnionTypeDefinition> noDynamicResolverForUnion = unionTypeDef -> !wiring.getWiringFactory().providesTypeResolver(new UnionWiringEnvironment(typeRegistry, unionTypeDef));
Predicate<TypeDefinition> noTypeResolver = typeDefinition -> !wiring.getTypeResolvers().containsKey(typeDefinition.getName());
Consumer<TypeDefinition> addError = typeDefinition -> errors.add(new MissingTypeResolverError(typeDefinition));
typeRegistry.types().values().stream().filter(typeDef -> typeDef instanceof InterfaceTypeDefinition).map(InterfaceTypeDefinition.class::cast).filter(noDynamicResolverForInterface).filter(noTypeResolver).forEach(addError);
typeRegistry.types().values().stream().filter(typeDef -> typeDef instanceof UnionTypeDefinition).map(UnionTypeDefinition.class::cast).filter(noDynamicResolverForUnion).filter(noTypeResolver).forEach(addError);
}
use of graphql.language.UnionTypeDefinition in project graphql-java by graphql-java.
the class SchemaTypeExtensionsChecker method checkUnionTypeExtensions.
/*
* Union type extensions have the potential to be invalid if incorrectly defined.
*
* The named type must already be defined and must be a Union type.
* The member types of a Union type extension must all be Object base types; Scalar, Interface and Union types must not be member types of a Union. Similarly, wrapping types must not be member types of a Union.
* All member types of a Union type extension must be unique.
* All member types of a Union type extension must not already be a member of the original Union type.
* Any directives provided must not already apply to the original Union type.
*/
private void checkUnionTypeExtensions(List<GraphQLError> errors, TypeDefinitionRegistry typeRegistry) {
typeRegistry.unionTypeExtensions().forEach((name, extensions) -> {
checkTypeExtensionHasCorrespondingType(errors, typeRegistry, name, extensions, UnionTypeDefinition.class);
checkTypeExtensionDirectiveRedefinition(errors, typeRegistry, name, extensions, UnionTypeDefinition.class);
extensions.forEach(extension -> {
List<TypeName> memberTypes = extension.getMemberTypes().stream().map(t -> TypeInfo.typeInfo(t).getTypeName()).collect(Collectors.toList());
checkNamedUniqueness(errors, memberTypes, TypeName::getName, (namedMember, memberType) -> new NonUniqueNameError(extension, namedMember));
memberTypes.forEach(memberType -> {
Optional<ObjectTypeDefinition> unionTypeDefinition = typeRegistry.getType(memberType, ObjectTypeDefinition.class);
if (!unionTypeDefinition.isPresent()) {
errors.add(new MissingTypeError("union member", extension, memberType));
}
});
});
});
}
Aggregations