protected static Expression VisitValueAccessors(IGraphQLType parent, LambdaExpression expression)
        {
            var visitor = new SchemaExpressionVisitor(parent);
            var result  = Expression.Lambda(expression.Body, expression.Parameters[0], visitor.DataReferenceParameter);

            return(visitor.Visit(result));
        }
Exemple #2
0
        internal bool Resolve(GraphQLDocument doc, IGraphQLFieldCollection rootType)
        {
            if (this.op.Name.Value == "__typename")
            {
                // special case here
                this.Type = Field.TypeName(doc);
            }
            else
            {
                // this is special we need to treat it as such
                this.Type = rootType.Fields.SingleOrDefault(x => x.Name == this.op.Name.Value);
                if (this.Type == null)
                {
                    doc.AddError(ErrorCodes.UnknownField, $"The field '{this.op.Name.Value}' in not a valid member of '{rootType.Name}'", this.op);
                    return(false);
                }
            }
            if (this.Selection != null)
            {
                IGraphQLType root = this.Type.Type.Type as IGraphQLType;

                var specifiedTypeName = doc.ResolveSpecifiedTypeName(this.op);
                this.Selection.Resolve(doc, root, specifiedTypeName);
            }

            //if (selection != null)
            //{
            //    // if we have subselction then it must be an object type really, unless an interface will work instead ???
            //    selection.Resolve(FieldType.Type.Type as IGraphQLFieldCollection);
            //}
            return(true);
        }
Exemple #3
0
 private IEnumerable <InterfaceType> ResolveInterfaces(GraphQLDocument doc)
 {
     foreach (NamedTypeNode interfaceDefinitionLookup in this.definition.Interfaces)
     {
         IGraphQLType actualinterface = doc.ResolveType(interfaceDefinitionLookup);
         yield return(actualinterface as InterfaceType);
     }
 }
        private static IGraphQLType Unwrap(IGraphQLType gqlType)
        {
            var result = gqlType.Unwrap();

            for (var previous = gqlType; result != previous; result = result.Unwrap())
            {
                previous = result;
            }

            return(result);
        }
Exemple #5
0
        private string GenerateType(IGraphQLType type)
        {
            if (this.inputTypeLookup.ContainsKey(type))
            {
                return(this.inputTypeLookup[type]);
            }

            //lets make sure it exists
            string name = FindBestName(type.Name, "");

            if (type is IGraphQLFieldCollection obj)
            {
                TypeViewModel typeVm = new TypeViewModel(name)
                {
                    Fields = obj.Fields.Select(x =>
                                               new NamedTypeViewModel()
                    {
                        Name = x.Name,
                        Type = GenerateTypeReference(x.Type)
                    }).ToList()
                };

                this.inputTypeLookup.Add(type, typeVm.Name);
                this.typeCollection.Add(typeVm);

                return(typeVm.Name);
            }
            else if (type is EnumType enumObj)
            {
                EnumViewModel typeVm = new EnumViewModel(name)
                {
                    Values = enumObj.Values
                };
                this.inputTypeLookup.Add(type, typeVm.Name);
                this.enumCollection.Add(typeVm);
                return(typeVm.Name);
            }
            else if (type is FragmentType fragment)
            {
                TypeViewModel typeVm = BuildTypeViewModel(fragment.Selection, name, $"_{fragment.Selection}_[fragment]");
                typeVm.IsInterface = true;

                fragmentQueries.Add(typeVm.Name, fragment.Query);
                this.inputTypeLookup.Add(type, typeVm.Name);

                return(typeVm.Name);
            }
            else
            {
                throw new Exception("unkown type");
                // probably an enum type
            }
        }
Exemple #6
0
        internal void Resolve(GraphQLDocument doc, IGraphQLType rootType)
        {
            this.RootType = rootType;
            foreach (var f in Fields)
            {
                f.Resolve(doc, rootType as IGraphQLFieldCollection);
            }

            foreach (var f in Fragments)
            {
                var root = doc.ResolveType(f.TypeCondition);
                f.Resolve(doc, root);
            }

            UniqueIdentifier = $"{this.TypeCondition?.Name?.Value}_{rootType?.Name}_{string.Join("|", Fields.Select(x => x.UniqueIdentifier))}_{string.Join("|", Fragments.Select(x => x.UniqueIdentifier))}";
        }
        private void SerializeType(IGraphQLType type)
        {
            if (type.Parameters.Count > 0)
            {
                _builder.Append("(");
                _builder.Append(string.Join(",", type.Parameters.Select(x => x.Name + ":" + x.Value).ToList()));
                _builder.Append(")");
            }

            AddScope();

            foreach (var o in type.Types)
            {
                SerializeObject(o);
            }

            RemoveScope();
        }
