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?