I am using a graphql-dotnet to build a graphql server in Net Core 2.2 Web API.
Schema description:
I have a ObjectGraphType
like below
public class UserType
: ObjectGraphType<Users>
{
public UserType()
{
Field(x => x.ContactNumber);
Field(x => x.Email, true);
Field(x => x.Fax);
}
}
}
I have a query definition like
public class UserOperationNameQuery
: ObjectGraphType
{
public UserOperationNameQuery()
{
Name = "getAllUsers";
Field<ListGraphType<UserType>>(name: "items", description: "Get all users",
resolve: context =>
{
// resolver
});
}
My schema is
public class GraphQLSchema
: Schema
{
public GraphQLSchema(IDependencyResolver resolver)
: base(resolver)
{
Query = resolver.Resolve<UserOperationNameQuery>();
}
}
My method of controller is:
[HttpPost]
public async Task<IActionResult> Post([FromBody] GraphQLQuery query)
{
if (query == null) { throw new ArgumentNullException(nameof(query)); }
var inputs = query.Variables.ToInputs();
var executionOptions = new ExecutionOptions
{
Schema = _schema,
Query = query.Query,
Inputs = inputs,
OperationName = query.OperationName,
};
var result = await _documentExecuter.ExecuteAsync(executionOptions).ConfigureAwait(false);
if (result.Errors?.Count > 0)
{
return BadRequest(result);
}
return Ok(result);
}
Query:
I send this query
query getAllUsers
{
items {
email
}
}
It is works.
But if I change the operation name on any value the query also executes and I get users.
query getAllCompanies
{
items {
email
}
}
I can write any operation name in the request and it will be succesfull executed.
I want to ask users only by certain operation name (in my case getAllUsers
) that I can to set somewhere in the definition's schema. As more if I skip the operation name the request should not be executed.
How can I do it?