Exemplo n.º 1
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 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);
        }
Exemplo n.º 2
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="query">GraphQL query</param>
 /// <param name="schemaProvider">Schema definition. Defines new fields/entities. Maps names, etc.</param>
 /// <typeparam name="TType"></typeparam>
 /// <returns></returns>
 public static QueryResult QueryObject <TType>(this TType context, QueryRequest request, ISchemaProvider schemaProvider, params object[] mutationArgs)
 {
     return(QueryObject(context, request, schemaProvider, null, false, mutationArgs));
 }