18

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?

Dylan Cristy
  • 916
  • 11
  • 29

1 Answers1

25
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");
Joe McBride
  • 3,789
  • 2
  • 34
  • 38
  • So I could force `GetArgument` to return `null` if the argument does not exist by doing `context.GetArgument("email", defaultValue: null)`? – Dylan Cristy Jun 20 '18 at 16:13
  • 1
    Yes. Providing a default value is optional (`defaultValue` is an optional parameter). The default for a string would be null. – Joe McBride Jun 20 '18 at 19:30