Comments (3)
from entitygraphql.
Hi Luke,
I manage my problem using these classes:
public class GraphQLSchemaBuilder<T> where T : class
{
public static void ConfigureSchema(SchemaProvider<GraphQLDbContext> schema)
{
var instance = Activator.CreateInstance<T>();
var name = instance.GetType().Name;
if (!schema.HasType(instance.GetType().Name))
{
schema.AddType<T>(name).AddAllFields();
}
schema.UpdateQuery(queryType =>
{
queryType.AddField(name, db => db.Set<T>(), $"List of {name}");
});
System.IO.File.WriteAllText("schema.graphql", schema.ToGraphQLSchemaString());
}
}
Then I can call the ConfigureSchema method using refletion:
builder.ConfigureServices((context, services) =>
{
services.AddDbContext<GraphQLDbContext>(/*config the db context*/);
services.AddGraphQLSchema<GraphQLDbContext>(options =>
{
options.ConfigureSchema = schemaBuilder =>
{
var myModels = Assembly.GetEntryAssembly().GetTypes().Where(/* logic to get only models I want to manage through graphql */)
myModels .ForEach(m=>
{
Type generic = typeof(GraphQLSchemaBuilder<>);
var model = Activator.CreateInstance(m);
Type[] typeArgs = { model.GetType() };
Type constructed = generic.MakeGenericType(typeArgs);
var method = constructed.GetMethod("ConfigureSchema");
method.Invoke(Activator.CreateInstance(constructed), new object[] { schemaBuilder });
});
};
});
});
In this way dbcontext can be like this:
public class GraphQLDbContext : DbContext
{
public GraphQLDbContext(DbContextOptions<GraphQLDbContext> options) : base(options) { }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetEntryAssembly()); //take only models I want to manage through graphql
}
}
@lukemurray What you think about this? I tried and it is working, do you see any downside?
Thanks
from entitygraphql.
OK. I didn't realise you just wanted to use ctx.Set<MyEntity>()
as the root of the expression.
Yes that will work and should be no issues. It is the same as manually adding them all but using reflection to auto add the ones you want if you don't want them defined at the DbContext
or manually write out each one.
from entitygraphql.
Related Issues (20)
- Localized Validation with Member Names. HOT 3
- Filter problem with boolean "Field true not found on type..." HOT 8
- Update doc for Mutatation GraphQLValidator for versions >= 5.0.0 HOT 1
- Customize the schema HOT 2
- No support for cyrillics in the filters and automatic types conversion
- UseFilter converting strings to DateTime
- Recommended way to restrict access to entities based on authenticated user HOT 2
- How to configure an argument HOT 2
- Improve error handling to allow users to make a decision on the type of error HOT 4
- federation / subgraph support HOT 1
- Filtering with IN, ANY operators HOT 4
- [Description] attribute incorrectly updates argument name, not description
- [GraphQLField] Attribute on a method also renames the arguments? (but only at time of execution) HOT 1
- Use OutputCache with Entity GraphQl? HOT 6
- Subscription returning 405 Method Not Allowed HOT 7
- How to use it in azure function? HOT 26
- Nested non-scalar types are not using its input type declarations HOT 2
- Offset Paging issue with two top level fields returning the paged object HOT 10
- DateOnly and Spacial Data types not supported HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from entitygraphql.