0

I am trying to build an application using .Net and GraphQL. I need to get materials. not all of them but with the given Ids. When I pass it via playground or client side, I don't have any problem when I debug but I am not sure how to parse in the server side.

                name: "materialsByIds",
                arguments: new QueryArguments(
                            new QueryArgument<ListGraphType<IntGraphType>> { Name = "ids"}),
                resolve: async (context) =>
                {
                    var ids = context.GetArgument<IntGraphType>("ids");
                    // Do some action to bring datas
                    // Send data back
                }

What am I missing here is there any methods to parse this in to list of int back?

2 Answers2

1

Instead of using a GraphType for retrieving the argument, use the .NET type you want.

name: "materialsByIds",
arguments: new QueryArguments(
               new QueryArgument<ListGraphType<IntGraphType>> { Name = "ids"}),

resolve: async (context) =>
{
    var ids = context.GetArgument<List<int>>("ids");
    // Do some action to bring datas
    // Send data back
 }
Joe McBride
  • 3,789
  • 2
  • 34
  • 38
0

you can use MediatR. Create a Query class and pass it to mediateR. In CQRS, Command is used to write on DB(Create/Delete/Update of CRUD) and Query is used to read from DB(Read of CRUD).

create 'GetMaterialsByIdsQuery' and inside it write your code to get your materials by Id. then use it inside 'resolve' like this:

resolve: async context =>
             {
               var ids = context.GetArgument<List<int>>("ids");             
               return await mediator.Send(new GetMaterialsByIdsQuery(ids));
             })

another way is that you can return somthing like MaterialsRepository.GetMaterialsByIds(ids) instead of using mediatR. But it is not recommended to use repository here. you can create a service class, inside it use your repository and then use your service here.

  • I am using MediatR in the project already. We were getting the materials one by one. I have created the mediatR and business implementation already. I was looking how to get a list of Ids from a query. The answer one above worked. Thanks for the answer. – Abuzer ALACA Mar 15 '22 at 12:51
  • you're welcome. joe has said true! i have copied your code to just saying about MediatR and i haven't seen the mistake about IntGraphType. I have edited it now. – Shohreh Mortazavi Mar 23 '22 at 22:05