/// <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); }
/// <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); }
/// Function that returns the DataContext for the queries. If null _serviceProvider is used public static IDictionary <string, object> QueryObject <TType>(this TType context, string dataQuery, ISchemaProvider schemaProvider, IRelationHandler relationHandler = null, IMethodProvider methodProvider = null, bool includeDebugInfo = false) { if (methodProvider == null) { methodProvider = new DefaultMethodProvider(); } Stopwatch timer = null; if (includeDebugInfo) { timer = new Stopwatch(); timer.Start(); } var allData = new ConcurrentDictionary <string, object>(); try { var objectGraph = new DataApiCompiler(schemaProvider, methodProvider, relationHandler).Compile(dataQuery); // Parallel.ForEach(objectGraph.Fields, node => foreach (var node in objectGraph.Fields) { try { if (!string.IsNullOrEmpty(node.Error)) { System.Console.WriteLine(node.Error); allData[node.Name] = node.Error; } else { var data = node.AsLambda().Compile().DynamicInvoke(context); allData[node.Name] = data; } } catch (Exception ex) { allData[node.Name] = new { eql_error = ex.Message }; } } // ); } catch (Exception ex) { allData["error"] = ex.Message; } if (includeDebugInfo && timer != null) { timer.Stop(); allData["_debug"] = new { TotalMilliseconds = timer.ElapsedMilliseconds }; } return(allData); }
public static CompiledQueryResult CompileWith(string query, Expression context, ISchemaProvider schemaProvider, IMethodProvider methodProvider = null, QueryVariables variables = null) { if (methodProvider == null) { methodProvider = new DefaultMethodProvider(); } if (variables == null) { variables = new QueryVariables(); } var expression = CompileQuery(query, context, schemaProvider, methodProvider, variables); var parameters = expression.Expression.NodeType == ExpressionType.Lambda ? ((LambdaExpression)expression.Expression).Parameters.ToList() : new List<ParameterExpression>(); return new CompiledQueryResult(expression, parameters); }
/// <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 IDictionary <string, object> QueryObject <TType>(this TType context, QueryRequest request, ISchemaProvider schemaProvider, IMethodProvider methodProvider = null, bool includeDebugInfo = false) { if (methodProvider == null) { methodProvider = new DefaultMethodProvider(); } Stopwatch timer = null; if (includeDebugInfo) { timer = new Stopwatch(); timer.Start(); } var queryData = new ConcurrentDictionary <string, object>(); var result = new Dictionary <string, object>(); var errors = new List <GraphQLError>(); try { var objectGraph = new GraphQLCompiler(schemaProvider, methodProvider).Compile(request); foreach (var node in objectGraph.Fields.Where(f => f.IsMutation)) { ExecuteNode(context, request, queryData, node); } // Parallel.ForEach(objectGraph.Fields, node => foreach (var node in objectGraph.Fields.Where(f => !f.IsMutation)) { ExecuteNode(context, request, queryData, node); } // ); } catch (Exception ex) { // error with the whole query errors.Add(new GraphQLError(ex.Message)); } if (includeDebugInfo && timer != null) { timer.Stop(); result["_debug"] = new { TotalMilliseconds = timer.ElapsedMilliseconds }; } result["data"] = queryData; result["errors"] = errors; return(result); }
/// <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); }
/// <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); }