1

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?

Alexcei Shmakov
  • 2,203
  • 3
  • 19
  • 34

1 Answers1

0

You are mapping the getAllUsers() in the wrong place, you should map that method as the ListGraphType in the RootQuery, then when you call something like this it should return you all the users.

query getAllUsers
   {
   users
   items {
     email
        }
   }

Note that query getAllUsers is arbitrarily and you can change it to query getWhatEver, and you will still get the same result. But with this graphql query above you are returning a list of users that have a field of items, and items have a field of email. If you want to only return a object you need to map a UserType field which will in essence return you the object you are looking for.

The query will also execute without the query GetAllUsers if you write it like this

   {
   users
   items {
     email
        }
   }

Check the documentation and the examples one more time for more clarification.

ItsIgzy
  • 446
  • 2
  • 7