Exemplo n.º 1
0
        public MethodInfo FindMethod(GraphQLType gqlType, QueryType requestType)
        {
            Dictionary <string, MethodInfo> loopThrough;

            if (requestType == QueryType.MUTATION)
            {
                loopThrough = Mutations;
            }
            else if (requestType == QueryType.QUERY)
            {
                loopThrough = Queries;
            }
            else if (requestType == QueryType.FIELD)
            {
                loopThrough = FieldResolvers;
            }
            else
            {
                return(null);
            }

            foreach (var q in loopThrough)
            {
                if (GraphQLType.FindOrGenerate(q.Value.ReturnType).TypeName == gqlType.TypeName)
                {
                    return(q.Value);
                }
            }
            return(null);
        }
Exemplo n.º 2
0
        private GraphQLType ParseType()
        {
            GraphQLType type  = null;
            var         start = this.currentToken.Start;

            if (this.Skip(TokenKind.BRACKET_L))
            {
                type = this.ParseType();
                this.Expect(TokenKind.BRACKET_R);
                type = new GraphQLListType()
                {
                    Type     = type,
                    Location = this.GetLocation(start)
                };
            }
            else
            {
                type = this.ParseNamedType();
            }

            if (this.Skip(TokenKind.BANG))
            {
                return(new GraphQLNonNullType()
                {
                    Type = type,
                    Location = this.GetLocation(start)
                });
            }

            return(type);
        }
Exemplo n.º 3
0
        private IGraphType ToGraphType(GraphQLType astType)
        {
            switch (astType.Kind)
            {
            case ASTNodeKind.NonNullType:
            {
                var type = ToGraphType(((GraphQLNonNullType)astType).Type);
                return(new NonNullGraphType(type));
            }

            case ASTNodeKind.ListType:
            {
                var type = ToGraphType(((GraphQLListType)astType).Type);
                return(new ListGraphType(type));
            }

            case ASTNodeKind.NamedType:
            {
                var namedType = (GraphQLNamedType)astType;
                var type      = GetType(namedType.Name.Value);
                return(type ?? new GraphQLTypeReference(namedType.Name.Value));
            }

            default:
                throw new ArgumentOutOfRangeException($"Unknown GraphQL type {astType.Kind}");
            }
        }
Exemplo n.º 4
0
 public SchemaQueryType(Schema schema, GraphQLType baseQueryType)
 {
     Fields = baseQueryType.Fields
              .Where(f => !f.IsMutation)
              .Select(f => new SchemaField(this, f, schema))
              .ToDictionary(f => f.FieldName, f => f as ISchemaField <Info>);
 }
        public IType Type(GraphQLType type)
        {
            switch (type.Kind)
            {
            case ASTNodeKind.NamedType:
            {
                var name = (GraphQLNamedType)type;
                return(new NamedType(Name(name.Name)).WithLocation(name, _body));
            }

            case ASTNodeKind.NonNullType:
            {
                var nonNull = (GraphQLNonNullType)type;
                return(new NonNullType(Type(nonNull.Type)).WithLocation(nonNull, _body));
            }

            case ASTNodeKind.ListType:
            {
                var list = (GraphQLListType)type;
                return(new ListType(Type(list.Type)).WithLocation(list, _body));
            }
            }

            throw new ExecutionError($"Unmapped type {type.Kind}");
        }
Exemplo n.º 6
0
 private void UnPackType(GraphQLType type, ValueTypeReference target)
 {
     try
     {
         if (type is GraphQLNonNullType nonNullType)
         {
             target.CanValueBeNull = true;
             UnPackType(nonNullType.Type, target);
         }
         else if (type is GraphQLListType listType)
         {
             target.IsCollection = true;
             if (target.CanValueBeNull)
             {
                 target.CanValueBeNull      = false;
                 target.CanCollectionBeNull = true;
             }
             UnPackType(listType.Type, target);
         }
         else if (type is GraphQLNamedType namedType)
         {
             target.Type = ResolveType(namedType);
         }
         else
         {
             throw new Exception("dunno???");
         }
     }catch (Exception ex)
     {
         throw;
     }
 }
