graphql-dotnet / server

ASP.NET Core GraphQL Server
MIT License
581 stars 164 forks source link

operationName is not set when using GraphQL.Server.Transports.AspNetCore.SystemTextJson #346

Closed chinwobble closed 4 years ago

chinwobble commented 4 years ago

We would like the operation name for logging purposes.

When I submit a query like this:

POST /graphql
content-type: application/json

query loadSummary {
   todo {
      id
   }

I don't get any operation name.

public class GraphQLExecutorWithDiagnostics<TSchema> : DefaultGraphQLExecuter<TSchema>
        where TSchema : GraphQL.Types.ISchema
    {
        private readonly TelemetryClient _telemetryClient;

        public GraphQLExecutorWithDiagnostics(
            TSchema schema,
            IDocumentExecuter documentExecuter,
            IOptions<GraphQLOptions> options,
            IEnumerable<IDocumentExecutionListener> listeners,
            IEnumerable<IValidationRule> validationRules,
            TelemetryClient telemetryClient)
            : base(schema, documentExecuter, options, listeners, validationRules)
        {
            _telemetryClient = telemetryClient;
        }

        public override async Task<ExecutionResult> ExecuteAsync(
            string operationName,
            string query,
            Inputs variables,
            IDictionary<string, object> context,
            CancellationToken cancellationToken = default)
        {
           // operationName is always null :(
            using var operation = _telemetryClient.StartOperation<RequestTelemetry>(operationName);

            var result = await base.ExecuteAsync(operationName, query, variables, context, cancellationToken);
            return result;
        }
    }

I am using the following packages.

<PackageReference Include="GraphQL" Version="3.0.0-preview-1490" />
    <PackageReference Include="GraphQL.Server.Transports.AspNetCore" Version="3.5.0-alpha0046" />
    <PackageReference Include="GraphQL.Server.Transports.AspNetCore.NewtonsoftJson" Version="3.5.0-alpha0046" />
sungam3r commented 4 years ago

It depends on what request the client sends. Try Samples.Server example and press F12 to open developer console, then network tab, select your request and see request payload:

{"operationName":"aaa","variables":{},"query":"query aaa {\n  messages {\n    sentAt\n  }\n}\n"}

You will see operation name. Original request:

query aaa {
    messages
    {
        sentAt
    }
}

operationName arg equals aaa.

chinwobble commented 4 years ago

Thanks for answering quickly. I don't think there's an issue with the library. The problem is postman doesn't extract the operationName and send it separately.

When using apollo client it does it automatically so for my purposes its working fine.