graphql-python / flask-graphql

Adds GraphQL support to your Flask application.
MIT License
1.33k stars 140 forks source link

YAML as a result format instead of JSON #50

Closed alexander-myltsev closed 6 years ago

alexander-myltsev commented 6 years ago

Is there an easy way to override the serializer to return YAML instead of JSON?

rscarrera27 commented 6 years ago

How about this script

import yaml
yaml.dump(<json obj>)
alexander-myltsev commented 6 years ago

@devArtoria I know how to dump YAML :) Whal elements of Flask-GraphQL should I override to paste your code in?

rscarrera27 commented 6 years ago

GraphQLView is works as view function for graphql endpoint, and GraphQLView's dispatch_request is dispatch request. it uses get_response to make response with json_encode. if you want GraphQLView return YAML, just edit get_response to user your own yaml_encode

 def get_response(self, request, data, show_graphiql=False):
        query, variables, operation_name, id = self.get_graphql_params(request, data)

        execution_result = self.execute_graphql_request(
            data,
            query,
            variables,
            operation_name,
            show_graphiql
        )

        status_code = 200
        if execution_result:
            response = {}

        ......

            result = self.json_encode(request, response, show_graphiql) <<<<< THIS PART
        else:
            result = None    

I hope my answer is helpful.

rscarrera27 commented 6 years ago

@alexander-myltsev Could I close this?

alexander-myltsev commented 6 years ago

Yes, thanks.