Exemplo n.º 7
0
        protected virtual GraphQLBaseType GetInputType(GraphQLType type)
        {
            if (type is GraphQLNamedType)
            {
                return(this.GetInputTypeFromNamedType((GraphQLNamedType)type));
            }

            if (type is GraphQLNonNullType)
            {
                var inputType = this.GetInputType(((GraphQLNonNullType)type).Type);

                if (inputType == null)
                {
                    return(null);
                }

                return(new GraphQLNonNull(inputType));
            }

            if (type is GraphQLListType)
            {
                return(new GraphQLList(this.GetInputType(((GraphQLListType)type).Type)));
            }

            throw new NotImplementedException();
        }
Exemplo n.º 8
0
        public static IType TypeFromAst(ISchema schema, GraphQLType type)
        {
            if (type == null)
            {
                return(null);
            }

            if (type.Kind == ASTNodeKind.NonNullType)
            {
                var innerType = TypeFromAst(schema, ((GraphQLNonNullType)type).Type);
                return(innerType != null ? new NonNull(innerType) : null);
            }

            if (type.Kind == ASTNodeKind.ListType)
            {
                var innerType = TypeFromAst(schema, ((GraphQLListType)type).Type);
                return(innerType != null ? new List(innerType) : null);
            }

            if (type.Kind == ASTNodeKind.NamedType)
            {
                var namedType = (GraphQLNamedType)type;
                return(schema.GetNamedType(namedType.Name.Value));
            }

            throw new GraphQLError(
                      $"Unexpected type kind: {type.Kind}");
        }
Exemplo n.º 9
0
        internal ValueTypeReference ResolveValueType(GraphQLType type)
        {
            var result = new ValueTypeReference();

            UnPackType(type, result);

            return(result);
        }
Exemplo n.º 10
0
        public SchemaType OfType(GraphQLType type)
        {
            SchemaType existing;

            if (_typeMap.TryGetValue(type, out existing))
            {
                return(existing);
            }
            return(_typeMap[type] = new SchemaType(type, this));
        }
Exemplo n.º 11
0
        protected TypeSyntax GetCSharpTypeFromGraphQLType(GraphQLType type, IEnumerable <ASTNode> allDefinitions)
        {
            switch (type.Kind)
            {
            case ASTNodeKind.NamedType: return(this.GetCSharpTypeFromGraphQLNamedType((GraphQLNamedType)type, allDefinitions));

            case ASTNodeKind.NonNullType: return(this.GetCSharpTypeFromGraphQLNonNullType((GraphQLNonNullType)type, allDefinitions));

            case ASTNodeKind.ListType: return(this.GetCSharpTypeFromGraphQLListType((GraphQLListType)type, allDefinitions));
            }

            throw new NotImplementedException($"GetCSharpTypeFromGraphQLType: Unknown type.Kind: {type.Kind}");
        }
Exemplo n.º 12
0
 internal SchemaType(GraphQLType type, Schema schema)
 {
     _type   = type;
     _fields = new Lazy <IReadOnlyDictionary <string, ISchemaField <Info> > >(() => type.GetAllFieldIncludeBaseType()
                                                                              .Select(f => new SchemaField(this, f, schema))
                                                                              // There might be duplicates (i.e. '__typename' on types with a base type) - ignore them.
                                                                              .Aggregate(
                                                                                  new Dictionary <string, ISchemaField <Info> >(),
                                                                                  (dict, field) =>
     {
         dict[field.FieldName] = field;
         return(dict);
     }
                                                                                  ));
 }
