Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In GraphQL .NET, how do I specify that a query can take optional parameters?

Let's say I want to be able to query for a user by specifying their ID, or by specifying some other identifier, like email address.

How do you construct the root Query object to accept that?

Given this

public class MyQuery : ObjectGraphType
{
    public MyQuery(IUserService userService)
    {
        Name = "Query";

        Field<UserType>(
            "user",
            arguments: new QueryArguments(
                new QueryArgument<IntGraphType>() { Name = "id" },
                new QueryArgument<StringGraphType>() { Name = "email" }
            ),
            resolve: context =>
            {
                int? id = context.GetArgument<int>("id");
                if (id != null)
                {
                    return userService.GetUserById(id);
                }
                string email = context.GetArgument<string>("email");
                if (email != null)
                {
                    return userService.GetUserByEmail(email);
                }
                return null;
            }
        );
    }
}

Is that the right way to do it? Will context.GetArgument() return null if it doesn't find the argument in the query? Or does providing two arguments to the QueryArguments mean that both arguments are required for the query?

like image 495
Dylan Cristy Avatar asked Jun 19 '18 18:06

Dylan Cristy


1 Answers

arguments: new QueryArguments(
  new QueryArgument<IntGraphType>() { Name = "id" },
  new QueryArgument<StringGraphType>() { Name = "email" }
)

This means that these arguments are nullable, which would make them optional.

arguments: new QueryArguments(
  new QueryArgument<NonNullGraphType<IntGraphType>>() { Name = "id" },
  new QueryArgument<NonNullGraphType<StringGraphType>>() { Name = "email" }
)

Wrapping the GraphType in NonNullGraphType specifies that the value should be non-null, making it required to provide a non-null value.

By default GetArgument<TType> will return default(TType) if the argument does not exist. You could also use:

context.GetArgument<string>("email", defaultValue: "my default value");
like image 143
Joe McBride Avatar answered Oct 10 '22 01:10

Joe McBride