Exemple #8
0
        private string GenerateType(IGraphQLType type)
        {
            if (inputTypeLookup.ContainsKey(type))
            {
                return(inputTypeLookup[type]);
            }

            //lets make sure it exists
            var name = FindBestName(type.Name, "");

            if (type is IGraphQLFieldCollection obj)
            {
                var typeVm = new TypeViewModel(name)
                {
                    Fields = obj.Fields.Select(x =>
                                               new NamedTypeViewModel()
                    {
                        Name = x.Name,
                        Type = GenerateTypeReference(x.Type)
                    }).ToList()
                };

                inputTypeLookup.Add(type, typeVm.Name);
                typeCollection.Add(typeVm);

                return(typeVm.Name);
            }
            else if (type is EnumType enumObj)
            {
                var typeVm = new EnumViewModel(name)
                {
                    Values = enumObj.Values
                };
                inputTypeLookup.Add(type, typeVm.Name);
                enumCollection.Add(typeVm);
                return(typeVm.Name);
            }
            else
            {
                throw new Exception("unkown type");
                // probably an enum type
            }
        }
        internal IGraphQLType ResolveType(string typeName)
        {
            IGraphQLType result = this.types.SingleOrDefault(x => x.Name == typeName);

            if (result == null)
            {
                WellknownScalarType wellknownType;
                if (!Enum.TryParse(typeName, out wellknownType))
                {
                    wellknownType = WellknownScalarType.OTHER;
                }
                result = new ScalarType()
                {
                    Name          = typeName,
                    WellknownType = wellknownType
                };
                this.types.Add(result);
            }

            return(result);
        }
        internal void Resolve(GraphQLDocument doc, IGraphQLType rootType, IValueNode <string> specifiedTypeName = null)
        {
            this.RootType = rootType;

            foreach (FieldSelection f in this.Fields)
            {
                f.Resolve(doc, rootType as IGraphQLFieldCollection);
            }

            foreach (SetSelection f in this.inlineFragments)
            {
                IGraphQLType root = doc.ResolveType(f.TypeCondition);
                f.Resolve(doc, root);
                this.fragments.Add(f);
            }

            // do these after the inline fragments as they have been pre-resolved
            foreach (NameNode name in this.fragmentNames)
            {
                var fragmentRoot = doc.ResolveType(name.Value) as FragmentType;
                this.fragmentsItems.Add(fragmentRoot);
                this.fragments.Add(fragmentRoot.Selection);
            }

            // merge fields from fragments into root object
            foreach (SetSelection f in this.fragments)
            {
                var currentFieldRefs = fields.Select(x => x.UniqueIdentifier);
                var newFields        = f.Fields.Where(x => !currentFieldRefs.Contains(x.UniqueIdentifier));
                fields.AddRange(newFields);
            }

            this.SpecifiedTypeName = specifiedTypeName;

            this.UniqueIdentifier = $"{this.TypeCondition?.Name?.Value}_{rootType?.Name}_{string.Join("|", this.Fields.Select(x => x.UniqueIdentifier))}_{string.Join("|", this.fragments.Select(x => x.UniqueIdentifier))}";
        }
 public GraphQLQueryParam(string key, IGraphQLType type)
 {
     Key  = key;
     Type = type;
 }
Exemple #12
0
 public ITypeBuilder WithType(IGraphQLType type)
 {
     _types.Add(type);
     return(this);
 }
Exemple #13
0
        public static string GetTypeName(this IGraphQLType graphQLType)
        {
            Check.IsNotNull(graphQLType, nameof(graphQLType));

            return(graphQLType.TypeAdapter.GetTypeDescription(graphQLType.GetType()).ToString());
        }
        protected SchemaExpressionVisitor(IGraphQLType parent)
        {
            this.parent = parent;

            DataReferenceParameter = Expression.Parameter(typeof(DataReference));
        }
Exemple #15
0
 public GraphQLMiddleware(RequestDelegate next, IGraphQLType graphQLType, IServiceProvider serviceProvider)
 {
     this._next            = next;
     this._graphQLType     = graphQLType;
     this._serviceProvider = serviceProvider;
 }