Coder Social home page Coder Social logo

Comments (3)

lukemurray avatar lukemurray commented on June 2, 2024

from entitygraphql.

StefanoVuerich avatar StefanoVuerich commented on June 2, 2024

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.

lukemurray avatar lukemurray commented on June 2, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.