Exemplo n.º 1
0
        /// <summary>
        /// Execute a query using this schema.
        /// </summary>
        /// <param name="gql">The query</param>
        /// <param name="context">The context object. An instance of the context the schema was build from</param>
        /// <param name="serviceProvider">A service provider used for looking up dependencies of field selections and mutations</param>
        /// <param name="claims">Optional claims to check access for queries</param>
        /// <param name="methodProvider"></param>
        /// <param name="includeDebugInfo"></param>
        /// <typeparam name="TContextType"></typeparam>
        /// <returns></returns>
        public QueryResult ExecuteQuery(QueryRequest gql, TContextType context, IServiceProvider serviceProvider, ClaimsIdentity claims, IMethodProvider methodProvider = null)
        {
            if (methodProvider == null)
            {
                methodProvider = new DefaultMethodProvider();
            }

            QueryResult result;

            try
            {
                var graphQLCompiler = new GraphQLCompiler(this, methodProvider);
                var queryResult     = graphQLCompiler.Compile(gql, claims);
                result = queryResult.ExecuteQuery(context, serviceProvider, gql.OperationName);
            }
            catch (Exception ex)
            {
                // error with the whole query
                result = new QueryResult {
                    Errors = { new GraphQLError(ex.InnerException != null ? ex.InnerException.Message : ex.Message) }
                };
            }

            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Execute a query using this schema.
        /// </summary>
        /// <param name="gql">The query</param>
        /// <param name="context">The context object. An instance of the context the schema was build from</param>
        /// <param name="serviceProvider">A service provider used for looking up dependencies of field selections and mutations</param>
        /// <param name="claims">Optional claims to check access for queries</param>
        /// <param name="methodProvider"></param>
        /// <param name="includeDebugInfo"></param>
        /// <typeparam name="TContextType"></typeparam>
        /// <returns></returns>
        public async Task <QueryResult> ExecuteQueryAsync(QueryRequest gql, TContextType context, IServiceProvider serviceProvider, ClaimsIdentity claims, IMethodProvider methodProvider = null)
        {
            //== JT
            this.User = Security.Worker.CreateUser(claims);
            // Clear any previous errors before executing new query
            GraphQLValidation.Errors = new List <GraphQLError>();
            //==

            if (methodProvider == null)
            {
                methodProvider = new DefaultMethodProvider();
            }

            QueryResult result;

            try
            {
                var graphQLCompiler = new GraphQLCompiler(this, methodProvider);
                var queryResult     = graphQLCompiler.Compile(gql, claims);
                result = await queryResult.ExecuteQueryAsync(context, serviceProvider, gql.OperationName);
            }
            catch (Exception ex)
            {
                // error with the whole query
                result = new QueryResult {
                    Errors = { new GraphQLError(ex.InnerException != null ? ex.InnerException.Message : ex.Message) }
                };
            }

            return(result);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Execute a query using this schema.
        /// </summary>
        /// <param name="gql">The query</param>
        /// <param name="context">The context object. An instance of the context the schema was build from</param>
        /// <param name="serviceProvider">A service provider used for looking up dependencies of field selections and mutations</param>
        /// <param name="claims">Optional claims to check access for queries</param>
        /// <param name="methodProvider"></param>
        /// <param name="includeDebugInfo"></param>
        /// <typeparam name="TContextType"></typeparam>
        /// <returns></returns>
        public QueryResult ExecuteQuery(QueryRequest gql, TContextType context, IServiceProvider serviceProvider, ClaimsIdentity claims, IMethodProvider methodProvider = null, bool includeDebugInfo = false)
        {
            if (methodProvider == null)
            {
                methodProvider = new DefaultMethodProvider();
            }
            Stopwatch timer = null;

            if (includeDebugInfo)
            {
                timer = new Stopwatch();
                timer.Start();
            }

            QueryResult result;

            try
            {
                var graphQLCompiler = new GraphQLCompiler(this, methodProvider);
                var queryResult     = graphQLCompiler.Compile(gql, claims);
                result = queryResult.ExecuteQuery(context, serviceProvider, gql.OperationName);
            }
            catch (Exception ex)
            {
                // error with the whole query
                result = new QueryResult {
                    Errors = { new GraphQLError(ex.InnerException != null ? ex.InnerException.Message : ex.Message) }
                };
            }
            if (includeDebugInfo && timer != null)
            {
                timer.Stop();
                result.SetDebug(new { TotalMilliseconds = timer.ElapsedMilliseconds });
            }

            return(result);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Extension method to query an object purely based on the a defined schema of that object.
        /// </summary>
        /// <param name="context">The root of your object graph you are querying. E.g. a DbContext</param>
        /// <param name="request">GraphQL request object</param>
        /// <param name="schemaProvider">Schema definition. Defines new fields/entities. Maps names, etc.</param>
        /// <param name="methodProvider">Extend the query language with methods</param>
        /// <param name="includeDebugInfo">Include debug/timing information in the result</param>
        /// <typeparam name="TType"></typeparam>
        /// <returns></returns>
        public static QueryResult QueryObject <TType>(this TType context, QueryRequest request, ISchemaProvider schemaProvider, IMethodProvider methodProvider = null, bool includeDebugInfo = false, params object[] mutationArgs)
        {
            if (methodProvider == null)
            {
                methodProvider = new DefaultMethodProvider();
            }
            Stopwatch timer = null;

            if (includeDebugInfo)
            {
                timer = new Stopwatch();
                timer.Start();
            }

            QueryResult result = null;

            try
            {
                var graphQLCompiler = new GraphQLCompiler(schemaProvider, methodProvider);
                var queryResult     = graphQLCompiler.Compile(request);
                result = queryResult.ExecuteQuery(context, request.OperationName, mutationArgs);
            }
            catch (Exception ex)
            {
                // error with the whole query
                result = new QueryResult {
                    Errors = { new GraphQLError(ex.InnerException != null ? ex.InnerException.Message : ex.Message) }
                };
            }
            if (includeDebugInfo && timer != null)
            {
                timer.Stop();
                result.SetDebug(new { TotalMilliseconds = timer.ElapsedMilliseconds });
            }

            return(result);
        }