graphql.language.UnionTypeDefinition Java Examples

The following examples show how to use graphql.language.UnionTypeDefinition. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GqlParentType.java    From manifold with Apache License 2.0 6 votes vote down vote up
private void addInnerUnionType( UnionTypeDefinition type, SrcLinkedClass enclosingType )
{
  String identifier = makeIdentifier( type.getName(), false );
  String fqn = getFqn() + '.' + identifier;
  SrcLinkedClass srcClass = new SrcLinkedClass( fqn, enclosingType, Interface )
    .addInterface( IJsonBindingsBacked.class.getSimpleName() )
    .addAnnotation( new SrcAnnotationExpression( Structural.class.getSimpleName() )
      .addArgument( "factoryClass", Class.class, identifier + ".ProxyFactory.class" ) )
    .modifiers( Modifier.PUBLIC );
  addUnionInterfaces( type, srcClass );
  String lub = findLub( type );
  if( lub != null )
  {
    srcClass.addInterface( makeIdentifier( lub, false ) );
  }
  addActualNameAnnotation( srcClass, type.getName(), false );
  addSourcePositionAnnotation( srcClass, type, srcClass );
  addProxyFactory( srcClass );
  addIntersectionMethods( srcClass, type );
  enclosingType.addInnerClass( srcClass );
}
 
Example #2
Source File: TypeEntry.java    From graphql-apigen with Apache License 2.0 6 votes vote down vote up
private static List<Directive> getDirectives(Definition def) {
    if ( def instanceof ObjectTypeDefinition ) {
        return ((ObjectTypeDefinition)def).getDirectives();
    } if ( def instanceof InterfaceTypeDefinition ) {
        return ((InterfaceTypeDefinition)def).getDirectives();
    } if ( def instanceof EnumTypeDefinition ) {
        return ((EnumTypeDefinition)def).getDirectives();
    } if ( def instanceof ScalarTypeDefinition ) {
        return ((ScalarTypeDefinition)def).getDirectives();
    } if ( def instanceof UnionTypeDefinition ) {
        return ((UnionTypeDefinition)def).getDirectives();
    } if ( def instanceof InputObjectTypeDefinition ) {
        return ((InputObjectTypeDefinition)def).getDirectives();
    } if ( def instanceof SchemaDefinition ) {
        return ((SchemaDefinition)def).getDirectives();
    }
    return Collections.emptyList();
}
 
Example #3
Source File: STModel.java    From graphql-apigen with Apache License 2.0 6 votes vote down vote up
public synchronized List<String> getImports() {
    if ( null == imports ) {
        Definition def = typeEntry.getDefinition();
        Set<String> names = new TreeSet<String>();
        if ( isObjectType() ) {
            addImports(names, (ObjectTypeDefinition)def);
        } else if ( isInterfaceType() ) {
            addImports(names, (InterfaceTypeDefinition)def);
        } else if ( isInputObjectType() ) {
            addImports(names, (InputObjectTypeDefinition)def);
        } else if ( isUnionType() ) {
            addImports(names, (UnionTypeDefinition)def);
        } else if ( isEnumType() ) {
            addImports(names, (EnumTypeDefinition)def);
        } else if ( isSchemaType() ) {
            addImports(names, (SchemaDefinition)def);
        }
        imports = new ArrayList<>(names);
    }
    return imports;
}
 
Example #4
Source File: STModel.java    From graphql-apigen with Apache License 2.0 6 votes vote down vote up
public synchronized List<Field> getFields() {
    if ( null == fields ) {
        Definition def = typeEntry.getDefinition();
        if ( isObjectType() ) {
            fields = getFields((ObjectTypeDefinition)def);
        } else if ( isInterfaceType() ) {
            fields = getFields((InterfaceTypeDefinition)def);
        } else if ( isInputObjectType() ) {
            fields = getFields((InputObjectTypeDefinition)def);
        } else if ( isUnionType() ) {
            fields = getFields((UnionTypeDefinition)def);
        } else if ( isEnumType() ) {
            fields = getFields((EnumTypeDefinition)def);
        } else if ( isSchemaType() ) {
            fields = getFields((SchemaDefinition)def);
        } else {
            fields = Collections.emptyList();
        }
    }
    return fields;
}
 
