graphql.language.TypeName Java Examples

The following examples show how to use graphql.language.TypeName. 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: Generator.java    From smallrye-graphql with Apache License 2.0 6 votes vote down vote up
private String toJava(TypeName typeName) {
    String string = typeName.getName();
    switch (string) {
        case "Int":
            return "Integer";
        case "Boolean":
        case "Float":
        case "String":
            return string;
        case "ID":
            return "String";
        default:
            other.add(new TypeGenerator(string, fieldNames));
            return string;
    }
}
 
Example #2
Source File: STModel.java    From graphql-apigen with Apache License 2.0 6 votes vote down vote up
private void addImports(Collection<String> imports, Type type) {
    if ( type instanceof ListType ) {
        imports.add("java.util.List");
        addImports(imports, ((ListType)type).getType());
    } else if ( type instanceof NonNullType ) {
        addImports(imports, ((NonNullType)type).getType());
    } else if ( type instanceof TypeName ) {
        String name = ((TypeName)type).getName();
        if ( BUILTINS.containsKey(name) ) {
            String importName = BUILTINS.get(name);
            if ( null == importName ) return;
            imports.add(importName);
        } else {
            TypeEntry refEntry = referenceTypes.get(name);
            // TODO: scalar name may be different... should read annotations for scalars.
            if ( null == refEntry ) {
                throw new RuntimeException("Unknown type '"+name+"' was not defined in the schema");
            } else {
                imports.add(refEntry.getPackageName() + "." + name);
            }
        }
    } else {
        throw new RuntimeException("Unknown Type="+type.getClass().getName());
    }
}
 
Example #3
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 #4
Source File: GraphQLIntrospectionResultToSchema.java    From js-graphql-intellij-plugin with MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked")
private Type createTypeIndirection(Map<String, Object> type) {
    String kind = (String) type.get("kind");
    switch (kind) {
        case "INTERFACE":
        case "OBJECT":
        case "UNION":
        case "ENUM":
        case "INPUT_OBJECT":
        case "SCALAR":
            return TypeName.newTypeName().name((String) type.get("name")).build();
        case "NON_NULL":
            return NonNullType.newNonNullType().type(createTypeIndirection((Map<String, Object>) type.get("ofType"))).build();
        case "LIST":
            return ListType.newListType().type(createTypeIndirection((Map<String, Object>) type.get("ofType"))).build();
        default:
            return assertShouldNeverHappen("Unknown kind %s", kind);
    }
}
 
Example #5
Source File: Generator.java    From smallrye-graphql with Apache License 2.0 5 votes vote down vote up
@Override
public String toString() {
    if (type instanceof ListType)
        return toListJava((ListType) type);
    if (type instanceof NonNullType)
        return toNonNullJava((NonNullType) type);
    if (type instanceof TypeName)
        return toJava((TypeName) type);
    throw new UnsupportedOperationException("unexpected type of type"); // unreachable
}
 
Example #6
Source File: GqlParentType.java    From manifold with Apache License 2.0 5 votes vote down vote up
private void addInterfaces( SrcLinkedClass srcClass, List<Type> interfaces )
{
  for( Type iface: interfaces )
  {
    if( iface instanceof TypeName )
    {
      srcClass.addInterface( makeIdentifier( ((TypeName)iface).getName(), false ) );
    }
  }
}
 
Example #7
Source File: GqlParentType.java    From manifold with Apache License 2.0 5 votes vote down vote up
/**
 * Searches globally for a type i.e., across all .graphql files.
 */
private TypeDefinition findTypeDefinition( Type type )
{
  TypeName componentType = (TypeName)getComponentType( type );
  TypeDefinition typeDefinition = _registry.getType( componentType ).orElse( null );
  if( typeDefinition != null )
  {
    return typeDefinition;
  }

  return _gqlManifold.findTypeDefinition( componentType.getName() );
}
 
Example #8
Source File: GqlParentType.java    From manifold with Apache License 2.0 5 votes vote down vote up
/**
 * Searches globally for a scalar type i.e., across all .graphql files.
 */
