Exemple #1
0
        protected virtual IObjectGraphType ToObjectGraphType(GraphQLObjectTypeDefinition astType)
        {
            var typeConfig = Types.For(astType.Name.Value);

            var type = new ObjectGraphType();

            type.Name        = astType.Name.Value;
            type.Description = typeConfig.Description;
            type.IsTypeOf    = typeConfig.IsTypeOfFunc;

            ApplyDeprecatedDirective(astType.Directives, reason =>
            {
                type.DeprecationReason = typeConfig.DeprecationReason ?? reason;
            });

            CopyMetadata(type, typeConfig);

            var fields = astType.Fields.Select(f => ToFieldType(type.Name, f));

            fields.Apply(f =>
            {
                type.AddField(f);
            });

            var interfaces = astType
                             .Interfaces
                             .Select(i => new GraphQLTypeReference(i.Name.Value))
                             .ToList();

            interfaces.Apply(type.AddResolvedInterface);

            return(type);
        }
        protected virtual IObjectGraphType ToObjectGraphType(GraphQLObjectTypeDefinition astType, bool isExtensionType = false)
        {
            var typeConfig = Types.For(astType.Name.Value);

            ObjectGraphType type;

            if (!_types.ContainsKey(astType.Name.Value))
            {
                type = new ObjectGraphType {
                    Name = astType.Name.Value
                };
            }
            else
            {
                type = _types[astType.Name.Value] as ObjectGraphType;
            }

            if (!isExtensionType)
            {
                type.Description = typeConfig.Description ?? astType.Comment?.Text;
                type.IsTypeOf    = typeConfig.IsTypeOfFunc;
            }

            CopyMetadata(type, typeConfig);

            Func <string, GraphQLFieldDefinition, FieldType> constructFieldType;

            if (type.Name == "Subscription")
            {
                constructFieldType = ToSubscriptionFieldType;
            }
            else
            {
                constructFieldType = ToFieldType;
            }

            var fields = astType.Fields.Select(f => constructFieldType(type.Name, f));

            fields.Apply(f => type.AddField(f));

            if (astType.Interfaces != null)
            {
                astType.Interfaces
                .Select(i => new GraphQLTypeReference(i.Name.Value))
                .Apply(type.AddResolvedInterface);
            }

            if (isExtensionType)
            {
                type.AddExtensionAstType(astType);
            }
            else
            {
                type.SetAstType(astType);
            }

            VisitNode(type, v => v.VisitObject(type));

            return(type);
        }
Exemple #3
0
        private string PickFieldName(GraphQLObjectTypeDefinition objectType, GraphQLFieldDefinition field, IEnumerable <ASTNode> allDefinitions)
        {
            var implementedInterfaceDefinitions = allDefinitions
                                                  .Where(def => def.Kind == ASTNodeKind.InterfaceTypeDefinition)
                                                  .Cast <GraphQLInterfaceTypeDefinition>()
                                                  .Where(def => objectType?.Interfaces != null && objectType.Interfaces.Any(e => e.Name.Value == def.Name.Value));

            var name = Utils.ToPascalCase(field.Name.Value);

            if (objectType.Name.Value == name)
            {
                return($"{name}Field");
            }

            foreach (var interfaceDefinition in implementedInterfaceDefinitions)
            {
                var collidingObjectTypeDefinitions = allDefinitions
                                                     .Where(def => def.Kind == ASTNodeKind.ObjectTypeDefinition)
                                                     .Cast <GraphQLObjectTypeDefinition>()
                                                     .Where(def => def.Name.Value == name &&
                                                            def.Interfaces != null &&
                                                            def.Interfaces.Any(i => i.Name.Value == interfaceDefinition.Name.Value));

                if (collidingObjectTypeDefinitions.Any())
                {
                    return($"{name}Field");
                }
            }

            return(name);
        }