Example #5
Source File: GraphQLIntrospectionResultToSchema.java    From js-graphql-intellij-plugin with MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked")
UnionTypeDefinition createUnion(Map<String, Object> input) {
    assertTrue(input.get("kind").equals("UNION"), () -> "wrong input");

    final List<Map<String, Object>> possibleTypes = (List<Map<String, Object>>) input.get("possibleTypes");
    final List<Type> memberTypes = Lists.newArrayList();
    for (Map<String, Object> possibleType : possibleTypes) {
        TypeName typeName = new TypeName((String) possibleType.get("name"));
        memberTypes.add(typeName);
    }

    return UnionTypeDefinition.newUnionTypeDefinition()
            .name((String) input.get("name"))
            .description(getDescription(input))
            .memberTypes(memberTypes)
            .build();
}
 
Example #6
Source File: GqlParentType.java    From manifold with Apache License 2.0 5 votes vote down vote up
private void addInnerTypes( SrcLinkedClass srcClass )
{
  mapUnionMemberToUnions();

  for( TypeDefinition type: _registry.types().values() )
  {
    if( type instanceof ObjectTypeDefinition )
    {
      addInnerObjectType( (ObjectTypeDefinition)type, srcClass );
    }
    else if( type instanceof InterfaceTypeDefinition )
    {
      addInnerInterfaceType( (InterfaceTypeDefinition)type, srcClass );
    }
    else if( type instanceof EnumTypeDefinition )
    {
      addInnerEnumType( (EnumTypeDefinition)type, srcClass );
    }
    else if( type instanceof InputObjectTypeDefinition )
    {
      addInnerInputType( (InputObjectTypeDefinition)type, srcClass );
    }
    else if( type instanceof UnionTypeDefinition )
    {
      addInnerUnionType( (UnionTypeDefinition)type, srcClass );
    }
  }
}
 
Example #7
Source File: GqlParentType.java    From manifold with Apache License 2.0 5 votes vote down vote up
private void mapUnionMemberToUnions()
{
  _registry.types().values().stream()
    .filter( typeDef -> typeDef instanceof UnionTypeDefinition )
    .map( typeDef -> (UnionTypeDefinition)typeDef )
    .forEach( unionTypeDef -> unionTypeDef.getMemberTypes().stream()
      .map( memberType -> findTypeDefinition( memberType ) )
      .forEach( memberTypeDef ->
        _typeToUnions.computeIfAbsent( memberTypeDef, t -> new HashSet<>() )
          .add( unionTypeDef ) ) );
}
 
Example #8
Source File: GqlParentType.java    From manifold with Apache License 2.0 5 votes vote down vote up
private void addUnionInterfaces( TypeDefinition type, SrcLinkedClass srcClass )
{
  Set<UnionTypeDefinition> unions = _typeToUnions.get( type );
  if( unions != null )
  {
    unions.forEach( union -> srcClass.addInterface( makeIdentifier( union.getName(), false ) ) );
  }
}
 
