thecodingmachine / graphqlite

Use PHP Attributes/Annotations to declare your GraphQL API
https://graphqlite.thecodingmachine.io
MIT License
555 stars 95 forks source link

Application/json is a valid graphql content-type #592

Closed Csardelacal closed 1 year ago

Csardelacal commented 1 year ago

Altair, for example, sends all of it's requests using application/json in it's content-type. Adding this allows graphqlite to properly parse the json sent by Altair. Everything seems to work as expected the moment it's added to the whitelisted content-types.

# Unpatched

❯ curl 'http://figure.commishes.local:80/api/v1' -H 'Accept-Encoding: gzip, deflate, br' -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Connection: keep-alive' -H 'Origin: moz-extension://6bd3578a-23f3-4834-bce9-724640c5fe8a' --data-binary '{"query":"# Welcome to Altair GraphQL Client.\n# You can send your request using CmdOrCtrl + Enter.\n\n# Enter your graphQL query here.\n\nquery ggb {\n  test\n}","variables":{}}' --compressed
Okay

# With content-type set to application/graphql but sending a json payload
❯ curl 'http://figure.commishes.local:80/api/v1' -H 'Accept-Encoding: gzip, deflate, br' -H 'Content-Type: application/graphql' -H 'Accept: application/json' -H 'Connection: keep-alive' -H 'Origin: moz-extension://6bd3578a-23f3-4834-bce9-724640c5fe8a' --data-binary '{"query":"# Welcome to Altair GraphQL Client.\n# You can send your request using CmdOrCtrl + Enter.\n\n# Enter your graphQL query here.\n\nquery ggb {\n  test\n}","variables":{}}' --compressed
{"errors":[{"message":"Syntax Error: Expected Name, found String \"query\"","locations":[{"line":1,"column":2}]}]}%                                                                           

# Patched
❯ curl 'http://figure.commishes.local:80/api/v1' -H 'Accept-Encoding: gzip, deflate, br' -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Connection: keep-alive' -H 'Origin: moz-extension://6bd3578a-23f3-4834-bce9-724640c5fe8a' --data-binary '{"query":"# Welcome to Altair GraphQL Client.\n# You can send your request using CmdOrCtrl + Enter.\n\n# Enter your graphQL query here.\n\nquery ggb {\n  test\n}","variables":{}}' --compressed
{"errors":[{"message":"Cannot query field \"test\" on type \"Query\".","locations":[{"line":7,"column":3}]}]}%                                                                                
Csardelacal commented 1 year ago

I just noticed the unit test failing, and I am assuming that it is in order to allow rest endpoints to still work with the application/json header. It seems that the json content-type is generally very common across graphql clients (I use Altair and Vue-Apollo and they both do use it).

Is there any way to make the application/json header work with GraphQL?

oojacoboo commented 1 year ago

Yea, it's not working like you think. Please review that file again. GraphQLite should be accepting both application/json and application/graphql. The responses are handled differently. We use Altair ourselves without issue.

Csardelacal commented 1 year ago

Okay, it seems that the trailing slashes in the URL were causing trouble. Thanks for looking into it regardless.