Exemplo n.º 1
0
 public static ScalarTypeDef GetScalarTypeDef(this GraphQLApiModel model, string name)
 {
     if (model.TypesByName.TryGetValue(name, out var typeDef))
     {
         return((ScalarTypeDef)typeDef);
     }
     return(null);
 }
Exemplo n.º 2
0
 public static TypeDefBase GetTypeDef(this GraphQLApiModel model, Type type)
 {
     if (model.TypesByClrType.TryGetValue(type, out var typeDef))
     {
         return(typeDef);
     }
     return(null);
 }
Exemplo n.º 3
0
 public static TypeDefBase LookupTypeDef(this GraphQLApiModel model, string name)
 {
     if (model.TypesByName.TryGetValue(name, out var typeDef))
     {
         return(typeDef);
     }
     return(null);
 }
Exemplo n.º 4
0
 public static TypeDefBase GetMappedGraphQLType(this GraphQLApiModel model, Type entityType)
 {
     if (model.TypesByEntityType.TryGetValue(entityType, out var typeDef))
     {
         return(typeDef);
     }
     return(null);
 }
Exemplo n.º 5
0
        public static IList <T> GetTypeDefs <T>(this GraphQLApiModel model, TypeKind kind,
                                                bool excludeHidden = false) where T : TypeDefBase
        {
            var temp = model.Types.Where(td => td.Kind == kind)
                       .Where(td => !excludeHidden || !td.Hidden)
                       .ToList();

            return(temp.OfType <T>().ToList());
        }
Exemplo n.º 6
0
        public static TypeDefBase LookupTypeDef(this GraphQLApiModel model, Type clrType)
        {
            var baseType = clrType;

            if (model.TypesByClrType.TryGetValue(baseType, out var typeDef))
            {
                return(typeDef);
            }
            return(null);
        }
Exemplo n.º 7
0
        public static ObjectTypeDef GetOperationDef(this GraphQLApiModel model, OperationType opType)
        {
            switch (opType)
            {
            case OperationType.Query: return(model.QueryType);

            case OperationType.Mutation: return(model.MutationType);

            default:
            case OperationType.Subscription: return(model.SubscriptionType);
            }
        }
Exemplo n.º 8
0
 public static void ForEachModelObject(this GraphQLApiModel model, Action <GraphQLModelObject> action)
 {
     foreach (var typeDef in model.Types)
     {
         if (typeDef.IsSchemaType()) // skip Module-Level transient types (ModuleQuery, ModuleMutation etc)
         {
             typeDef.ApplyDeep(action);
         }
     }
     foreach (var dirDef in model.Directives.Values)
     {
         dirDef.ApplyDeep(action);
     }
 } //method
Exemplo n.º 9
0
 public static ScalarTypeDef GetScalarTypeDef(this GraphQLApiModel model, Type type)
 {
     return((ScalarTypeDef)model.GetTypeDef(type));
 }
Exemplo n.º 10
0
        public string GenerateSchema(GraphQLApiModel model)
        {
            _model   = model;
            _builder = new StringBuilder();

            // custom scalar types
            var scalarTypes = SelectTypes <ScalarTypeDef>(TypeKind.Scalar)
                              .Where(td => td.Scalar.IsCustom).ToList();

            foreach (var sd in scalarTypes)
            {
                AppendDescr(sd.Description);
                _builder.AppendLine("scalar " + sd.Name);
            }
            _builder.AppendLine();

            // enums
            var enumDefs = SelectTypes <EnumTypeDef>(TypeKind.Enum);

            foreach (var enumDef in enumDefs)
            {
                AppendDescr(enumDef.Description);
                _builder.AppendLine("enum " + enumDef.Name + " {");
                foreach (var m in enumDef.EnumValues)
                {
                    AppendDescr(m.Description, true);
                    _builder.AppendLine(Indent + m.Name);
                }
                _builder.AppendLine("}");
                _builder.AppendLine();
            }

            // interfaces
            var intfTypes = SelectTypes <InterfaceTypeDef>(TypeKind.Interface);

            foreach (var tDef in intfTypes)
            {
                AppendDescr(tDef.Description);
                _builder.AppendLine("interface " + tDef.Name + " {");
                foreach (var fld in tDef.Fields)
                {
                    if (fld.Flags.IsSet(FieldFlags.Hidden))
                    {
                        continue;
                    }
                    Append(fld);
                    _builder.AppendLine();
                }
                _builder.AppendLine("}");
                _builder.AppendLine();
            }

            // Input types
            var inpTypes = SelectTypes <InputObjectTypeDef>(TypeKind.InputObject);

            foreach (var tDef in inpTypes)
            {
                AppendDescr(tDef.Description);
                _builder.Append("input " + tDef.Name);
                _builder.AppendLine(" {");
                foreach (var fldDef in tDef.Fields)
                {
                    Append(fldDef, indent: true);
                    _builder.AppendLine();
                }
                _builder.AppendLine("}");
                _builder.AppendLine();
            }

            // Unions
            var unionTypes = SelectTypes <UnionTypeDef>(TypeKind.Union);

            foreach (var tDef in unionTypes)
            {
                var typeNames = string.Join(" | ", tDef.PossibleTypes.Select(t => t.Name));
                AppendDescr(tDef.Description);
                _builder.AppendLine($"union {tDef.Name} = {typeNames}");
                _builder.AppendLine();
            }

            // types
            var objTypes = SelectTypes <ObjectTypeDef>(TypeKind.Object).Where(td => td.IsSchemaType()).ToList();

            foreach (var tDef in objTypes)
            {
                AppendDescr(tDef.Description);
                _builder.Append("type " + tDef.Name);
                if (tDef.Implements.Count > 0)
                {
                    _builder.Append(" implements ");
                    var intfList = string.Join(" & ", tDef.Implements.Select(iDef => iDef.Name));
                    _builder.Append(intfList);
                }
                AppendDirs(tDef.Directives);
                _builder.AppendLine(" {");
                foreach (var fld in tDef.Fields)
                {
                    if (fld.Flags.IsSet(FieldFlags.Hidden))
                    {
                        continue;
                    }
                    Append(fld);
                    _builder.AppendLine();
                }
                _builder.AppendLine("}");
                _builder.AppendLine();
            }

            return(_builder.ToString());
        }