Python graphql.execute() Examples

The following are 7 code examples of graphql.execute(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module graphql , or try the search function .
Example #1
Source File: schema_utils.py    From graphene-tornado with MIT License 6 votes vote down vote up
def generate_schema_hash(schema: GraphQLSchema) -> str:
    """
    Generates a stable hash of the current schema using an introspection query.
    """
    ast = parse(introspection_query)
    result = cast(ExecutionResult, execute(schema, ast))

    if result and not result.data:
        raise GraphQLError("Unable to generate server introspection document")

    schema = result.data["__schema"]
    # It's important that we perform a deterministic stringification here
    # since, depending on changes in the underlying `graphql-core` execution
    # layer, varying orders of the properties in the introspection
    stringified_schema = stringify(schema).encode("utf-8")
    return hashlib.sha512(stringified_schema).hexdigest() 
Example #2
Source File: local_schema.py    From gql with MIT License 5 votes vote down vote up
def execute(
        self, document: DocumentNode, *args, **kwargs,
    ) -> ExecutionResult:
        """Execute the provided document AST for on a local GraphQL Schema.
        """

        result_or_awaitable = execute(self.schema, document, *args, **kwargs)

        execution_result: ExecutionResult

        if isawaitable(result_or_awaitable):
            result_or_awaitable = cast(Awaitable[ExecutionResult], result_or_awaitable)
            execution_result = await result_or_awaitable
        else:
            result_or_awaitable = cast(ExecutionResult, result_or_awaitable)
            execution_result = result_or_awaitable

        return execution_result 
Example #3
Source File: views.py    From graphene-django-extras with MIT License 5 votes vote down vote up
def execute(self, *args, **kwargs):
        operation_ast = get_operation_ast(args[0])

        if operation_ast and operation_ast.operation == "subscription":
            result = subscribe(self.schema, *args, **kwargs)
            if isinstance(result, Observable):
                a = []
                result.subscribe(lambda x: a.append(x))
                if len(a) > 0:
                    result = a[-1]
            return result

        return execute(self.schema, *args, **kwargs) 
Example #4
Source File: test_introspection_from_schema.py    From graphql-core with MIT License 5 votes vote down vote up
def test_execute_introspection_query(benchmark, big_schema_sdl):  # noqa: F811
    schema = build_schema(big_schema_sdl, assume_valid=True)
    document = parse(get_introspection_query())
    result = benchmark(lambda: execute(schema=schema, document=document))
    assert result.errors is None 
Example #5
Source File: __init__.py    From graphql-django-view with MIT License 5 votes vote down vote up
def execute(self, *args, **kwargs):
        return execute(self.schema, *args, **kwargs) 
Example #6
Source File: __init__.py    From graphql-django-view with MIT License 5 votes vote down vote up
def execute_graphql_request(self, request):
        query, variables, operation_name = self.get_graphql_params(request, self.parse_body(request))

        if not query:
            raise HttpError(HttpResponseBadRequest('Must provide query string.'))

        source = Source(query, name='GraphQL request')

        try:
            document_ast = parse(source)
            validation_errors = validate(self.schema, document_ast)
            if validation_errors:
                return ExecutionResult(
                    errors=validation_errors,
                    invalid=True,
                )
        except Exception as e:
            return ExecutionResult(errors=[e], invalid=True)

        if request.method.lower() == 'get':
            operation_ast = get_operation_ast(document_ast, operation_name)
            if operation_ast and operation_ast.operation != 'query':
                raise HttpError(HttpResponseNotAllowed(
                    ['POST'], 'Can only perform a {} operation from a POST request.'.format(operation_ast.operation)
                ))

        try:
            return self.execute(
                document_ast,
                root_value=self.get_root_value(request),
                variable_values=variables,
                operation_name=operation_name,
                context_value=self.get_context(request),
                executor=self.executor,
            )
        except Exception as e:
            return ExecutionResult(errors=[e], invalid=True) 
Example #7
Source File: tornado_graphql_handler.py    From graphene-tornado with MIT License 5 votes vote down vote up
def execute(
        self, *args, **kwargs
    ) -> Union[Awaitable[ExecutionResult], ExecutionResult]:
        return execute(self.schema.graphql_schema, *args, **kwargs)