File tree 2 files changed +26
-5
lines changed
2 files changed +26
-5
lines changed Original file line number Diff line number Diff line change
1
+ from __future__ import annotations
2
+
1
3
from graphql import DocumentNode , Source , parse
2
4
3
5
4
- def gql (request_string : str ) -> DocumentNode :
5
- """Given a String containing a GraphQL request, parse it into a Document.
6
+ def gql (request_string : str | Source ) -> DocumentNode :
7
+ """Given a string containing a GraphQL request, parse it into a Document.
6
8
7
9
:param request_string: the GraphQL request as a String
8
- :type request_string: str
10
+ :type request_string: str | Source
9
11
:return: a Document which can be later executed or subscribed by a
10
12
:class:`Client <gql.client.Client>`, by an
11
13
:class:`async session <gql.client.AsyncClientSession>` or by a
12
14
:class:`sync session <gql.client.SyncClientSession>`
13
15
14
16
:raises GraphQLError: if a syntax error is encountered.
15
17
"""
16
- source = Source (request_string , "GraphQL request" )
18
+ if isinstance (request_string , Source ):
19
+ source = request_string
20
+ elif isinstance (request_string , str ):
21
+ source = Source (request_string , "GraphQL request" )
22
+ else :
23
+ raise TypeError ("Request must be passed as a string or Source object." )
17
24
return parse (source )
Original file line number Diff line number Diff line change 1
1
import pytest
2
- from graphql import GraphQLError
2
+ from graphql import GraphQLError , Source
3
3
4
4
from gql import Client , gql
5
5
from tests .starwars .schema import StarWarsSchema
@@ -323,3 +323,17 @@ def test_mutation_result(client):
323
323
expected = {"createReview" : {"stars" : 5 , "commentary" : "This is a great movie!" }}
324
324
result = client .execute (query , variable_values = params )
325
325
assert result == expected
326
+
327
+
328
+ def test_query_from_source (client ):
329
+ source = Source ("{ hero { name } }" )
330
+ query = gql (source )
331
+ expected = {"hero" : {"name" : "R2-D2" }}
332
+ result = client .execute (query )
333
+ assert result == expected
334
+
335
+
336
+ def test_already_parsed_query (client ):
337
+ query = gql ("{ hero { name } }" )
338
+ with pytest .raises (TypeError , match = "must be passed as a string" ):
339
+ gql (query )
You can’t perform that action at this time.
0 commit comments