Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Status codes per GraphQL over HTTP spec #1142

Merged
merged 5 commits into from
Aug 9, 2024
Merged

Conversation

Shane32
Copy link
Member

@Shane32 Shane32 commented Aug 9, 2024

No description provided.

@Shane32 Shane32 self-assigned this Aug 9, 2024
@Shane32 Shane32 added this to the 8.0 milestone Aug 9, 2024
@Shane32 Shane32 requested a review from gao-artur August 9, 2024 04:30
var contentType = SelectResponseContentType(context);
context.Response.ContentType = contentType == _options.DefaultResponseContentType ? _options.DefaultResponseContentTypeString : contentType.ToString();
context.Response.StatusCode = (int)HttpStatusCode.OK;
if (result.Executed == false)

Check notice

Code scanning / CodeQL

Unnecessarily complex Boolean expression Note

The expression 'A == false' can be simplified to '!A'.
- `GraphQLHttpMiddlewareOptions.ValidationErrorsReturnBadRequest` is now a nullable boolean where
`null` means "use the default behavior". The default behavior is to return a 200 status code
when the response content type is `application/json` and a 400 status code otherwise. The
default value for this in v7 was `true`; set this option to retain the v7 behavior.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the new default is the only correct behavior. Let's deprecate this configuration, maybe?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, users may likely have clients that count on GraphQL returning 400 even if they use application/json. The GraphQL over http spec only recommends 200 be returned, and only because (in theory) proxy services may return 400 with application/json. Users will know if their configuration meets that criteria or not, and if such a concern is warranted. And other users’ client code may always expect 200 even in the case of an error.

I think this is rather important to keep flexible, just as it was configurable in v7.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The client can choose the status codes by using either application/json or application/graphql-response+json. The additional option: using application/json and expecting 4xx codes is not forbidden but discouraged. What can force users to prefer this behavior when they can just change the Accept header?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're assuming that they can control the clients behavior easily. In our company, we have a large infrastructure of GraphQL services that talk to each other. One client may be .NET code, one may be javascript, we even have python code. Changing the format of responses would require us to review each of these different pieces of code scattered throughout our various codebases. Any tests we've written that validate the returned status code may need to change. Since we are not exposing GraphQL to the public, compatibility with the GraphQL over HTTP protocol is not a concern. And if we had a public API, it would be important to maintain behavior of the endpoint throughout the supported version of that API. I've always had our servers return 400 for validation errors, and I expect that to continue.

It is also important to note that Apollo GraphQL server behaves as if this option were false by default, always returning 200 for validation errors (which are not transport errors). For compatibility with other servers alone, we may want to keep this option.

I really don't see any reason this isn't a valid and normal configuration option for the server project, just like changing the default content type returned, enabling/disabling CSRF protection, or enabling/disabling form parsing. Keep in mind that it's just an option, and it's configured to the recommended default per the draft GraphQL over HTTP spec. (The spec isn't even finalized yet!)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood

@Shane32 Shane32 merged commit bf0cd69 into develop Aug 9, 2024
7 checks passed
@Shane32 Shane32 deleted the better_status_codes branch August 9, 2024 14:45
@Shane32 Shane32 linked an issue Aug 9, 2024 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

HttpStatus Code on GRAPHQL_VALIDATION error
2 participants