Exemplo n.º 1
0
        public __Schema Build(GraphQLApiModel model)
        {
            _model         = model;
            _stringNotNull = _model.GetScalarTypeDef("String").TypeRefNotNull;
            _schema        = _model.Schema_ = new __Schema();

            // Create type objects without internal details; for typeDef and its typeRefs
            foreach (var typeDef in _model.Types)
            {
                if (!typeDef.IsSchemaType())
                {
                    continue;
                }
                CreateTypeObject(typeDef);
                foreach (var typeRef in typeDef.TypeRefs)
                {
                    SetupTypeObject(typeRef);
                }
            }

            // Build types internals - fields, etc
            BuildTypeObjectsInternals();
            BuildDirectives();
            CompleteSchemaObject();
            return(_schema);
        }
Exemplo n.º 2
0
        public void Apply(GraphQLApiModel model, GraphQLModelObject element, object[] argValues)
        {
            var intro = element.Intro_;

            if (intro == null)
            {
                return;
            }
            intro.IsDeprecated      = true;
            intro.DeprecationReason = (string)argValues[0];
        }
Exemplo n.º 3
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.Append("enum " + enumDef.Name);
                AppendDirs(enumDef);
                _builder.AppendLine(" {");
                foreach (var enumFld in enumDef.Fields)
                {
                    AppendDescr(enumFld.Description, true);
                    _builder.Append(Indent + enumFld.Name + " ");
                    AppendDirs(enumFld);
                    _builder.AppendLine();
                }
                _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);

            foreach (var tDef in objTypes)
            {
                if (tDef.IsModuleTransientType())
                {
                    continue; // skip module-level Query, Mutation transient types
                }
                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);
                _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());
        }
Exemplo n.º 4
0
 public ModelVisitor(GraphQLApiModel model)
 {
     _model = model;
 }
Exemplo n.º 5
0
        public void BuildModel()
        {
            _model            = _server.Model = new GraphQLApiModel(_server);
            _modelAdjustments = _server.Modules.SelectMany(m => m.Adjustments).ToList();

            // collect,register scalar, data types, query/mutation types, resolvers
            RegisterScalars();
            if (!RegisterGraphQLTypes())
            {
                return;
            }
            RegisterResolverClasses();

            if (!BuildRegisteredDirectiveDefinitions())
            {
                return;
            }

            if (!AssignMappedEntitiesForObjectTypes())
            {
                return;
            }

            BuildTypesInternalsFromClrType();
            if (_model.HasErrors)
            {
                return;
            }

            LinkImplementedInterfaces();
            BuildUnionTypes();
            if (_model.HasErrors)
            {
                return;
            }

            BuildSchemaDef();
            if (_model.HasErrors)
            {
                return;
            }

            var introSchemaBuilder = new IntrospectionSchemaBuilder();

            introSchemaBuilder.Build(_model);
            if (_model.HasErrors)
            {
                return;
            }

            // apply directives to all model objects in the model
            _model.ForEachModelObject(this.ApplyDirectives);
            if (_model.HasErrors)
            {
                return;
            }

            MapObjectFields();
            if (_model.HasErrors)
            {
                return;
            }

            var schemaGen = new SchemaDocGenerator();

            _model.SchemaDoc = schemaGen.GenerateSchema(_model);

            VerifyModel();
        }
Exemplo n.º 6
0
 public void ModelDirectiveApply(GraphQLApiModel model, GraphQLModelObject element, object[] argValues)
 {
 }
Exemplo n.º 7
0
 public RequestMapper(RequestContext requestContext)
 {
     _requestContext = requestContext;
     _model          = _requestContext.ApiModel;
 }