Exemplo n.º 13
0
        private GraphQLScalarType GetTypeDefinition(GraphQLType typeDefinition)
        {
            if (typeDefinition is GraphQLNamedType)
            {
                return(this.typeTranslator.GetType((GraphQLNamedType)typeDefinition));
            }

            if (typeDefinition is Language.AST.GraphQLNonNullType)
            {
                return(new Type.GraphQLNonNullType((GraphQLNullableType)this.GetTypeDefinition(((Language.AST.GraphQLNonNullType)typeDefinition).Type)));
            }

            if (typeDefinition is GraphQLListType)
            {
                return(new GraphQLList(this.GetTypeDefinition(((GraphQLListType)typeDefinition).Type)));
            }

            return(null);
        }
Exemplo n.º 14
0
        public GraphQLServer(int port = 8080)
        {
            _httpListener = new HttpListener();
            // Allow the server to listen on any address, with https and http, on a port
            _httpListener.Prefixes.Add($"http://+:{port}/");

            // Set up request handle thread
            _handleThread = new Thread(HandleRequests);

            // Set up types
            try
            {
                GraphQLType.RegisterIntrinsic();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
Exemplo n.º 15
0
        protected virtual GraphQLBaseType GetOutputType(GraphQLType type)
        {
            if (type is GraphQLNamedType)
            {
                return(this.GetOutputTypeFromNamedType((GraphQLNamedType)type));
            }

            if (type is GraphQLNonNullType)
            {
                return(this.GetOutputType(((GraphQLNonNullType)type).Type));
            }

            if (type is GraphQLListType)
            {
                return(this.GetOutputType(((GraphQLListType)type).Type));
            }

            throw new NotImplementedException();
        }
Exemplo n.º 16
0
        private GraphQLBaseType GetTypeDefinition(GraphQLType typeDefinition)
        {
            if (typeDefinition is GraphQLNamedType)
            {
                return(this.schemaRepository.GetSchemaInputTypeByName(((GraphQLNamedType)typeDefinition).Name.Value));
            }

            if (typeDefinition is GraphQLNonNullType)
            {
                return(new GraphQLNonNull(this.GetTypeDefinition(((GraphQLNonNullType)typeDefinition).Type)));
            }

            if (typeDefinition is GraphQLListType)
            {
                return(new GraphQLList(this.GetTypeDefinition(((GraphQLListType)typeDefinition).Type)));
            }

            return(null);
        }
Exemplo n.º 17
0
        public static bool IsArrayType(this GraphQLType graphqlType)
        {
            var isArray = false;

            switch (graphqlType.Kind)
            {
            case ASTNodeKind.NonNullType:
            {
                isArray = IsArrayType(((GraphQLNonNullType)graphqlType).Type !);
                break;
            }

            case ASTNodeKind.ListType:
            {
                isArray = true;
                break;
            }
            }

            return(isArray);
        }
Exemplo n.º 18
0
        internal SchemaType(GraphQLType type, Schema schema)
        {
            _type   = type;
            _fields = new Lazy <IReadOnlyDictionary <string, ISchemaField <Info> > >(() => type.GetQueryFields()
                                                                                     .Select(f => new SchemaField(this, f, schema))
                                                                                     // There might be duplicates (i.e. '__typename' on types with a base type) - ignore them.
                                                                                     .Aggregate(
                                                                                         new Dictionary <string, ISchemaField <Info> >(),
                                                                                         (dict, field) =>
            {
                dict[field.FieldName] = field;
                return(dict);
            }
                                                                                         ));

            _possibleTypes = new Lazy <IEnumerable <ISchemaQueryType <Info> > >(
                () => type.PossibleTypes.Select(schema.OfType));

            _interfaces = new Lazy <IEnumerable <ISchemaQueryType <Info> > >(
                () => type.Interfaces.Select(schema.OfType));
        }
Exemplo n.º 19
0
 public Info(GraphQLType type)
 {
     Type = type;
 }
Exemplo n.º 20
0
        public GraphQLController()
        {
            var controllerAttr = (GQLController)GetType().GetCustomAttribute(typeof(GQLController));

            gqlType = GraphQLType.FindOrGenerate(controllerAttr.type);
        }
Exemplo n.º 21
0
 public Info(GraphQLType type)
 {
     Type = type;
 }
Exemplo n.º 22
0
        private static List <RequestNode> GetFieldGroups(string query, GraphQLTypeIntrinsic.GQLInterface parentType,
                                                         JObject variables)
        {
            List <RequestNode> groups = new List <RequestNode>();

            while (query.Length > 0)
            {
                query = query.Trim(' ', '\n');
                if (query == "")
                {
                    break;
                }

                var(start, end) = NextBracketSection(query);

                // If non sub group fields are the only ones that remain
                if (end == -1)
                {
                    foreach (var fName in query.Split('\n'))
                    {
                        var(name, args) = ParseArgs(fName, variables);
                        GraphQLType t = null;
                        foreach (var f in parentType.Fields)
                        {
                            if (f.Name == name)
                            {
                                t = f.type;
                                break;
                            }
                        }

                        if (t != null)
                        {
                            RequestNode n = new RequestNode
                            {
                                QueryType = QueryType.FIELD, NodeType = t, Name = name, Args = args
                            };
                            groups.Add(n);
                        }
                    }

                    break;
                }

                var fullGroup = query.Substring(0, end);
                var names     = fullGroup.Split('{')[0];

                // Parse possible fields that are above a sub group
                var fields2 = names.Split('\n');
                for (int i = 0; i < fields2.Length - 1; i++)
                {
                    var(name, args) = ParseArgs(fields2[i], variables);
                    GraphQLType t = null;
                    foreach (var f in parentType.Fields)
                    {
                        if (f.Name == name)
                        {
                            t = f.type;
                            break;
                        }
                    }

                    if (t != null)
                    {
                        RequestNode n = new RequestNode
                        {
                            QueryType = QueryType.FIELD, NodeType = t, Name = name, Args = args
                        };
                        groups.Add(n);
                    }
                }

                // Parse the sub group
                if (fullGroup.Length > 0)
                {
                    var(name, args) = ParseArgs(fields2[fields2.Length - 1], variables);
                    GraphQLType t = null;
                    foreach (var f in parentType.Fields)
                    {
                        if (f.Name == name)
                        {
                            t = f.type;
                            break;
                        }
                    }

                    if (t != null)
                    {
                        List <RequestNode> fields = new List <RequestNode>();
                        if (t is GraphQLTypeIntrinsic.GQLInterface)
                        {
                            fields = GetFieldGroups(query.Substring(start, end - start),
                                                    (GraphQLTypeIntrinsic.GQLInterface)t, variables);
                        }

                        RequestNode n = new RequestNode
                        {
                            QueryType = QueryType.FIELD, NodeType = t, Name = name, Fields = fields,
                            Args      = args
                        };
                        groups.Add(n);
                    }
                }

                query = query.Substring(end);
            }

            return(groups);
        }
Exemplo n.º 23
0
 /// <summary>
 /// Searches a schema for a graph type specified by an AST type after unwrapping any
 /// <see cref="GraphQLNonNullType"/> or <see cref="GraphQLListType"/> layers. If the type cannot be
 /// found, returns <see langword="null"/>.
 /// </summary>
 /// <param name="type">The AST type to search for.</param>
 /// <param name="schema">The schema to search within.</param>
 public static IGraphType?NamedGraphTypeFromType(this GraphQLType type, ISchema schema) => type switch
 {
Exemplo n.º 24
0
 public IsType(Type type)
 {
     Type = (GraphQLType)Activator.CreateInstance(type);
 }
Exemplo n.º 25
0
 public SchemaRootType(Schema schema, GraphQLType baseQueryType)
 {
     Fields = baseQueryType.OwnFields
              .Select(f => new SchemaField(this, f, schema))
              .ToDictionary(f => f.FieldName, f => f as ISchemaField <Info>);
 }