Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to migrate existing stored procedures to use graphql

Tags:

graphql

I have a legacy project which uses 700+ complex stored procedures to fetch and modify data. Is there a way to use Graphql to create an API without changing the stored procedures?

like image 370
Sai Manibalan Avatar asked Oct 30 '25 09:10

Sai Manibalan


1 Answers

Depending on the language you are working in, there may be GraphQL libraries that are stored-procedure aware. If not, you can write your own.

You can also write a thin REST layer and layer GraphQL over that.

Edit: If you're using ASP.NET Core, check out this library and follow the examples in their documentation's introduction page, except replace the hard-coded data with calls in the library you would usually use to communicate with SQL Server to fetch the required data. I recommend Dapper (it was actually written by the folks at Stack Overflow!) as it will let you write lightweight queries like conn.Query("[dbo].myFooProc @bar @baz", new { bar = quux, baz = 22 }) (or using whatever existing classes you have).

So ultimately your code would look something like

public class FooBarQuery : ObjectGraphType
{
  public FooBarQuery()
  {

    var MyData  = conn.Query("[dbo].myFooBarProc @FooId", new { FooId = 2 }).ToList(); 
    Field<FooType>(
      resolve: context => MyData; 
    );
  }
}
like image 195
Hugo Avatar answered Nov 01 '25 00:11

Hugo