Exemplo n.º 1
0
    static ResolveFieldContext ResolveFieldContext(
        GraphQlEfSampleDbContext ctx,
        CancellationToken token,
        Document document,
        ISchema schema)
    {
        var operation        = document.Operations.FirstOrDefault();
        var variableValues   = ExecutionHelper.GetVariableValues(document, schema, operation?.Variables, null);
        var executionContext = new ExecutionContext
        {
            Document                  = document,
            Schema                    = schema,
            UserContext               = ctx,
            Variables                 = variableValues,
            Fragments                 = document.Fragments,
            CancellationToken         = token,
            Listeners                 = new IDocumentExecutionListener[0],
            Operation                 = operation,
            ThrowOnUnhandledException = true // DEBUG
        };

        var operationRootType = ExecutionHelper.GetOperationRootType(
            executionContext.Document,
            executionContext.Schema,
            executionContext.Operation);

        var node = ExecutionStrategy.BuildExecutionRootNode(executionContext, operationRootType);

        return(GetContext(executionContext, node.SubFields["companies"]));
    }
 public Task <ExecutionResult> Post(
     [BindRequired, FromBody] PostBody body,
     [FromServices] GraphQlEfSampleDbContext dbContext,
     CancellationToken cancellation)
 {
     return(Execute(dbContext, body.Query, body.OperationName, body.Variables, cancellation));
 }
Exemplo n.º 3
0
    async Task <ExecutionResult> Execute(
        GraphQlEfSampleDbContext dbContext,
        string query,
        string operationName,
        JObject variables,
        CancellationToken cancellation)
    {
        var options = new ExecutionOptions
        {
            Schema            = schema,
            Query             = query,
            OperationName     = operationName,
            Inputs            = variables?.ToInputs(),
            UserContext       = dbContext,
            CancellationToken = cancellation,
#if (DEBUG)
            ExposeExceptions = true,
            EnableMetrics    = true,
#endif
        };

        var result = await executer.ExecuteAsync(options);

        if (result.Errors?.Count > 0)
        {
            Response.StatusCode = (int)HttpStatusCode.BadRequest;
        }

        return(result);
    }
Exemplo n.º 4
0
    public void ConfigureServices(IServiceCollection services)
    {
        GraphTypeTypeRegistry.Register <Employee, EmployeeGraph>();
        GraphTypeTypeRegistry.Register <EmployeeSummary, EmployeeSummaryGraph>();
        GraphTypeTypeRegistry.Register <Company, CompanyGraph>();
        services.AddScoped(_ => DbContextBuilder.BuildDbContext());
        services.AddSingleton <Func <GraphQlEfSampleDbContext> >(provider => provider.GetRequiredService <GraphQlEfSampleDbContext>);

        EfGraphQLConventions.RegisterInContainer <GraphQlEfSampleDbContext>(
            services,
            model: GraphQlEfSampleDbContext.GetModel());
        EfGraphQLConventions.RegisterConnectionTypesInContainer(services);

        foreach (var type in GetGraphQlTypes())
        {
            services.AddSingleton(type);
        }

        var graphQl = services.AddGraphQL(
            options => options.ExposeExceptions = true);

        graphQl.AddWebSockets();
        services.AddSingleton <ContextFactory>();
        services.AddSingleton <IDocumentExecuter, EfDocumentExecuter>();
        services.AddSingleton <IDependencyResolver>(
            provider => new FuncDependencyResolver(provider.GetRequiredService));
        services.AddSingleton <ISchema, Schema>();
        var mvc = services.AddMvc();

        mvc.SetCompatibilityVersion(CompatibilityVersion.Latest);
    }
    public static IModel GetModel()
    {
        var builder = new DbContextOptionsBuilder();

        builder.UseSqlServer("Fake");
        using var dbContext = new GraphQlEfSampleDbContext(builder.Options);
        return(dbContext.Model);
    }
    static async Task CreateDb(GraphQlEfSampleDbContext context)
    {
        await context.Database.EnsureCreatedAsync();

        var company1 = new Company
        {
            Id      = 1,
            Content = "Company1"
        };
        var employee1 = new Employee
        {
            Id        = 2,
            CompanyId = company1.Id,
            Content   = "Employee1",
            Age       = 25
        };
        var employee2 = new Employee
        {
            Id        = 3,
            CompanyId = company1.Id,
            Content   = "Employee2",
            Age       = 31
        };
        var company2 = new Company
        {
            Id      = 4,
            Content = "Company2"
        };
        var employee4 = new Employee
        {
            Id        = 5,
            CompanyId = company2.Id,
            Content   = "Employee4",
            Age       = 34
        };
        var company3 = new Company
        {
            Id      = 6,
            Content = "Company3"
        };
        var company4 = new Company
        {
            Id      = 7,
            Content = "Company4"
        };
        var employee5 = new Employee
        {
            Id        = 8,
            Content   = null,
            CompanyId = company2.Id
        };

        context.AddRange(company1, employee1, employee2, company2, company3, company4, employee4, employee5);
        await context.SaveChangesAsync();
    }
    public Task <ExecutionResult> Get(
        [FromQuery] string query,
        [FromQuery] string variables,
        [FromQuery] string operationName,
        [FromServices] GraphQlEfSampleDbContext dbContext,
        CancellationToken cancellation)
    {
        var jObject = ParseVariables(variables);

        return(Execute(dbContext, query, operationName, jObject, cancellation));
    }
