GraphQL.Net List of int as argument

990 Views Asked by At

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

There are 2 best solutions below

1
Joe McBride On BEST ANSWER

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
 }
2
Shohreh Mortazavi On

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.