在.NET Core 2.0 WebApi中实现GraphQL

时间:2018-01-16 04:24:39

标签: c# postgresql asp.net-web-api graphql asp.net-core-2.0

我想知道你是否可以帮我解决GraphQL和.NET Asp.Net WebAPI问题。

我目前有这种情况,我有一个.NET Core 2.0 WebApi项目,并使用Postgresql作为数据库后端。 (见下文的实施)。

客户端实施:

public class Client {
   public long Id { get; set; }
   public Name Name { get; set; }
   public long Id { get; set; }
   public string Email { get; set; }
   public IEnumerable<Paper> Papers { get; set;}
}

public class Name {
   public string First {get; set;}
   public string Middle {get; set;}
   public string Last {get; set;}
}

public class Paper {
   public long Id { get; set; }
   public string Content { get; set; }
   public string Title { get; set; }
}

public class ClientService
{
    public IMyRepository _clients;

    public ClientService(IMyRepository client) {
        _clients = client;
    }

    public IEnumerable<Client> GetClients()
    {
        return _clients.GetAllClients();
    }

    public IEnumerable<Client> Search(long id) {
        return _clients.GetById(id);
    }

    public IEnumerable<Client> SearchByEmail(string email) {
        return _clients.GetById(id);
    }

    public IEnumerable<Client> SearchPapers(string title) {
        return _clients.GetClientsWithPaperTitle(title);
    }

    // and any other searches I want to implement based on Client and Paper classes
}


public class ClientController: Controller
{
    private ClientService _service;

    public ClientController(ClientService service) {
        _service = service;
    }

    public IActionResult Get() {
        var records = _service.GetClients();
        return new JsonResult(records);
    }

    // This is where I'm getting stuck
    public IActionResult Search(string value) {
        var records = _service.GetClients();
        return new JsonResult(records);
    }
}

public class Query {
    private ClientService _service;

    [GraphQLMetadata("client")]
    public async Task<IClient> GetClient()
    {
        return _service.GetClients();
    }

    [GraphQLMetadata("client")]
    public async Task<IClient> GetClient(string email, long id, long paperId, string paperTitle)
    {
        return _service.GetClients();
    }


}

我正在考虑提供一个单一的端点,我希望webapi客户端使用者根据预定义的预期类对象提供所需的过滤器。

鉴于GraphQL能够公开类似查询的函数,GraphQL是否能够执行复杂的WHERE子句,我可以传递类似于此的过滤器对象?

如果使用查询GetClient(电子邮件:“test@email.com”)

 {
        client(email: "test@email.com, paper: "MyPaperTitle", range.paperId (1,5) {
            Id
            Name
            Email
            Papers {
                Title
                Content
            }
        }
   }

的等效Postgresql查询
SELECT *
   FROM 
        client
        inner join papers on client.Id = Papers.ClientId
    WHERE
        Email = 'test@email.com'
        and papers.title LIKE 'MyTitle%'
        and papers.id  between 1 and 5
    ;

或者:

 {
        client(email: "test@email.com) {
            Id
            Name
            Email
            Papers (paper: "MyPaperTitle") {
                Title
                Content
            }
        }
   }

的等效Postgresql查询
SELECT *
   FROM 
        client
        inner join papers on client.Id = Papers.ClientId
    WHERE
        Email = 'test@email.com'
        and papers.title LIKE 'MyTitle%'
    ;

非常感谢有关此方面的任何帮助/建议。

非常感谢, 安

0 个答案:

没有答案