So, I'm following this GraphQL tutorial and in it we have the code below and I would like to know how, in NotesMutation, can I use another DB connection in "resolve: context", because I have created another one without Entity Framework:
NotesContext.cs:
public class NotesContext : DbContext
{
public DbSet<Note> Notes { get; set; }
public NotesContext(DbContextOptions options) : base(options)
{
}
}
program.cs:
builder.Services.AddDbContext<NotesContext>(options =>
{
options.UseSqlServer(builder.Configuration.GetConnectionString("Default"));
});
builder.Services.AddSingleton<ISchema, NotesSchema>(services => new NotesSchema(new SelfActivatingServiceProvider(services)));
NotesMutation.cs:
public class NotesMutation : ObjectGraphType
{
public NotesMutation()
{
Field<NoteType>(
"createNote",
arguments: new QueryArguments(
new QueryArgument<NonNullGraphType<StringGraphType>> { Name = "message"}
),
resolve: context =>
{
var message = context.GetArgument<string>("message");
var notesContext = context.RequestServices.GetRequiredService<NotesContext>();
var note = new Note
{
Message = message,
};
notesContext.Notes.Add(note);
notesContext.SaveChanges();
return note;
}
);
}
}