boolean objectIsMemberOfUnion(GraphQLUnionType unionType, GraphQLOutputType objectType) { return unionType.getTypes().contains(objectType); }
@Override public TraversalControl visitGraphQLUnionType(GraphQLUnionType node, TraverserContext<GraphQLType> context) { node.replaceTypes(node.getTypes().stream() .map(type -> (GraphQLOutputType) typeMap.get(type.getName())) .collect(Collectors.toList())); return super.visitGraphQLUnionType(node, context); }
private boolean checkTypeCondition(FieldCollectorParameters parameters, GraphQLType conditionType) { GraphQLObjectType type = parameters.getObjectType(); if (conditionType.equals(type)) { return true; } if (conditionType instanceof GraphQLInterfaceType) { List<GraphQLObjectType> implementations = parameters.getGraphQLSchema().getImplementations((GraphQLInterfaceType) conditionType); return implementations.contains(type); } else if (conditionType instanceof GraphQLUnionType) { return ((GraphQLUnionType) conditionType).getTypes().contains(type); } return false; }
private TypePrinter<GraphQLUnionType> unionPrinter() { return (out, type, visibility) -> { if (isIntrospectionType(type)) { return; } printComments(out, type, ""); out.format("union %s%s = ", type.getName(), directivesString(type.getDirectives())); List<GraphQLOutputType> types = type.getTypes() .stream() .sorted(Comparator.comparing(GraphQLOutputType::getName)) .collect(toList()); for (int i = 0; i < types.size(); i++) { GraphQLOutputType objectType = types.get(i); if (i > 0) { out.format(" | "); } out.format("%s", objectType.getName()); } out.format("\n\n"); }; }
/** * Returns true if a specified concrete type is a possible type of a provided abstract type. * If the provided abstract type is: * - an interface, it checks whether the concrete type is one of its implementations. * - a union, it checks whether the concrete type is one of its possible types. * * @param abstractType abstract type either interface or union * @param concreteType concrete type * * @return true if possible type, false otherwise. */ public boolean isPossibleType(GraphQLType abstractType, GraphQLObjectType concreteType) { if (abstractType instanceof GraphQLInterfaceType) { return getImplementations((GraphQLInterfaceType) abstractType).stream() .map(GraphQLType::getName) .anyMatch(name -> concreteType.getName().equals(name)); } else if (abstractType instanceof GraphQLUnionType) { return ((GraphQLUnionType) abstractType).getTypes().stream() .map(GraphQLType::getName) .anyMatch(name -> concreteType.getName().equals(name)); } return assertShouldNeverHappen("Unsupported abstract type %s. Abstract types supported are Union and Interface.", abstractType.getName()); }
private List<? extends GraphQLType> getPossibleType(GraphQLType type) { List<? extends GraphQLType> possibleConditionTypes = null; if (type instanceof GraphQLObjectType) { possibleConditionTypes = Collections.singletonList(type); } else if (type instanceof GraphQLInterfaceType) { possibleConditionTypes = getValidationContext().getSchema().getImplementations((GraphQLInterfaceType) type); } else if (type instanceof GraphQLUnionType) { possibleConditionTypes = ((GraphQLUnionType) type).getTypes(); } else { Assert.assertShouldNeverHappen(); } return possibleConditionTypes; }
public Builder(GraphQLUnionType existing) { this.name = existing.getName(); this.description = existing.getDescription(); this.typeResolver = existing.getTypeResolver(); this.definition = existing.getDefinition(); this.types.putAll(getByName(existing.getTypes(), GraphQLType::getName)); this.directives.putAll(getByName(existing.getDirectives(), GraphQLDirective::getName)); }
boolean objectIsMemberOfUnion(GraphQLUnionType unionType, GraphQLOutputType objectType) { return unionType.getTypes().contains(objectType); }
@Override public TraversalControl visitGraphQLUnionType(GraphQLUnionType node, TraverserContext<GraphQLType> context) { node.replaceTypes(node.getTypes().stream() .map(type -> (GraphQLOutputType) typeMap.get(type.getName())) .collect(Collectors.toList())); return super.visitGraphQLUnionType(node, context); }
private boolean checkTypeCondition(FieldCollectorParameters parameters, GraphQLType conditionType) { GraphQLObjectType type = parameters.getObjectType(); if (conditionType.equals(type)) { return true; } if (conditionType instanceof GraphQLInterfaceType) { List<GraphQLObjectType> implementations = parameters.getGraphQLSchema().getImplementations((GraphQLInterfaceType) conditionType); return implementations.contains(type); } else if (conditionType instanceof GraphQLUnionType) { return ((GraphQLUnionType) conditionType).getTypes().contains(type); } return false; }
public static void replaceTypeReferencesForUnionType(final GraphQLSchema schema, final Set<GraphQLUnionType> graphQLUnionTypeMap) { Field graphQLTypesField = ReflectionUtils.findField(GraphQLUnionType.class, "types"); graphQLTypesField.setAccessible(true); for (GraphQLUnionType graphQLUnionType : graphQLUnionTypeMap) { List<GraphQLType> graphQLTypes = new ArrayList<>(); for (GraphQLType graphQLType : graphQLUnionType.getTypes()) { if (graphQLType instanceof GraphQLTypeReference) { graphQLTypes.add(schema.getType(graphQLType.getName())); } else { graphQLTypes.add(graphQLType); } } ReflectionUtils.setField(graphQLTypesField, graphQLUnionType, graphQLTypes); } } }
public TypeRegistry(Collection<GraphQLType> knownTypes) { //extract known interface implementations knownTypes.stream() .filter(type -> type instanceof GraphQLObjectType && Directives.isMappedType(type)) .map(type -> (GraphQLObjectType) type) .forEach(obj -> obj.getInterfaces().forEach( inter -> registerCovariantType(inter.getName(), Directives.getMappedType(obj), obj))); //extract known union members knownTypes.stream() .filter(type -> type instanceof GraphQLUnionType) .map(type -> (GraphQLUnionType) type) .forEach(union -> union.getTypes().stream() .filter(type -> type instanceof GraphQLObjectType && Directives.isMappedType(type)) .map(type -> (GraphQLObjectType) type) .forEach(obj -> registerCovariantType(union.getName(), Directives.getMappedType(obj), obj))); }
/** * Returns true if a specified concrete type is a possible type of a provided abstract type. * If the provided abstract type is: * - an interface, it checks whether the concrete type is one of its implementations. * - a union, it checks whether the concrete type is one of its possible types. * * @param abstractType abstract type either interface or union * @param concreteType concrete type * * @return true if possible type, false otherwise. */ public boolean isPossibleType(GraphQLType abstractType, GraphQLObjectType concreteType) { if (abstractType instanceof GraphQLInterfaceType) { return getImplementations((GraphQLInterfaceType) abstractType).stream() .map(GraphQLType::getName) .anyMatch(name -> concreteType.getName().equals(name)); } else if (abstractType instanceof GraphQLUnionType) { return ((GraphQLUnionType) abstractType).getTypes().stream() .map(GraphQLType::getName) .anyMatch(name -> concreteType.getName().equals(name)); } return assertShouldNeverHappen("Unsupported abstract type %s. Abstract types supported are Union and Interface.", abstractType.getName()); }
private List<? extends GraphQLType> getPossibleType(GraphQLType type) { List<? extends GraphQLType> possibleConditionTypes = null; if (type instanceof GraphQLObjectType) { possibleConditionTypes = Collections.singletonList(type); } else if (type instanceof GraphQLInterfaceType) { possibleConditionTypes = getValidationContext().getSchema().getImplementations((GraphQLInterfaceType) type); } else if (type instanceof GraphQLUnionType) { possibleConditionTypes = ((GraphQLUnionType) type).getTypes(); } else { Assert.assertShouldNeverHappen(); } return possibleConditionTypes; }
private TypePrinter<GraphQLUnionType> unionPrinter() { return (out, type, visibility) -> { if (isIntrospectionType(type)) { return; } SchemaPrinterComparatorEnvironment environment = SchemaPrinterComparatorEnvironment.newEnvironment() .parentType(GraphQLUnionType.class) .elementType(GraphQLOutputType.class) .build(); Comparator<? super GraphQLType> comparator = options.comparatorRegistry.getComparator(environment); printComments(out, type, ""); out.format("union %s%s = ", type.getName(), directivesString(GraphQLUnionType.class, type.getDirectives())); List<GraphQLOutputType> types = type.getTypes() .stream() .sorted(comparator) .collect(toList()); for (int i = 0; i < types.size(); i++) { GraphQLOutputType objectType = types.get(i); if (i > 0) { out.format(" | "); } out.format("%s", objectType.getName()); } out.format("\n\n"); }; }
public Builder(GraphQLUnionType existing) { this.name = existing.getName(); this.description = existing.getDescription(); this.typeResolver = existing.getTypeResolver(); this.definition = existing.getDefinition(); this.types.putAll(getByName(existing.getTypes(), GraphQLType::getName)); this.directives.putAll(getByName(existing.getDirectives(), GraphQLDirective::getName)); }
@Test public void testUnion() { GraphQLInterfaceRetriever graphQLInterfaceRetriever = GraphQLAnnotations.getInstance().getObjectHandler().getTypeRetriever().getGraphQLInterfaceRetriever(); GraphQLUnionType unionType = (GraphQLUnionType) graphQLInterfaceRetriever.getInterface(TestUnion.class, GraphQLAnnotations.getInstance().getContainer()); assertEquals(unionType.getTypes().size(), 1); assertEquals(unionType.getTypes().get(0).getName(), "TestObject1"); }
buildContext.typeRegistry.registerCovariantType(union.getName(), possibleJavaTypes.get(i), union.getTypes().get(i));