Exemple #4
0
        private ClassDeclarationSyntax GenerateMethod(
            GraphQLObjectTypeDefinition objectType,
            ClassDeclarationSyntax classDeclaration,
            GraphQLFieldDefinition field,
            IEnumerable <ASTNode> allDefinitions)
        {
            var returnType = this.GetCSharpTypeFromGraphQLType(field.Type, allDefinitions);
            var methodName = PickFieldName(objectType, field, allDefinitions);

            var nonNullArguments  = field.Arguments.Where(arg => arg.Type.Kind == ASTNodeKind.NonNullType);
            var nullableArguments = field.Arguments.Where(arg => arg.Type.Kind != ASTNodeKind.NonNullType);

            var argumentList = nonNullArguments.ToList();

            classDeclaration = CreateMethod(
                classDeclaration, field, allDefinitions, returnType, methodName, argumentList);

            foreach (var argument in nullableArguments)
            {
                argumentList.Add(argument);
                classDeclaration = CreateMethod(
                    classDeclaration, field, allDefinitions, returnType, methodName, argumentList);
            }

            return(classDeclaration);
        }
 public static bool AstTypeHasFields(this IProvideMetadata type)
 {
     return(GetAstType <ASTNode>(type) switch
     {
         GraphQLObjectTypeDefinition otd => otd.Fields?.Any() ?? false,
         GraphQLInterfaceTypeDefinition itd => itd.Fields?.Any() ?? false,
         _ => false
     });
        protected virtual IObjectGraphType ToObjectGraphType(GraphQLObjectTypeDefinition astType, bool isExtensionType = false)
        {
            var typeConfig = Types.For(astType.Name.Value);

            ObjectGraphType type;

            if (!_types.ContainsKey(astType.Name.Value))
            {
                type = new ObjectGraphType {
                    Name = astType.Name.Value
                };
            }
            else
            {
                type = _types[astType.Name.Value] as ObjectGraphType;
            }

            if (!isExtensionType)
            {
                type.Description = typeConfig.Description;
                type.IsTypeOf    = typeConfig.IsTypeOfFunc;

                ApplyDeprecatedDirective(astType.Directives, reason =>
                {
                    type.DeprecationReason = typeConfig.DeprecationReason ?? reason;
                });
            }

            CopyMetadata(type, typeConfig);

            Func <string, GraphQLFieldDefinition, FieldType> constructFieldType;

            if (type.Name == "Subscription")
            {
                constructFieldType = ToSubscriptionFieldType;
            }
            else
            {
                constructFieldType = ToFieldType;
            }

            var fields = astType.Fields.Select(f => constructFieldType(type.Name, f));

            fields.Apply(f =>
            {
                type.AddField(f);
            });

            var interfaces = astType
                             .Interfaces
                             .Select(i => new GraphQLTypeReference(i.Name.Value))
                             .ToList();

            interfaces.Apply(type.AddResolvedInterface);

            return(type);
        }
        protected virtual IObjectGraphType ToObjectGraphType(GraphQLObjectTypeDefinition astType, bool isExtensionType = false)
        {
            var name       = (string)astType.Name !.Value;
            var typeConfig = Types.For(name);

            AssertKnownType(typeConfig);

            var type = _types.TryGetValue(name, out var t)
                ? t as ObjectGraphType ?? throw new InvalidOperationException($"Type '{name} should be ObjectGraphType")
                : new ObjectGraphType {
                                 Name = name
                             };

            if (!isExtensionType)
            {
                type.Description = typeConfig.Description ?? astType.Description?.Value.ToString() ?? astType.Comment?.Text.ToString();
                type.IsTypeOf    = typeConfig.IsTypeOfFunc;
            }

            typeConfig.CopyMetadataTo(type);

            Func <string, GraphQLFieldDefinition, FieldType> constructFieldType = IsSubscriptionType(type)
                ? ToSubscriptionFieldType
                : ToFieldType;

            if (astType.Fields != null)
            {
                foreach (var f in astType.Fields)
                {
                    type.AddField(constructFieldType(type.Name, f));
                }
            }

            if (astType.Interfaces != null)
            {
                foreach (var i in astType.Interfaces)
                {
                    type.AddResolvedInterface(new GraphQLTypeReference((string)i.Name !.Value));
                }
            }

            if (isExtensionType)
            {
                type.AddExtensionAstType(astType);
            }
            else
            {
                type.SetAstType(astType);
                OverrideDeprecationReason(type, typeConfig.DeprecationReason);
            }

            return(type);
        }
        public virtual GraphQLObjectTypeDefinition BeginVisitObjectTypeDefinition(GraphQLObjectTypeDefinition node)
        {
            if (node.Directives != null)
            {
                this.BeginVisitNodeCollection(node.Directives);
            }

            if (node.Fields != null)
            {
                this.BeginVisitNodeCollection(node.Fields);
            }

            return(node);
        }
Exemple #9
0
        private string PrintObjectTypeDefinition(GraphQLObjectTypeDefinition node)
        {
            var name       = PrintName(node.Name);
            var interfaces = node.Interfaces?.Select(PrintNamedType);
            var directives = node.Directives?.Select(PrintDirective);
            var fields     = node.Fields?.Select(PrintFieldDefinition);

            return(Join(new[]
            {
                "type",
                name,
                Wrap("implements ", Join(interfaces, " & ")),
                Join(directives, " "),
                Block(fields) ?? "{ }"
            },
                        " "));
        }
Exemple #10
0
        private ClassDeclarationSyntax CreateProperties(
            GraphQLObjectTypeDefinition objectType,
            ClassDeclarationSyntax classDeclaration,
            IEnumerable <GraphQLFieldDefinition> fields,
            IEnumerable <ASTNode> allDefinitions)
        {
            foreach (var field in fields)
            {
                if (field.Arguments == null || field.Arguments.Count() == 0)
                {
                    classDeclaration = GenerateProperty(objectType, classDeclaration, field, allDefinitions);
                }
                else
                {
                    classDeclaration = GenerateMethod(objectType, classDeclaration, field, allDefinitions);
                }
            }

            return(classDeclaration);
        }
Exemple #11
0
        private ClassDeclarationSyntax GenerateProperty(
            GraphQLObjectTypeDefinition objectType,
            ClassDeclarationSyntax classDeclaration,
            GraphQLFieldDefinition field,
            IEnumerable <ASTNode> allDefinitions)
        {
            var member = SyntaxFactory.PropertyDeclaration(
                this.GetCSharpTypeFromGraphQLType(field.Type, allDefinitions),
                PickFieldName(objectType, field, allDefinitions))
                         .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
                         .AddModifiers(SyntaxFactory.Token(SyntaxKind.VirtualKeyword))
                         .AddAttributeLists(GetFieldAttributes(field))
                         .AddAccessorListAccessors(
                SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration)
                .WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)),
                SyntaxFactory.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration)
                .WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)));

            return(classDeclaration.AddMembers(member));
        }
