/** * 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(); }
void unionType() { GraphQLUnionType PetType = newUnionType() .name("Pet") .possibleType(CatType) .possibleType(DogType) .typeResolver(new TypeResolver() { @Override public GraphQLObjectType getType(TypeResolutionEnvironment env) { if (env.getObject() instanceof Cat) { return CatType; } if (env.getObject() instanceof Dog) { return DogType; } return null; } }) .build(); }
private GraphQLUnionType buildUnionType(BuildContext buildCtx, UnionTypeDefinition typeDefinition) { buildCtx.enterNode(typeDefinition); GraphQLUnionType.Builder builder = GraphQLUnionType.newUnionType(); builder.definition(typeDefinition); builder.name(typeDefinition.getName());
/** * 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(); }
@Override public GraphQLUnionType.Builder getUnionBuilder(Class<?> iface) throws GraphQLAnnotationsException, IllegalArgumentException { if (!iface.isInterface()) { throw new IllegalArgumentException(iface + " is not an interface"); } GraphQLUnionType.Builder builder = newUnionType(); GraphQLUnion unionAnnotation = iface.getAnnotation(GraphQLUnion.class); builder.name(getTypeName(iface)); GraphQLDescription description = iface.getAnnotation(GraphQLDescription.class); if (description != null) { builder.description(description.value()); } GraphQLType typeAnnotation = iface.getAnnotation(GraphQLType.class); TypeFunction typeFunction = defaultTypeFunction; if (typeAnnotation != null) { typeFunction = newInstance(typeAnnotation.value()); } TypeFunction finalTypeFunction = typeFunction; Arrays.asList(unionAnnotation.possibleTypes()).stream() .map(new Function<Class<?>, graphql.schema.GraphQLType>() { @Override public graphql.schema.GraphQLType apply(Class<?> aClass) { return finalTypeFunction.buildType(aClass, null); } }) .map(v -> (GraphQLObjectType) v) .forEach(builder::possibleType); builder.typeResolver(new UnionTypeResolver(unionAnnotation.possibleTypes())); return builder; }
throw new IllegalArgumentException(iface + " is not an interface"); Builder builder = newUnionType();
public GraphQLUnionType getSchemaFieldsType(GraphQLContext context) { Map<String, GraphQLObjectType> types = generateSchemaFieldType(context); GraphQLObjectType[] typeArray = types.values().toArray(new GraphQLObjectType[types.values().size()]); GraphQLUnionType fieldType = newUnionType().name(NODE_FIELDS_TYPE_NAME).possibleTypes(typeArray).description("Fields of the node.") .typeResolver(env -> { Object object = env.getObject(); if (object instanceof NodeGraphFieldContainer) { NodeGraphFieldContainer fieldContainer = (NodeGraphFieldContainer) object; String schemaName = fieldContainer.getSchemaContainerVersion().getName(); GraphQLObjectType foundType = types.get(schemaName); if (foundType == null) { throw new GraphQLException("The type for the schema with name {" + schemaName + "} could not be found. Maybe the schema is not linked to the project."); } return foundType; } return null; }).build(); return fieldType; }
public GraphQLType createType(GraphQLContext context) { Map<String, GraphQLObjectType> types = generateMicroschemaFieldType(context); // No microschemas have been found - We need to add a dummy type in order to keep the type system working if (types.isEmpty()) { types.put("dummy", newObject().name("dummy").field(newFieldDefinition().name("dummy").type(GraphQLString).staticValue(null).build()).description("Placeholder dummy microschema type").build()); } GraphQLObjectType[] typeArray = types.values().toArray(new GraphQLObjectType[types.values().size()]); GraphQLUnionType fieldType = newUnionType().name(MICRONODE_TYPE_NAME).possibleTypes(typeArray).description("Fields of the micronode.") .typeResolver(env -> { Object object = env.getObject(); if (object instanceof Micronode) { Micronode fieldContainer = (Micronode) object; MicroschemaContainerVersion micronodeFieldSchema = fieldContainer.getSchemaContainerVersion(); String schemaName = micronodeFieldSchema.getName(); GraphQLObjectType foundType = types.get(schemaName); return foundType; } return null; }).build(); return fieldType; }
GraphQLUnionType.Builder builder = newUnionType() .name(name) .description(description)
graphQLObjectType = GraphQLUnionType.newUnionType() .name(resolvableTypeAccessor.getName()) .possibleTypes(possibleTypes.toArray(new GraphQLType[possibleTypes.size()]))
@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); }
@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); }
private GraphQLUnionType buildUnionType(BuildContext buildCtx, UnionTypeDefinition typeDefinition) { buildCtx.enterNode(typeDefinition); GraphQLUnionType.Builder builder = GraphQLUnionType.newUnionType(); builder.definition(typeDefinition); builder.name(typeDefinition.getName());