mirror of
				https://github.com/graphql-python/graphene-django.git
				synced 2025-11-04 18:08:01 +03:00 
			
		
		
		
	Add headers support to GraphiQL (#1016)
Co-authored-by: Jonathan Kim <jkimbo@gmail.com>
This commit is contained in:
		
							parent
							
								
									2308965658
								
							
						
					
					
						commit
						55769e814f
					
				| 
						 | 
					@ -186,3 +186,24 @@ Default: ``None``
 | 
				
			||||||
   GRAPHENE = {
 | 
					   GRAPHENE = {
 | 
				
			||||||
      'SUBSCRIPTION_PATH': "/ws/graphql"
 | 
					      'SUBSCRIPTION_PATH': "/ws/graphql"
 | 
				
			||||||
   }
 | 
					   }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					``GRAPHIQL_HEADER_EDITOR_ENABLED``
 | 
				
			||||||
 | 
					---------------------
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					GraphiQL starting from version 1.0.0 allows setting custom headers in similar fashion to query variables.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Set to ``False`` if you want to disable GraphiQL headers editor tab for some reason.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					This setting is passed to ``headerEditorEnabled`` GraphiQL options, for details refer to GraphiQLDocs_.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.. _GraphiQLDocs: https://github.com/graphql/graphiql/tree/main/packages/graphiql#options
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Default: ``True``
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.. code:: python
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					   GRAPHENE = {
 | 
				
			||||||
 | 
					      'GRAPHIQL_HEADER_EDITOR_ENABLED': True,
 | 
				
			||||||
 | 
					   }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -41,6 +41,10 @@ DEFAULTS = {
 | 
				
			||||||
    "DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME": None,
 | 
					    "DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME": None,
 | 
				
			||||||
    # Use a separate path for handling subscriptions.
 | 
					    # Use a separate path for handling subscriptions.
 | 
				
			||||||
    "SUBSCRIPTION_PATH": None,
 | 
					    "SUBSCRIPTION_PATH": None,
 | 
				
			||||||
 | 
					    # By default GraphiQL headers editor tab is enabled, set to False to hide it
 | 
				
			||||||
 | 
					    # This sets headerEditorEnabled GraphiQL option, for details go to
 | 
				
			||||||
 | 
					    # https://github.com/graphql/graphiql/tree/main/packages/graphiql#options
 | 
				
			||||||
 | 
					    "GRAPHIQL_HEADER_EDITOR_ENABLED": True,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if settings.DEBUG:
 | 
					if settings.DEBUG:
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -61,13 +61,15 @@
 | 
				
			||||||
  var fetchURL = locationQuery(otherParams);
 | 
					  var fetchURL = locationQuery(otherParams);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // Defines a GraphQL fetcher using the fetch API.
 | 
					  // Defines a GraphQL fetcher using the fetch API.
 | 
				
			||||||
  function httpClient(graphQLParams) {
 | 
					  function httpClient(graphQLParams, opts) {
 | 
				
			||||||
    var headers = {
 | 
					    if (typeof opts === 'undefined') {
 | 
				
			||||||
      Accept: "application/json",
 | 
					      opts = {};
 | 
				
			||||||
      "Content-Type": "application/json",
 | 
					    }
 | 
				
			||||||
    };
 | 
					    var headers = opts.headers || {};
 | 
				
			||||||
 | 
					    headers['Accept'] = headers['Accept'] || 'application/json';
 | 
				
			||||||
 | 
					    headers['Content-Type'] = headers['Content-Type'] || 'application/json';
 | 
				
			||||||
    if (csrftoken) {
 | 
					    if (csrftoken) {
 | 
				
			||||||
      headers["X-CSRFToken"] = csrftoken;
 | 
					      headers['X-CSRFToken'] = csrftoken
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    return fetch(fetchURL, {
 | 
					    return fetch(fetchURL, {
 | 
				
			||||||
      method: "post",
 | 
					      method: "post",
 | 
				
			||||||
| 
						 | 
					@ -108,7 +110,7 @@
 | 
				
			||||||
  var activeSubscription = null;
 | 
					  var activeSubscription = null;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // Define a GraphQL fetcher that can intelligently route queries based on the operation type.
 | 
					  // Define a GraphQL fetcher that can intelligently route queries based on the operation type.
 | 
				
			||||||
  function graphQLFetcher(graphQLParams) {
 | 
					  function graphQLFetcher(graphQLParams, opts) {
 | 
				
			||||||
    var operationType = getOperationType(graphQLParams);
 | 
					    var operationType = getOperationType(graphQLParams);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // If we're about to execute a new operation, and we have an active subscription,
 | 
					    // If we're about to execute a new operation, and we have an active subscription,
 | 
				
			||||||
| 
						 | 
					@ -126,7 +128,7 @@
 | 
				
			||||||
        },
 | 
					        },
 | 
				
			||||||
      };
 | 
					      };
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
      return httpClient(graphQLParams);
 | 
					      return httpClient(graphQLParams, opts);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -173,6 +175,7 @@
 | 
				
			||||||
    onEditQuery: onEditQuery,
 | 
					    onEditQuery: onEditQuery,
 | 
				
			||||||
    onEditVariables: onEditVariables,
 | 
					    onEditVariables: onEditVariables,
 | 
				
			||||||
    onEditOperationName: onEditOperationName,
 | 
					    onEditOperationName: onEditOperationName,
 | 
				
			||||||
 | 
					    headerEditorEnabled: GRAPHENE_SETTINGS.graphiqlHeaderEditorEnabled,
 | 
				
			||||||
    query: parameters.query,
 | 
					    query: parameters.query,
 | 
				
			||||||
  };
 | 
					  };
 | 
				
			||||||
  if (parameters.variables) {
 | 
					  if (parameters.variables) {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -45,6 +45,7 @@ add "&raw" to the end of the URL within a browser.
 | 
				
			||||||
    {% if subscription_path %}
 | 
					    {% if subscription_path %}
 | 
				
			||||||
      subscriptionPath: "{{subscription_path}}",
 | 
					      subscriptionPath: "{{subscription_path}}",
 | 
				
			||||||
    {% endif %}
 | 
					    {% endif %}
 | 
				
			||||||
 | 
					      graphiqlHeaderEditorEnabled: {{ graphiql_header_editor_enabled|yesno:"true,false" }},
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
  </script>
 | 
					  </script>
 | 
				
			||||||
  <script src="{% static 'graphene_django/graphiql.js' %}"></script>
 | 
					  <script src="{% static 'graphene_django/graphiql.js' %}"></script>
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -167,6 +167,8 @@ class GraphQLView(View):
 | 
				
			||||||
                    subscriptions_transport_ws_sri=self.subscriptions_transport_ws_sri,
 | 
					                    subscriptions_transport_ws_sri=self.subscriptions_transport_ws_sri,
 | 
				
			||||||
                    # The SUBSCRIPTION_PATH setting.
 | 
					                    # The SUBSCRIPTION_PATH setting.
 | 
				
			||||||
                    subscription_path=self.subscription_path,
 | 
					                    subscription_path=self.subscription_path,
 | 
				
			||||||
 | 
					                    # GraphiQL headers tab,
 | 
				
			||||||
 | 
					                    graphiql_header_editor_enabled=graphene_settings.GRAPHIQL_HEADER_EDITOR_ENABLED,
 | 
				
			||||||
                )
 | 
					                )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if self.batch:
 | 
					            if self.batch:
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user