Exemple #12
0
 public ObjectType(GraphQLObjectTypeDefinition definition)
 {
     this.definition = definition;
     Name            = definition.Name?.Value;
 }
        protected virtual IObjectGraphType ToObjectGraphType(GraphQLObjectTypeDefinition astType, bool isExtensionType = false)
        {
            var name       = (string)astType.Name.Value;
            var typeConfig = Types.For(name);

            ObjectGraphType type;

            if (!_types.ContainsKey(name))
            {
                type = new ObjectGraphType {
                    Name = name
                };
            }
            else
            {
                type = _types[name] as ObjectGraphType ?? throw new InvalidOperationException($"Type '{name} should be ObjectGraphType");
            }

            if (!isExtensionType)
            {
                type.Description = typeConfig.Description ?? astType.Comment?.Text.ToString();
                type.IsTypeOf    = typeConfig.IsTypeOfFunc;
            }

            typeConfig.CopyMetadataTo(type);

            Func <string, GraphQLFieldDefinition, FieldType> constructFieldType;

            if (IsSubscriptionType(type))
            {
                constructFieldType = ToSubscriptionFieldType;
            }
            else
            {
                constructFieldType = ToFieldType;
            }

            if (astType.Fields != null)
            {
                foreach (var f in astType.Fields)
                {
                    type.AddField(constructFieldType(type.Name, f));
                }
            }

            if (astType.Interfaces != null)
            {
                foreach (var i in astType.Interfaces)
                {
                    type.AddResolvedInterface(new GraphQLTypeReference((string)i.Name.Value));
                }
            }

            if (isExtensionType)
            {
                type.AddExtensionAstType(astType);
            }
            else
            {
                type.SetAstType(astType);
            }

            return(type);
        }