private ScalarTypeDefinition findScalarTypeDefinition( TypeName type )
{
  TypeName componentType = (TypeName)getComponentType( type );
  ScalarTypeDefinition typeDefinition = _registry.scalars().get( componentType.getName() );
  if( typeDefinition != null )
  {
    return typeDefinition;
  }

  return _gqlManifold.findScalarTypeDefinition( componentType.getName() );
}
 
Example #9
Source File: GqlParentType.java    From manifold with Apache License 2.0 5 votes vote down vote up
private ListType getListType( Type type )
{
  if( type instanceof ListType )
  {
    return (ListType)type;
  }
  if( type instanceof TypeName )
  {
    return null;
  }
  return getListType( ((NonNullType)type).getType() );
}
 
Example #10
Source File: GqlParentType.java    From manifold with Apache License 2.0 5 votes vote down vote up
private SrcType makeSrcType( SrcLinkedClass owner, Type type, boolean typeParam )
{
  SrcType srcType;
  if( type instanceof ListType )
  {
    srcType = new SrcType( "List" );
    srcType.addTypeParam( makeSrcType( owner, ((ListType)type).getType(), true ) );
  }
  else if( type instanceof TypeName )
  {
    String typeName = getJavaClassName( owner, (TypeName)type, typeParam );
    srcType = new SrcType( typeName );
    if( !typeParam )
    {
      Class<?> javaClass = getJavaClass( (TypeName)type, false );
      srcType.setPrimitive( javaClass != null && javaClass.isPrimitive() );
    }
  }
  else if( type instanceof NonNullType )
  {
    Type theType = ((NonNullType)type).getType();
    srcType = makeSrcType( owner, theType, typeParam );
    if( !typeParam && !srcType.isPrimitive() )
    {
      srcType.addAnnotation( new SrcAnnotationExpression( NotNull.class.getSimpleName() ) );
    }
  }
  else
  {
    throw new IllegalStateException( "Unhandled type: " + type.getClass().getTypeName() );
  }
  return srcType;
}
 
Example #11
Source File: GqlParentType.java    From manifold with Apache License 2.0 5 votes vote down vote up
private String getJavaClassName( SrcLinkedClass owner, TypeName type, boolean boxed )
{
  ScalarTypeDefinition scalarType = findScalarTypeDefinition( type );
  if( scalarType == null )
  {
    // not a scalar type, therefore it must be a 'type'
    return makeIdentifier( owner.getDisambiguatedNameInNest( type.getName() ), false );
  }

  Class<?> cls = getJavaClass( type, boxed );
  return cls == null ? scalarType.getName() : getJavaName( cls );
}
 
Example #12
Source File: STModel.java    From graphql-apigen with Apache License 2.0 5 votes vote down vote up
private String toGraphQLType(Type type) {
    if ( type instanceof ListType ) {
        return "new GraphQLList(" + toGraphQLType(((ListType)type).getType()) + ")";
    } else if ( type instanceof NonNullType ) {
        return toGraphQLType(((NonNullType)type).getType());
    } else if ( type instanceof TypeName ) {
        String name = ((TypeName)type).getName();
        if ( BUILTINS.containsKey(name) ) {
            return "Scalars.GraphQL" + name;
        }
        return "new GraphQLTypeReference(\""+name+"\")";
    } else {
        throw new UnsupportedOperationException("Unknown Type="+type.getClass().getName());
    }
}
 
Example #13
Source File: STModel.java    From graphql-apigen with Apache License 2.0 5 votes vote down vote up
private String toJavaTypeName(Type type) {
    if ( type instanceof ListType ) {
        return "List<" + toJavaTypeName(((ListType)type).getType()) + ">";
    } else if ( type instanceof NonNullType ) {
        return toJavaTypeName(((NonNullType)type).getType());
    } else if ( type instanceof TypeName ) {
        String name = ((TypeName)type).getName();
        String rename = RENAME.get(name);
        // TODO: scalar type directive to get implementation class...
        if ( null != rename ) return rename;
        return name;
    } else {
        throw new UnsupportedOperationException("Unknown Type="+type.getClass().getName());
    }
}
 
