use of graphql.schema.GraphQLUnionType in project graphql-java by graphql-java.
the class ExecutionStrategy method resolveType.
protected GraphQLObjectType resolveType(ExecutionContext executionContext, ExecutionStrategyParameters parameters, GraphQLType fieldType) {
GraphQLObjectType resolvedType;
if (fieldType instanceof GraphQLInterfaceType) {
TypeResolutionParameters resolutionParams = TypeResolutionParameters.newParameters().graphQLInterfaceType((GraphQLInterfaceType) fieldType).field(parameters.getField().get(0)).value(parameters.getSource()).argumentValues(parameters.getArguments()).context(executionContext.getContext()).schema(executionContext.getGraphQLSchema()).build();
resolvedType = resolveTypeForInterface(resolutionParams);
} else if (fieldType instanceof GraphQLUnionType) {
TypeResolutionParameters resolutionParams = TypeResolutionParameters.newParameters().graphQLUnionType((GraphQLUnionType) fieldType).field(parameters.getField().get(0)).value(parameters.getSource()).argumentValues(parameters.getArguments()).context(executionContext.getContext()).schema(executionContext.getGraphQLSchema()).build();
resolvedType = resolveTypeForUnion(resolutionParams);
} else {
resolvedType = (GraphQLObjectType) fieldType;
}
return resolvedType;
}
use of graphql.schema.GraphQLUnionType in project graphql-java by graphql-java.
the class ObjectsImplementInterfaces method isCompatible.
/**
* @return {@code true} if the specified objectType satisfies the constraintType.
*/
boolean isCompatible(GraphQLOutputType constraintType, GraphQLOutputType objectType) {
if (isSameType(constraintType, objectType)) {
return true;
} else if (constraintType instanceof GraphQLUnionType) {
return objectIsMemberOfUnion((GraphQLUnionType) constraintType, objectType);
} else if (constraintType instanceof GraphQLInterfaceType && objectType instanceof GraphQLObjectType) {
return objectImplementsInterface((GraphQLInterfaceType) constraintType, (GraphQLObjectType) objectType);
} else if (constraintType instanceof GraphQLList && objectType instanceof GraphQLList) {
GraphQLOutputType wrappedConstraintType = (GraphQLOutputType) ((GraphQLList) constraintType).getWrappedType();
GraphQLOutputType wrappedObjectType = (GraphQLOutputType) ((GraphQLList) objectType).getWrappedType();
return isCompatible(wrappedConstraintType, wrappedObjectType);
} else if (objectType instanceof GraphQLNonNull) {
GraphQLOutputType nullableConstraint;
if (constraintType instanceof GraphQLNonNull) {
nullableConstraint = (GraphQLOutputType) ((GraphQLNonNull) constraintType).getWrappedType();
} else {
nullableConstraint = constraintType;
}
GraphQLOutputType nullableObjectType = (GraphQLOutputType) ((GraphQLNonNull) objectType).getWrappedType();
return isCompatible(nullableConstraint, nullableObjectType);
} else {
return false;
}
}
use of graphql.schema.GraphQLUnionType 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();
}
Aggregations