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;
}
);
}
}
I guess the context derives from the
<GraphQLType>and the Type derives from theObjectGraphType<Model>. So, the context will get the context of the model. I think that may be you want to access some other data from that connection. So, you could define a<GraphQLType>of the specific model of that DB Connection and<NoteType>, and then you can resolve it in this Field. I am not sure this is what you are looking for, but just gave an opinion.