Exemplo n.º 8
0
    static void CreateDb(DbContextOptionsBuilder <GraphQlEfSampleDbContext> builder)
    {
        using (var context = new GraphQlEfSampleDbContext(builder.Options))
        {
            context.Database.EnsureCreated();

            Model = context.Model;

            var company1 = new Company
            {
                Id      = 1,
                Content = "Company1"
            };
            var employee1 = new Employee
            {
                Id        = 2,
                CompanyId = company1.Id,
                Content   = "Employee1",
                Age       = 25
            };
            var employee2 = new Employee
            {
                Id        = 3,
                CompanyId = company1.Id,
                Content   = "Employee2",
                Age       = 31
            };
            var company2 = new Company
            {
                Id      = 4,
                Content = "Company2"
            };
            var employee4 = new Employee
            {
                Id        = 5,
                CompanyId = company2.Id,
                Content   = "Employee4",
                Age       = 34
            };
            var company3 = new Company
            {
                Id      = 6,
                Content = "Company3"
            };
            var company4 = new Company
            {
                Id      = 7,
                Content = "Company4"
            };
            context.AddRange(company1, employee1, employee2, company2, company3, company4, employee4);
            context.SaveChanges();
        }
    }
    static void CreateDb(GraphQlEfSampleDbContext context)
    {
        context.Database.EnsureCreated();

        var company1 = new Company
        {
            Id      = 1,
            Content = "Company1"
        };
        var employee1 = new Employee
        {
            Id        = 2,
            CompanyId = company1.Id,
            Content   = "Employee1",
            Age       = 25
        };
        var employee2 = new Employee
        {
            Id        = 3,
            CompanyId = company1.Id,
            Content   = "Employee2",
            Age       = 31
        };
        var company2 = new Company
        {
            Id      = 4,
            Content = "Company2"
        };
        var employee4 = new Employee
        {
            Id        = 5,
            CompanyId = company2.Id,
            Content   = "Employee4",
            Age       = 34
        };
        var company3 = new Company
        {
            Id      = 6,
            Content = "Company3"
        };
        var company4 = new Company
        {
            Id      = 7,
            Content = "Company4"
        };

        context.AddRange(company1, employee1, employee2, company2, company3, company4, employee4);
        context.SaveChanges();
    }
Exemplo n.º 10
0
    static async Task <List <Company> > GetCompanies(
        ResolveEventStreamContext context,
        GraphQlEfSampleDbContext ctx,
        long lastId,
        int take = 1,
        CancellationToken token = default)
    {
        var returnType = ctx.Companies;

        var document = new GraphQLDocumentBuilder().Build(SupposePersistedQuery());

        var fieldContext = ResolveFieldContext(ctx, token, document, context.Schema);

        var withArguments = returnType.ApplyGraphQlArguments(fieldContext);

        var greaterThanLastIdAndPaged = withArguments
                                        .Where(transaction => transaction.Id > lastId)
                                        .Take(take);

        return(await greaterThanLastIdAndPaged.ToListAsync(token));
    }
    Task <ExecutionResult> Execute(
        GraphQlEfSampleDbContext dbContext,
        string query,
        string operationName,
        JObject variables,
        CancellationToken cancellation)
    {
        var options = new ExecutionOptions
        {
            Schema            = schema,
            Query             = query,
            OperationName     = operationName,
            Inputs            = variables?.ToInputs(),
            UserContext       = dbContext,
            CancellationToken = cancellation,
#if (DEBUG)
            ExposeExceptions = true,
            EnableMetrics    = true,
#endif
        };

        return(executer.ExecuteAsync(options));
    }
 public GraphQlController(ISchema schema, IDocumentExecuter executer, GraphQlEfSampleDbContext dbContext)
 {
     this.schema    = schema;
     this.executer  = executer;
     this.dbContext = dbContext;
 }