Example #9
Source File: GqlParentType.java    From manifold with Apache License 2.0 5 votes vote down vote up
private void addIntersectionMethods( SrcLinkedClass srcClass, UnionTypeDefinition union )
{
  Set<Pair<String, String>> fieldDefs = new HashSet<>();
  Map<String, FieldDefinition> nameToFieldDef = new HashMap<>();
  for( Type memberType: union.getMemberTypes() )
  {
    TypeDefinition typeDef = findTypeDefinition( memberType );
    if( typeDef instanceof ObjectTypeDefinition )
    {
      if( fieldDefs.isEmpty() )
      {
        fieldDefs.addAll( ((ObjectTypeDefinition)typeDef).getFieldDefinitions()
          .stream().map( e -> {
            nameToFieldDef.put( e.getName(), e );
            return new Pair<>( e.getName(), e.getType().toString() );
          } ).collect( Collectors.toSet() ) );
      }
      else
      {
        fieldDefs.retainAll( ((ObjectTypeDefinition)typeDef).getFieldDefinitions()
          .stream().map( e -> new Pair<>( e.getName(), e.getType().toString() ) ).collect( Collectors.toSet() ) );
      }
    }
  }
  fieldDefs.forEach(
    fieldDef -> addMember( srcClass, null, nameToFieldDef.get( fieldDef.getFirst() ).getType(), fieldDef.getFirst(),
      name -> fieldDefs.stream().anyMatch( f -> f.getFirst().equals( name ) ) ) );
}
 
Example #10
Source File: GqlParentType.java    From manifold with Apache License 2.0 5 votes vote down vote up
private String findLub( UnionTypeDefinition typeDef )
{
  Set<Set<InterfaceTypeDefinition>> ifaces = new HashSet<>();
  for( Type t: typeDef.getMemberTypes() )
  {
    TypeDefinition td = findTypeDefinition( t );
    if( td instanceof ObjectTypeDefinition )
    {
      ifaces.add( ((ObjectTypeDefinition)td).getImplements().stream()
        .map( type -> (InterfaceTypeDefinition)findTypeDefinition( type ) )
        .collect( Collectors.toSet() ) );
    }
    else if( td instanceof InterfaceTypeDefinition )
    {
      ifaces.add( Collections.singleton( (InterfaceTypeDefinition)td ) );
    }
  }
  //noinspection OptionalGetWithoutIsPresent
  Set<InterfaceTypeDefinition> intersection =
    ifaces.stream().reduce( ( p1, p2 ) -> {
      p1.retainAll( p2 );
      return p1;
    } ).get();
  if( intersection.isEmpty() )
  {
    // no common interface
    return null;
  }
  else
  {
    // can only use one
    return intersection.iterator().next().getName();
  }
}
 
Example #11
Source File: GqlParentType.java    From manifold with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unused")
private String getStartSymbol( Node node )
{
  String start = "";
  if( node instanceof TypeDefinition )
  {
    if( node instanceof ObjectTypeDefinition )
    {
      start = "type";
    }
    else if( node instanceof EnumTypeDefinition )
    {
      start = "enum";
    }
    else if( node instanceof InputObjectTypeDefinition )
    {
      start = "input";
    }
    else if( node instanceof InterfaceTypeDefinition )
    {
      start = "interface";
    }
    else if( node instanceof ScalarTypeDefinition )
    {
      start = "scalar";
    }
    else if( node instanceof UnionTypeDefinition )
    {
      start = "union";
    }
  }
  else if( node instanceof OperationDefinition )
  {
    start = ((OperationDefinition)node).getOperation().name().toLowerCase();
  }
  return start;
}
 
Example #12
Source File: STModel.java    From graphql-apigen with Apache License 2.0 5 votes vote down vote up
private List<Field> getFields(UnionTypeDefinition def) {
    List<Field> fields = new ArrayList<Field>();
    for ( Type type : def.getMemberTypes() ) {
        fields.add(new Field(null, toJavaTypeName(type)));
    }
    return fields;
}
 
Example #13
Source File: STModel.java    From graphql-apigen with Apache License 2.0 4 votes vote down vote up
public boolean isUnionType() {
    return typeEntry.getDefinition() instanceof UnionTypeDefinition;
}
 
Example #14
Source File: STModel.java    From graphql-apigen with Apache License 2.0 4 votes vote down vote up
private void addImports(Collection<String> imports, UnionTypeDefinition def) {
    for ( Type type : def.getMemberTypes() ) {
        addImports(imports, type);
    }
}