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

Transport for application/graphql contentType #2592

Merged
merged 3 commits into from
Apr 7, 2023

Conversation

RatkoR
Copy link
Contributor

@RatkoR RatkoR commented Mar 25, 2023

This pull requests has three commits:

8cac3b5 adds new GRAPHQL transport
a270f35 adds header tests for this new transport
829596f adds GRAPHQL transport to the documentation

It is split into three commits to be easier to look at changes.

GRAPHQL transport is described here: https://graphql.org/learn/serving-over-http/#post-request nothing about it is on https://graphql.github.io/graphql-over-http/draft/.

I created a new transport (http_graphql.go) to minimize impact on existing gqlgen implementations. I could have added it to POST transport, but that would be riskier and an error there could make all gql servers inoperable. With it's own transport, developers can eneble or disable it, while keeping POST transport working as before.

From observing our graphql clients that use application/graphql content-type, some add query= at body start, some html escape body contents and some don't. I don't know what is correct, but in order for us to migrate to gqlgen, we have to handle all those clients successfully. That's why cleanupBody() does some body transformations for this transport.

I did not add GRAPHQL transport to the default server to keep existing gqlgen instances as are. Those interested in application/graphql can add it themselves as I noted in migration-0.11.md.

Closes #1129

Comments are welcome.

I have:

  • Added tests covering the bug / feature (see testing)
  • Updated any relevant documentation (see docs)

RatkoR added 3 commits March 25, 2023 12:03
This commit adds 'application/graphql' transport. It is based
on POST metod and has only the 'query' part in it's body.

See: https://graphql.org/learn/serving-over-http/#post-request and
it's comment about this content-type.

An example of correct application/graphql query is:

```
curl 'http://host/graphql' -d '{time{now}}' -H "Content-Type: application/graphql"
```

Some clients prefix body with 'query=':

```
-d 'query={time{now}}'
```

Some clients html encode body payload:

```
-d 'query=%7Btime%7Bnow%7D%7D'
```

We cleanup both in cleanupBody() method.

Tests are in http_graphql_test.go file.
GRAPHQL transport (like GET, POST and MULTIPART transports) can
have specific response headers added.

This commit adds tests for it and changes doRequest() method
so that we can set inbound Content-Type. Graphql transport
uses 'application/graphql' content-type and not 'application/json'.
@coveralls
Copy link

Coverage Status

Coverage: 75.387% (-0.02%) from 75.408% when pulling 829596f on RatkoR:graphql_transport into 677d854 on 99designs:master.

@RatkoR RatkoR changed the title Transport handling application/graphql contentType Transport for application/graphql contentType Mar 28, 2023
@StevenACoffman StevenACoffman merged commit 4548815 into 99designs:master Apr 7, 2023
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.

Support content-type application/graphql
3 participants