github.com/weaviate/weaviate@v1.24.6/adapters/handlers/graphql/local/get/class_builder_nested.go (about) 1 // _ _ 2 // __ _____ __ ___ ___ __ _| |_ ___ 3 // \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \ 4 // \ V V / __/ (_| |\ V /| | (_| | || __/ 5 // \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___| 6 // 7 // Copyright © 2016 - 2024 Weaviate B.V. All rights reserved. 8 // 9 // CONTACT: hello@weaviate.io 10 // 11 12 package get 13 14 import ( 15 "fmt" 16 17 "github.com/tailor-inc/graphql" 18 "github.com/weaviate/weaviate/entities/models" 19 "github.com/weaviate/weaviate/entities/schema" 20 ) 21 22 func (b *classBuilder) nestedField(propertyType schema.PropertyDataType, 23 property *models.Property, className string, 24 ) *graphql.Field { 25 return b.parseNestedProperties(property.NestedProperties, className, property.Name, property.DataType) 26 } 27 28 func (b *classBuilder) parseNestedProperties(nestedProps []*models.NestedProperty, 29 className, prefix string, propDataType []string, 30 ) *graphql.Field { 31 fields := graphql.Fields{} 32 for _, prop := range nestedProps { 33 if prop.NestedProperties != nil { 34 fields[prop.Name] = b.parseNestedProperties(prop.NestedProperties, 35 className, fmt.Sprintf("%s_%s", prefix, prop.Name), prop.DataType) 36 } else { 37 fields[prop.Name] = &graphql.Field{ 38 Name: fmt.Sprintf("%s_%s_%s_field", className, prefix, prop.Name), 39 Type: b.determinNestedPropertyType(prop.DataType, prop.Name), 40 } 41 } 42 } 43 44 fieldType := graphql.NewObject(graphql.ObjectConfig{ 45 Name: fmt.Sprintf("%s_%s_object", className, prefix), 46 Fields: fields, 47 }) 48 49 if len(propDataType) == 1 && propDataType[0] == schema.DataTypeObjectArray.String() { 50 return &graphql.Field{Type: graphql.NewList(fieldType)} 51 } 52 return &graphql.Field{Type: fieldType} 53 } 54 55 func (b *classBuilder) determinNestedPropertyType(dataType []string, propName string) graphql.Output { 56 switch schema.DataType(dataType[0]) { 57 case schema.DataTypeText, schema.DataTypeString: 58 return graphql.String 59 case schema.DataTypeInt: 60 return graphql.Int 61 case schema.DataTypeNumber: 62 return graphql.Float 63 case schema.DataTypeBoolean: 64 return graphql.Boolean 65 case schema.DataTypeDate: 66 return graphql.String 67 case schema.DataTypeBlob: 68 return graphql.String 69 case schema.DataTypeUUID: 70 return graphql.String 71 case schema.DataTypeTextArray, schema.DataTypeStringArray: 72 return graphql.NewList(graphql.String) 73 case schema.DataTypeIntArray: 74 return graphql.NewList(graphql.Int) 75 case schema.DataTypeNumberArray: 76 return graphql.NewList(graphql.Float) 77 case schema.DataTypeBooleanArray: 78 return graphql.NewList(graphql.Boolean) 79 case schema.DataTypeDateArray: 80 return graphql.NewList(graphql.String) 81 case schema.DataTypeUUIDArray: 82 return graphql.NewList(graphql.String) 83 default: 84 panic(fmt.Sprintf("determinNestedPropertyType: unknown primitive type for property %s: %s", 85 propName, dataType[0])) 86 } 87 }