chkimes / graphql-net

Convert GraphQL to IQueryable
MIT License
891 stars 86 forks source link

Question: Does it possible to work not only with EF/NHibernate frameworks? #63

Closed MakaBuka closed 7 years ago

MakaBuka commented 7 years ago

Hi guys,

I've run an example that based on EF and it works fine.

But, could you clarify me some question according to GraphQL:

according to EntityFrameworkExecutionTests.cs I found that you're using EFcontext (based on DbContext - which includes populating objects with data from a database, change tracking, and persisting data to the database): var schema = GraphQL.CreateDefaultSchema(() => new EfContext()); It's mean, that we will have up-to-date data (let's assume that our EFContext is configured and working with MSSQLLocalDB).

It would be great if you clarify me (or just send me an example) how it works without EF (in case when we have to retrieve/update data from database like mysql or mssql)

Looking forward to your reply. Thx, Dima.

chkimes commented 7 years ago

Hi there,

This library attempts to translate a GraphQL query into an IQueryable object. Almost all of the .NET object-relational mappers use IQueryable to represent a query (Entity Framework, NHibernate, LINQ to SQL, Elm, etc.). As long as you use a library that understands IQueryable, this library can work with it. For example, we have tests that work against in-memory Lists by using the .AsQueryable() method:

https://github.com/ckimes89/graphql-net/blob/master/Tests/MemContext.cs https://github.com/ckimes89/graphql-net/blob/master/Tests/InMemoryExecutionTests.cs

What this library can not do is generate SQL directly for use with a database (via ADO.NET or similar). However, you can use an ORM (such as EF) to translate from IQueryable to SQL.

chkimes commented 7 years ago

To clarify, the pipeline for database interaction looks something like:

GraphQL query -> (GraphQL.Net) -> IQueryable object -> (ORM) -> SQL query

In the case of our tests, Entity Framework is taking place of the ORM. You can replace that with anything else that understands IQueryable.

MakaBuka commented 7 years ago

Hi ckimes89,

Thank you for the prompt replay. BTW, I've tried to find any documentation (except test examples) which has relation to mutation.

And the last one question: What I should use if I have request to use condition in queries (more/less; >=, <=).

Thx, Dima.

chkimes commented 7 years ago

Hi Dima,

We're still working on fleshing out all the documentation. In the meantime, here's the issue where I added mutation support: https://github.com/ckimes89/graphql-net/issues/16#issuecomment-229530182

That provides an example of how to do mutation, plus a description of the parameters.

For using conditions in queries, you should check with the syntax of your chosen ORM. Most .NET-based ORMs (all that I know of) support using <= and >= in queries. For example, you could add a query for sales after a certain year:

schema.AddField("salesAfterYear",
    new { year = 0 },
    (db, args) => db.Sales.Where(s => s.Year >= args.year));
MakaBuka commented 7 years ago

Hi ckimes89,

Again and again thank you for the reply and your clarifications :)

According to conditions: so, let's assume we have GraphQL query some like this "products(price>=50){id, name}", and how it could be parsed/described on schema that we're using on server-side and how it might be applied to any ORM frameworks.

BTW, I've just forked your project and I'll be notified if you have some updates.

Thx, D.

chkimes commented 7 years ago

Oh, I misunderstood what you were asking earlier. According to the GraphQL Spec, there's no way to use operators in a GraphQL query. The only thing you can do is pass parameters to a query. I would suggest creating a parameter or query for each comparison, like: products(priceGreaterThan: 50) { id, name } or products(priceLessThan: 50) { id, name }.