/** * This helps you transform the current GraphQLUnionType into another one by starting a builder with all * the current values and allows you to transform it how you want. * * @param builderConsumer the consumer code that will be given a builder to transform * * @return a new object based on calling build on that builder */ public GraphQLUnionType transform(Consumer<Builder> builderConsumer) { Builder builder = newUnionType(this); builderConsumer.accept(builder); return builder.build(); }
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)); }
public GraphQLUnionType build() { return new GraphQLUnionType(name, description, valuesToList(types), typeResolver, valuesToList(directives), definition); } }
private static TypeResolver getTypeResolverForUnion(GraphQLUnionType parentType, Map<String, TypeResolver> typeResolverMap) { assertNotNull(parentType); TypeResolver typeResolver = typeResolverMap.get(parentType.getName()); if (typeResolver == null) { typeResolver = parentType.getTypeResolver(); } return assertNotNull(typeResolver, "There must be a type resolver for union " + parentType.getName()); }
@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); }
GraphQLUnionType.Builder builder = newUnionType() .name(name) .description(description) buildContext.typeRegistry.registerCovariantType(union.getName(), possibleJavaTypes.get(i), union.getTypes().get(i));
private GraphQLUnionType buildUnionType(BuildContext buildCtx, UnionTypeDefinition typeDefinition) { buildCtx.enterNode(typeDefinition); GraphQLUnionType.Builder builder = GraphQLUnionType.newUnionType(); builder.definition(typeDefinition); builder.name(typeDefinition.getName()); if (!buildCtx.codeRegistry.hasTypeResolver(unionType.getName())) { TypeResolver typeResolver = getTypeResolverForUnion(buildCtx, typeDefinition); buildCtx.getCodeRegistry().typeResolver(unionType, typeResolver);
boolean objectIsMemberOfUnion(GraphQLUnionType unionType, GraphQLOutputType objectType) { return unionType.getTypes().contains(objectType); }
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"); }; }
@Test(expectedExceptions = InvalidDirectiveLocationException.class) public void wireGraphQLUnionType_invalidLocations_exceptionIsThrown() throws Exception { // Arrange AnnotationsDirectiveWiring upperWiring = mock(AnnotationsDirectiveWiring.class); GraphQLUnionType directiveContainer = GraphQLUnionType.newUnionType().name("asdf") .possibleType(GraphQLObjectType.newObject().name("Asdfaaaa").build()).typeResolver(env -> null).build(); AnnotationsWiringEnvironmentImpl upperCaseEnv = new AnnotationsWiringEnvironmentImpl(directiveContainer, directiveContainer.getDirective("upperCase")); when(upperWiring.onUnion(upperCaseEnv)).thenReturn(directiveContainer); HashMap<GraphQLDirective, AnnotationsDirectiveWiring> map = new HashMap<>(); GraphQLDirective upperCase = newDirective().name("upperCase").build(); map.put(upperCase, upperWiring); // Act directiveWirer.wire(directiveContainer, map); }
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))); }
public Builder typeResolver(GraphQLUnionType parentType, TypeResolver typeResolver) { typeResolverMap.put(parentType.getName(), typeResolver); return this; }
@Test public void getResolver_resolverIsDefaultOne_returnsUnionTypeResolver() throws Exception { //Arrange GraphQLInterfaceRetriever graphQLInterfaceRetriever = GraphQLAnnotations.getInstance().getObjectHandler().getTypeRetriever().getGraphQLInterfaceRetriever(); //Act GraphQLUnionType unionType = (GraphQLUnionType) graphQLInterfaceRetriever.getInterface(Hardware.class, GraphQLAnnotations.getInstance().getContainer()); TypeResolver typeResolver = unionType.getTypeResolver(); //Assert assertThat(typeResolver, instanceOf(UnionTypeResolver.class)); }
} else if (descriptionHolder instanceof GraphQLUnionType) { GraphQLUnionType type = (GraphQLUnionType) descriptionHolder; return descriptionAndComments(type::getDescription, type::getDefinition, () -> type.getDefinition().getDescription()); } else if (descriptionHolder instanceof GraphQLInputObjectType) { GraphQLInputObjectType type = (GraphQLInputObjectType) descriptionHolder;
private GraphQLUnionType buildUnionType(BuildContext buildCtx, UnionTypeDefinition typeDefinition) { buildCtx.enterNode(typeDefinition); GraphQLUnionType.Builder builder = GraphQLUnionType.newUnionType(); builder.definition(typeDefinition); builder.name(typeDefinition.getName()); if (!buildCtx.codeRegistry.hasTypeResolver(unionType.getName())) { TypeResolver typeResolver = getTypeResolverForUnion(buildCtx, typeDefinition); buildCtx.getCodeRegistry().typeResolver(unionType, typeResolver);
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; } 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"); }; }
@Test public void wireGraphQLUnionType_validLocations_correctMethodIsCalled() throws Exception { // Arrange AnnotationsDirectiveWiring upperWiring = mock(AnnotationsDirectiveWiring.class); GraphQLUnionType directiveContainer = GraphQLUnionType.newUnionType().name("asdf") .possibleType(GraphQLObjectType.newObject().name("Asdfaaaa").build()).typeResolver(env -> null).build(); AnnotationsWiringEnvironmentImpl upperCaseEnv = new AnnotationsWiringEnvironmentImpl(directiveContainer, directiveContainer.getDirective("upperCase")); when(upperWiring.onUnion(upperCaseEnv)).thenReturn(directiveContainer); HashMap<GraphQLDirective, AnnotationsDirectiveWiring> map = new HashMap<>(); GraphQLDirective upperCase = newDirective().name("upperCase").validLocations(Introspection.DirectiveLocation.UNION).build(); map.put(upperCase, upperWiring); // Act directiveWirer.wire(directiveContainer, map); // Assert verify(upperWiring).onUnion(upperCaseEnv); }
@Override public TraversalControl visitGraphQLUnionType(GraphQLUnionType node, TraverserContext<GraphQLType> context) { TypeResolver typeResolver = node.getTypeResolver(); if (typeResolver != null) { codeRegistry.typeResolverIfAbsent(node, typeResolver); } assertTrue(codeRegistry.getTypeResolver(node) != null, "You MUST provide a type resolver for the union type '" + node.getName() + "'"); return CONTINUE; } }
@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); }