graphql.language.InterfaceTypeDefinition Java Examples

The following examples show how to use graphql.language.InterfaceTypeDefinition. 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 addInnerInterfaceType( InterfaceTypeDefinition 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 );
  addActualNameAnnotation( srcClass, type.getName(), false );
  addSourcePositionAnnotation( srcClass, type, srcClass );
  addProxyFactory( srcClass );
  List<FieldDefinition> fieldDefinitions = type.getFieldDefinitions();
  for( FieldDefinition member: fieldDefinitions )
  {
    addMember( srcClass, member, name -> fieldDefinitions.stream().anyMatch( f -> f.getName().equals( name ) ) );
  }
  addInterfaceExtensions( type, srcClass );
  enclosingType.addInnerClass( srcClass );
}
 
Example #2
Source File: GqlParentType.java    From manifold with Apache License 2.0 6 votes vote down vote up
private void addInterfaceExtensions( InterfaceTypeDefinition type, SrcLinkedClass srcClass )
{
  List<FieldDefinition> baseFieldDefinitions = type.getFieldDefinitions();
  List<InterfaceTypeExtensionDefinition> interfaceExtensions = _registry.interfaceTypeExtensions().get( type.getName() );
  if( interfaceExtensions != null )
  {
    for( InterfaceTypeExtensionDefinition ext: interfaceExtensions )
    {
      List<FieldDefinition> extFieldDefinitions = ext.getFieldDefinitions();
      for( FieldDefinition member: extFieldDefinitions )
      {
        addMember( srcClass, member,
          name ->
            baseFieldDefinitions.stream().anyMatch( f -> f.getName().equals( name ) ) ||
            extFieldDefinitions.stream().anyMatch( f -> f.getName().equals( name ) )
        );
      }
    }
  }
}
 
Example #3
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 #4
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 #5
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 #6
Source File: GraphQLInterfaceTypeFactory.java    From glitr with MIT License 6 votes vote down vote up
/**
 * Creates the {@link GraphQLInterfaceType} dynamically for the given interface
 *
 * @param clazz class to be introspected
 * @return {@link GraphQLInterfaceType} object exposed via graphQL
 */
private GraphQLInterfaceType createInterfaceType(Class clazz) {
    List<GraphQLFieldDefinition> fields = Arrays.stream(clazz.getMethods())
            .filter(ReflectionUtil::eligibleMethod)
            .sorted(Comparator.comparing(Method::getName))
            .map(method -> getGraphQLFieldDefinition(clazz, method))
            .collect(Collectors.toList());

    codeRegistryBuilder.typeResolver(clazz.getSimpleName(), typeRegistry);

    return newInterface()
            .name(clazz.getSimpleName())
            .definition(new InterfaceTypeDefinition(clazz.getCanonicalName()))
            .description(ReflectionUtil.getDescriptionFromAnnotatedElement(clazz))
            .fields(fields)
            .build();
}
 
Example #7
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 #8
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 #9
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 #10
Source File: STModel.java    From graphql-apigen with Apache License 2.0 5 votes vote down vote up
private List<Field> getFields(InterfaceTypeDefinition def) {
    List<Field> fields = new ArrayList<Field>();
    for ( FieldDefinition fieldDef : def.getFieldDefinitions() ) {
        Field field = new Field(fieldDef.getName(), toJavaTypeName(fieldDef.getType()));
        field.args = toArgs(fieldDef.getInputValueDefinitions());
        fields.add(field);
    }
    return fields;
}
 
Example #11
Source File: GraphQLIntrospectionResultToSchema.java    From js-graphql-intellij-plugin with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
InterfaceTypeDefinition createInterface(Map<String, Object> input) {
    assertTrue(input.get("kind").equals("INTERFACE"), () -> "wrong input");

    final List<Map<String, Object>> fields = (List<Map<String, Object>>) input.get("fields");
    final InterfaceTypeDefinition interfaceTypeDefinition = InterfaceTypeDefinition.newInterfaceTypeDefinition()
            .name((String) input.get("name"))
            .description(getDescription(input))
            .definitions(createFields(fields))
            .build();

    return interfaceTypeDefinition;

}
 
Example #12
Source File: STModel.java    From graphql-apigen with Apache License 2.0 4 votes vote down vote up
public boolean isInterfaceType() {
    return typeEntry.getDefinition() instanceof InterfaceTypeDefinition;
}
 
Example #13
Source File: STModel.java    From graphql-apigen with Apache License 2.0 4 votes vote down vote up
private void addImports(Collection<String> imports, InterfaceTypeDefinition def) {
    for ( FieldDefinition fieldDef : def.getFieldDefinitions() ) {
        addImports(imports, fieldDef.getType());
    }
}