use of graphql.language.UnionTypeDefinition in project graphql-java by graphql-java.
the class SchemaGenerator method buildUnionType.
private GraphQLUnionType buildUnionType(BuildContext buildCtx, UnionTypeDefinition typeDefinition) {
GraphQLUnionType.Builder builder = GraphQLUnionType.newUnionType();
builder.definition(typeDefinition);
builder.name(typeDefinition.getName());
builder.description(schemaGeneratorHelper.buildDescription(typeDefinition, typeDefinition.getDescription()));
builder.typeResolver(getTypeResolverForUnion(buildCtx, typeDefinition));
List<UnionTypeExtensionDefinition> extensions = unionTypeExtensions(typeDefinition, buildCtx);
typeDefinition.getMemberTypes().forEach(mt -> {
GraphQLOutputType outputType = buildOutputType(buildCtx, mt);
if (outputType instanceof GraphQLTypeReference) {
builder.possibleType((GraphQLTypeReference) outputType);
} else {
builder.possibleType((GraphQLObjectType) outputType);
}
});
builder.withDirectives(buildDirectives(typeDefinition.getDirectives(), directivesOf(extensions), UNION));
extensions.forEach(extension -> extension.getMemberTypes().forEach(mt -> {
GraphQLOutputType outputType = buildOutputType(buildCtx, mt);
if (!builder.containType(outputType.getName())) {
if (outputType instanceof GraphQLTypeReference) {
builder.possibleType((GraphQLTypeReference) outputType);
} else {
builder.possibleType((GraphQLObjectType) outputType);
}
}
}));
return builder.build();
}
use of graphql.language.UnionTypeDefinition in project graphql-java by graphql-java.
the class SchemaDiff method checkType.
private void checkType(DiffCtx ctx, Type oldType, Type newType) {
String typeName = getTypeName(oldType);
// prevent circular references
if (ctx.examiningType(typeName)) {
return;
}
if (isSystemScalar(typeName)) {
return;
}
if (isReservedType(typeName)) {
return;
}
Optional<TypeDefinition> oldTD = ctx.getOldTypeDef(oldType, TypeDefinition.class);
Optional<TypeDefinition> newTD = ctx.getNewTypeDef(newType, TypeDefinition.class);
if (!oldTD.isPresent()) {
ctx.report(DiffEvent.apiInfo().typeName(typeName).reasonMsg("Type '%s' is missing", typeName).build());
return;
}
TypeDefinition oldDef = oldTD.get();
ctx.report(DiffEvent.apiInfo().typeName(typeName).typeKind(getTypeKind(oldDef)).reasonMsg("Examining type '%s' ...", typeName).build());
if (!newTD.isPresent()) {
ctx.report(DiffEvent.apiBreakage().category(DiffCategory.MISSING).typeName(typeName).typeKind(getTypeKind(oldDef)).reasonMsg("The new API does not have a type called '%s'", typeName).build());
ctx.exitType();
return;
}
TypeDefinition newDef = newTD.get();
if (!oldDef.getClass().equals(newDef.getClass())) {
ctx.report(DiffEvent.apiBreakage().category(DiffCategory.INVALID).typeName(typeName).typeKind(getTypeKind(oldDef)).components(getTypeKind(oldDef), getTypeKind(newDef)).reasonMsg("The new API has changed '%s' from a '%s' to a '%s'", typeName, getTypeKind(oldDef), getTypeKind(newDef)).build());
ctx.exitType();
return;
}
if (oldDef instanceof ObjectTypeDefinition) {
checkObjectType(ctx, (ObjectTypeDefinition) oldDef, (ObjectTypeDefinition) newDef);
}
if (oldDef instanceof InterfaceTypeDefinition) {
checkInterfaceType(ctx, (InterfaceTypeDefinition) oldDef, (InterfaceTypeDefinition) newDef);
}
if (oldDef instanceof UnionTypeDefinition) {
checkUnionType(ctx, (UnionTypeDefinition) oldDef, (UnionTypeDefinition) newDef);
}
if (oldDef instanceof InputObjectTypeDefinition) {
checkInputObjectType(ctx, (InputObjectTypeDefinition) oldDef, (InputObjectTypeDefinition) newDef);
}
if (oldDef instanceof EnumTypeDefinition) {
checkEnumType(ctx, (EnumTypeDefinition) oldDef, (EnumTypeDefinition) newDef);
}
if (oldDef instanceof ScalarTypeDefinition) {
checkScalarType(ctx, (ScalarTypeDefinition) oldDef, (ScalarTypeDefinition) newDef);
}
ctx.exitType();
}
use of graphql.language.UnionTypeDefinition in project graphql-java by graphql-java.
the class IntrospectionResultToSchema method createUnion.
@SuppressWarnings("unchecked")
UnionTypeDefinition createUnion(Map<String, Object> input) {
assertTrue(input.get("kind").equals("UNION"), "wrong input");
UnionTypeDefinition unionTypeDefinition = new UnionTypeDefinition((String) input.get("name"));
unionTypeDefinition.setComments(toComment((String) input.get("description")));
List<Map<String, Object>> possibleTypes = (List<Map<String, Object>>) input.get("possibleTypes");
for (Map<String, Object> possibleType : possibleTypes) {
TypeName typeName = new TypeName((String) possibleType.get("name"));
unionTypeDefinition.getMemberTypes().add(typeName);
}
return unionTypeDefinition;
}
Aggregations