Example #14
Source File: GqlParentType.java    From manifold with Apache License 2.0 4 votes vote down vote up
@Nullable
private Class<?> getJavaClass( TypeName type, boolean boxed )
{
  Class<?> cls;
  switch( type.getName() )
  {
    case "String":
    case "ID":
      cls = String.class;
      break;
    case "Byte":
      cls = boxed ? Byte.class : byte.class;
      break;
    case "Char":
    case "Character":
      cls = boxed ? Character.class : char.class;
      break;
    case "Int":
    case "Integer":
      cls = boxed ? Integer.class : int.class;
      break;
    case "Long":
      cls = boxed ? Long.class : long.class;
      break;
    case "Float":
    case "Double":
      cls = boxed ? Double.class : double.class;
      break;
    case "Boolean":
      cls = boxed ? Boolean.class : boolean.class;
      break;
    case "BigInteger":
      cls = BigInteger.class;
      break;
    case "BigDecimal":
      cls = BigDecimal.class;
      break;
    default:
      cls = findFormatType( type.getName() );
  }
  return cls;
}
 
Example #15
Source File: GraphQLIntrospectionResultToSchema.java    From js-graphql-intellij-plugin with MIT License 4 votes vote down vote up
/**
 * Returns a IDL Document that represents the schema as defined by the introspection result map
 *
 * @param introspectionResult the result of an introspection query on a schema
 *
 * @return a IDL Document of the schema
 */
@SuppressWarnings("unchecked")
public Document createSchemaDefinition(Map<String, Object> introspectionResult) {
    assertTrue(introspectionResult.get("__schema") != null, () -> "__schema expected");
    Map<String, Object> schema = (Map<String, Object>) introspectionResult.get("__schema");


    Map<String, Object> queryType = (Map<String, Object>) schema.get("queryType");
    assertNotNull(queryType, () -> "queryType expected");
    TypeName query = TypeName.newTypeName().name((String) queryType.get("name")).build();
    boolean nonDefaultQueryName = !"Query".equals(query.getName());

    SchemaDefinition.Builder schemaDefinition = SchemaDefinition.newSchemaDefinition();
    schemaDefinition.operationTypeDefinition(OperationTypeDefinition.newOperationTypeDefinition().name("query").typeName(query).build());

    Map<String, Object> mutationType = (Map<String, Object>) schema.get("mutationType");
    boolean nonDefaultMutationName = false;
    if (mutationType != null) {
        TypeName mutation = TypeName.newTypeName().name((String) mutationType.get("name")).build();
        nonDefaultMutationName = !"Mutation".equals(mutation.getName());
        schemaDefinition.operationTypeDefinition(OperationTypeDefinition.newOperationTypeDefinition().name("mutation").typeName(mutation).build());
    }

    Map<String, Object> subscriptionType = (Map<String, Object>) schema.get("subscriptionType");
    boolean nonDefaultSubscriptionName = false;
    if (subscriptionType != null) {
        TypeName subscription = TypeName.newTypeName().name(((String) subscriptionType.get("name"))).build();
        nonDefaultSubscriptionName = !"Subscription".equals(subscription.getName());
        schemaDefinition.operationTypeDefinition(OperationTypeDefinition.newOperationTypeDefinition().name("subscription").typeName(subscription).build());
    }

    Document.Builder document = Document.newDocument();
    if (nonDefaultQueryName || nonDefaultMutationName || nonDefaultSubscriptionName) {
        document.definition(schemaDefinition.build());
    }

    List<Map<String, Object>> types = (List<Map<String, Object>>) schema.get("types");
    for (Map<String, Object> type : types) {
        TypeDefinition typeDefinition = createTypeDefinition(type);
        if (typeDefinition == null) continue;
        document.definition(typeDefinition);
    }

    return document.build();
}