-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphqldomain.py
1032 lines (818 loc) · 34.4 KB
/
graphqldomain.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""A GraphQL domain for Sphinx."""
from typing import (
Dict,
Iterable,
Iterator,
List,
NamedTuple,
Optional,
Sequence,
Tuple,
Type,
)
from docutils import nodes
from docutils.nodes import Element, Node
from docutils.parsers.rst import Directive, directives
from docutils.parsers.rst.states import Inliner
from graphql.language import ast as gql_ast
from graphql.language.parser import Parser
from graphql.language.token_kind import TokenKind
from sphinx import addnodes
from sphinx.addnodes import desc_signature, pending_xref
from sphinx.application import Sphinx
from sphinx.builders import Builder
from sphinx.directives import ObjectDescription
from sphinx.domains import Domain, Index, IndexEntry, ObjType
from sphinx.environment import BuildEnvironment
from sphinx.roles import XRefRole
from sphinx.util.docfields import GroupedField, TypedField
from sphinx.util import logging
from sphinx.util.nodes import make_refnode
from sphinx.util.typing import OptionSpec, TextlikeNode
__version__ = "0.2.0"
LOGGER = logging.getLogger(__name__)
DEFAULT_SCHEMA_NAME = "__gqlschema__"
class ObjectEntry(NamedTuple):
"""Information about a declared object for use in indexing the domain.
These are created in :method:`GQLObject.add_target_and_index`
for use in :class:`GraphQLSchemaIndex`.
"""
docname: str
"""The name of the Sphinx document that the object was declared in."""
node_id: str
"""The identifier of the declared object.
This is the fully qualified name of the type.
They are already guaranteed to be unique
or they would create conflicts in the schema.
"""
class OperationTypeField(TypedField):
def make_field(
self,
types: Dict[str, List[Node]],
domain: str,
items: Tuple, # type: ignore[type-arg]
env: Optional[BuildEnvironment] = None,
inliner: Optional[Inliner] = None,
location: Optional[Element] = None,
) -> nodes.field:
# Sphinx will allow the field to be parsed without a type.
# That's not valid GraphQL, so use the default operation names
# when the operation type is specified without a name.
types.setdefault("query", [nodes.Text("Query")])
types.setdefault("mutation", [nodes.Text("Mutation")])
types.setdefault("subscription", [nodes.Text("Subscription")])
valid_optypes = ("query", "mutation", "subscription")
fieldname = nodes.field_name("", self.label)
bodynode = self.list_type()
for fieldarg, value in items:
if value and value[0].rawsource:
msg = (
f"optype field {fieldarg} has a description, which will be ignored"
)
LOGGER.warning(msg, type="graphqldomain", subtype="invalid_optype")
par = nodes.paragraph()
par += addnodes.literal_strong(fieldarg, fieldarg)
if fieldarg not in valid_optypes:
msg = (
f"Invalid operation type '{fieldarg}', "
f"should be one of: {', '.join(valid_optypes)}"
)
LOGGER.warning(msg, type="graphqldomain", subtype="invalid_optype")
else:
par += nodes.Text(": ")
fieldtype = types[fieldarg]
if len(fieldtype) == 1 and isinstance(fieldtype[0], nodes.Text):
typename = fieldtype[0].astext()
par.extend(
self.make_xrefs(
self.typerolename,
domain,
typename,
addnodes.literal_emphasis,
env=env,
inliner=inliner,
location=location,
)
)
else:
par += fieldtype
bodynode += nodes.list_item("", par)
fieldbody = nodes.field_body("", bodynode)
return nodes.field("", fieldname, fieldbody)
def make_xref(
self,
rolename: str,
domain: str,
target: str,
innernode: Type[TextlikeNode] = nodes.emphasis,
contnode: Optional[Node] = None,
env: Optional[BuildEnvironment] = None,
inliner: Optional[Inliner] = None,
location: Optional[Element] = None,
) -> Node:
result = super().make_xref(
rolename, domain, target, innernode, contnode, env, inliner, location
)
xref = None
if isinstance(result, pending_xref):
xref = result
elif result.children and isinstance(result.children[0], pending_xref):
xref = result.children[0]
if xref and env:
xref["gql:schema"] = env.ref_context.get("gql:schema")
return result
def type_to_xref(
target: str, env: BuildEnvironment, reftype: str = "any"
) -> pending_xref:
"""Create a pending_xref node, attaching schema context if applicable."""
xref = pending_xref(
"",
nodes.Text(target),
refdomain="gql",
reftype=reftype,
reftarget=target,
refspecific=True,
)
xref["gql:schema"] = env.ref_context.get("gql:schema")
return xref
class GQLObject(ObjectDescription[Tuple[str, Optional[str]]]):
"""The base class for any GraphQL type."""
option_spec = {
"noindex": directives.flag,
}
obj_type: str
"""The name of the type.
This is used as a key in the domain data and as the name of the Sphinx object type.
Therefore any value of :attr:``obj_type`` must also exist in
:attr:`GQLDomain.initial_data` and :attr:`GQLDomain.object_types`.
"""
parent_type: Optional[str] = None
def add_target_and_index(
self, name: Tuple[str, Optional[str]], sig: str, signode: desc_signature
) -> None:
node_id = signode["fullname"]
signode["ids"].append(node_id)
if "noindex" not in self.options:
self.env.domaindata["gql"][self.obj_type][
signode["fullname"]
] = ObjectEntry(
self.env.docname,
node_id,
)
def _handle_signature_directives(
self, signode: desc_signature, ast_nodes: Sequence[gql_ast.ConstDirectiveNode]
) -> None:
for directive_node in ast_nodes:
signode += addnodes.desc_sig_space()
signode += addnodes.desc_sig_operator("", "@")
directive_name = directive_node.name.value
signode += type_to_xref(directive_name, self.env, reftype="directive")
self._handle_signature_const_arguments(signode, directive_node.arguments)
def _handle_signature_const_arguments(
self, signode: desc_signature, ast_nodes: Sequence[gql_ast.ConstArgumentNode]
) -> None:
if not ast_nodes:
return
signode += addnodes.desc_sig_operator("", "(")
for i, argument_node in enumerate(ast_nodes):
if i != 0:
signode += addnodes.desc_sig_punctuation("", ",")
signode += addnodes.desc_sig_space()
signode += addnodes.desc_sig_name("", argument_node.name.value)
signode += addnodes.desc_sig_punctuation("", ":")
signode += addnodes.desc_sig_space()
self._handle_signature_literal(signode, argument_node.value)
signode += addnodes.desc_sig_operator("", ")")
def _handle_signature_input_values(
self,
signode: desc_signature,
ast_nodes: Sequence[gql_ast.InputValueDefinitionNode],
) -> None:
if not ast_nodes:
return
signode += addnodes.desc_sig_operator("", "(")
for i, argument_node in enumerate(ast_nodes):
if i != 0:
signode += addnodes.desc_sig_punctuation("", ",")
signode += addnodes.desc_sig_space()
signode += addnodes.desc_sig_name("", argument_node.name.value)
signode += addnodes.desc_sig_punctuation("", ":")
signode += addnodes.desc_sig_space()
self._handle_signature_type_reference(signode, argument_node.type)
self._handle_signature_default_value(signode, argument_node.default_value)
self._handle_signature_directives(signode, argument_node.directives)
signode += addnodes.desc_sig_operator("", ")")
def _handle_signature_default_value(
self, signode: desc_signature, ast_nodes: Optional[gql_ast.ConstValueNode]
) -> None:
if not ast_nodes:
return
signode += addnodes.desc_sig_space()
signode += addnodes.desc_sig_operator("", "=")
signode += addnodes.desc_sig_space()
self._handle_signature_literal(signode, ast_nodes)
def _handle_signature_literal(
self, signode: desc_signature, ast_nodes: Optional[gql_ast.ConstValueNode]
) -> None:
if isinstance(ast_nodes, gql_ast.ListValueNode):
signode += addnodes.desc_sig_operator("", "[")
for i, item_node in enumerate(ast_nodes.values):
if i != 0:
signode += addnodes.desc_sig_punctuation("", ",")
signode += addnodes.desc_sig_space()
self._handle_signature_literal(signode, item_node)
signode += addnodes.desc_sig_operator("", "]")
elif isinstance(ast_nodes, gql_ast.ObjectValueNode):
signode += addnodes.desc_sig_operator("", "{")
for i, field_node in enumerate(ast_nodes.fields):
if i != 0:
signode += addnodes.desc_sig_punctuation("", ",")
signode += addnodes.desc_sig_space()
signode += addnodes.desc_sig_name("", field_node.name.value)
signode += addnodes.desc_sig_punctuation("", ":")
signode += addnodes.desc_sig_space()
self._handle_signature_literal(signode, field_node.value)
signode += addnodes.desc_sig_operator("", "}")
elif isinstance(ast_nodes, (gql_ast.IntValueNode, gql_ast.FloatValueNode)):
signode += addnodes.desc_sig_literal_number("", ast_nodes.value)
elif isinstance(ast_nodes, gql_ast.StringValueNode):
signode += addnodes.desc_sig_operator("", '"')
signode += addnodes.desc_sig_literal_string("", ast_nodes.value)
signode += addnodes.desc_sig_operator("", '"')
elif isinstance(ast_nodes, gql_ast.BooleanValueNode):
signode += addnodes.desc_sig_keyword("", str(ast_nodes.value).lower())
elif isinstance(ast_nodes, gql_ast.NullValueNode):
signode += addnodes.desc_sig_keyword("", "null")
elif isinstance(ast_nodes, gql_ast.EnumValueNode):
signode += addnodes.desc_sig_name("", ast_nodes.value)
# Variable values are a valid literal but not in schemas
else:
raise TypeError(f"Unknown literal node type '{type(ast_nodes)}'")
def _handle_signature_type_reference(
self,
signode: desc_signature,
ast_node: gql_ast.TypeNode,
) -> None:
if isinstance(ast_node, gql_ast.NamedTypeNode):
type_name = ast_node.name.value
signode += type_to_xref(type_name, self.env)
elif isinstance(ast_node, gql_ast.NonNullTypeNode):
self._handle_signature_type_reference(signode, ast_node.type)
signode += addnodes.desc_sig_operator("", "!")
elif isinstance(ast_node, gql_ast.ListTypeNode):
signode += addnodes.desc_sig_operator("", "[")
self._handle_signature_type_reference(signode, ast_node.type)
signode += addnodes.desc_sig_operator("", "]")
else:
raise TypeError(f"Unknown type node '{type(ast_node)}")
def _resolve_names(
self, name: str, signode: desc_signature
) -> Tuple[str, Optional[str]]:
"""Use the parenting of objects to resolve the fullname attribute.
By default an object can only be parented to a schema.
Objects that are parented to something other than a schema should
inherit from :class:`GQLChildObject`.
"""
parent_name = self.env.ref_context.get("gql:schema")
if parent_name:
fullname = f"{parent_name}.{name}"
else:
fullname = name
signode["fullname"] = fullname
return (fullname, parent_name)
class GQLParentObject(GQLObject):
"""A base class for any GraphQL types that can have child entities."""
def before_content(self) -> None:
"""Set the domain context to be this object."""
if self.names:
fullname, _ = self.names[-1]
# GraphQL entities can only have on level of nesting,
# so we only need to set and unset the context
# rather than needing to maintain a stack.
self.env.ref_context[f"gql:{self.obj_type}"] = fullname
def after_content(self) -> None:
"""Unset the domain context."""
self.env.ref_context[f"gql:{self.obj_type}"] = None
class GQLChildObject(GQLObject):
"""A base class for any GraphQL types that only exists as the child of another type.
This class uses the context set by the parent :class:`GQLParentObject`
to correctly prefix the name of this entity with the parent
and tell Sphinx to associate this child with the parent.
"""
parent_type: str
"""The value of the :attr:`obj_type` attribute on the parent object."""
def _resolve_names(
self, name: str, signode: desc_signature
) -> Tuple[str, Optional[str]]:
parent_name = self.env.ref_context.get(f"gql:{self.parent_type}")
if parent_name:
fullname = f"{parent_name}.{name}"
else:
fullname = name
signode["fullname"] = fullname
return (fullname, parent_name)
class GQLField(GQLChildObject):
"""A base class for any field type.
See Also:
https://spec.graphql.org/June2018/#FieldDefinition
"""
doc_field_types = [
GroupedField(
"argument",
label="Arguments",
names=("arg", "argument"),
),
]
def handle_signature(
self, sig: str, signode: desc_signature
) -> Tuple[str, Optional[str]]:
parser = Parser(sig, no_location=True)
parser.expect_token(TokenKind.SOF)
node = parser.parse_field_definition()
parser.expect_token(TokenKind.EOF)
name = node.name.value
signode += addnodes.desc_name(name, name)
self._handle_signature_input_values(signode, node.arguments)
signode += addnodes.desc_sig_operator("", ":")
signode += addnodes.desc_sig_space()
self._handle_signature_type_reference(signode, node.type)
self._handle_signature_directives(signode, node.directives)
return self._resolve_names(name, signode)
class GQLDirective(GQLObject):
"""Represents the definition of a GraphQL Directive.
See also:
https://spec.graphql.org/June2018/#sec-Type-System.Directives
"""
obj_type = "directive"
doc_field_types = [
GroupedField(
"argument",
label="Arguments",
names=("arg", "argument"),
),
]
def handle_signature(
self, sig: str, signode: desc_signature
) -> Tuple[str, Optional[str]]:
# https://spec.graphql.org/June2018/#sec-Type-System.Directives
parser = Parser("directive " + sig, no_location=True)
parser.expect_token(TokenKind.SOF)
node = parser.parse_directive_definition()
parser.expect_token(TokenKind.EOF)
prefix = [nodes.Text("directive"), addnodes.desc_sig_space()]
signode += addnodes.desc_annotation(str(prefix), "", *prefix)
signode += addnodes.desc_sig_space()
signode += addnodes.desc_sig_operator("", "@")
name = node.name.value
signode += addnodes.desc_name(name, name)
self._handle_signature_input_values(signode, node.arguments)
signode += addnodes.desc_sig_space()
signode += addnodes.desc_sig_keyword("", "on")
signode += addnodes.desc_sig_space()
for i, location in enumerate(node.locations):
if i != 0:
signode += addnodes.desc_sig_space()
signode += addnodes.desc_sig_operator("", "|")
signode += addnodes.desc_sig_space()
signode += nodes.Text(location.value)
return self._resolve_names(name, signode)
class GQLEnum(GQLParentObject):
"""Represents the definition of a GraphQL Enum.
See also:
https://spec.graphql.org/June2018/#sec-Enums
"""
obj_type = "enum"
def handle_signature(
self, sig: str, signode: desc_signature
) -> Tuple[str, Optional[str]]:
# https://spec.graphql.org/June2018/#sec-Interfaces
parser = Parser("enum " + sig, no_location=True)
parser.expect_token(TokenKind.SOF)
node = parser.parse_enum_type_definition()
parser.expect_token(TokenKind.EOF)
prefix = [nodes.Text("enum"), addnodes.desc_sig_space()]
signode += addnodes.desc_annotation(str(prefix), "", *prefix)
name = node.name.value
signode += addnodes.desc_name(name, name)
self._handle_signature_directives(signode, node.directives)
return self._resolve_names(name, signode)
class GQLEnumValue(GQLChildObject):
"""Represents the definition of a value on a GraphQL Enum.
See also:
https://spec.graphql.org/June2018/#sec-Enums
"""
obj_type = "enum:value"
parent_type = "enum"
def handle_signature(
self, sig: str, signode: desc_signature
) -> Tuple[str, Optional[str]]:
# https://spec.graphql.org/June2018/#EnumValueDefinition
parser = Parser(sig, no_location=True)
parser.expect_token(TokenKind.SOF)
node = parser.parse_enum_value_definition()
parser.expect_token(TokenKind.EOF)
name = node.name.value
signode += addnodes.desc_name(name, name)
self._handle_signature_directives(signode, node.directives)
return self._resolve_names(name, signode)
class GQLInput(GQLParentObject):
"""Represents the definition of a GraphQL Input Object.
See also:
https://spec.graphql.org/June2018/#sec-Input-Objects
"""
obj_type = "input"
def handle_signature(
self, sig: str, signode: desc_signature
) -> Tuple[str, Optional[str]]:
# https://spec.graphql.org/June2018/#sec-Input-Objects
parser = Parser("input " + sig, no_location=True)
parser.expect_token(TokenKind.SOF)
node = parser.parse_input_object_type_definition()
parser.expect_token(TokenKind.EOF)
prefix = [nodes.Text("input"), addnodes.desc_sig_space()]
signode += addnodes.desc_annotation(str(prefix), "", *prefix)
name = node.name.value
signode += addnodes.desc_name(name, name)
self._handle_signature_directives(signode, node.directives)
return self._resolve_names(name, signode)
class GQLInputField(GQLChildObject):
"""Represents the definition of a field on a GraphQL Input Object.
See also:
https://spec.graphql.org/June2018/#sec-Input-Objects
"""
obj_type = "input:field"
parent_type = "input"
def handle_signature(
self, sig: str, signode: desc_signature
) -> Tuple[str, Optional[str]]:
parser = Parser(sig, no_location=True)
parser.expect_token(TokenKind.SOF)
node = parser.parse_input_value_def()
parser.expect_token(TokenKind.EOF)
name = node.name.value
signode += addnodes.desc_name(name, name)
signode += addnodes.desc_sig_operator("", ":")
signode += addnodes.desc_sig_space()
self._handle_signature_type_reference(signode, node.type)
self._handle_signature_default_value(signode, node.default_value)
self._handle_signature_directives(signode, node.directives)
return self._resolve_names(name, signode)
class GQLInterface(GQLParentObject):
"""Represents the definition of a GraphQL Interface.
See also:
https://spec.graphql.org/June2018/#sec-Interfaces
"""
obj_type = "interface"
def handle_signature(
self, sig: str, signode: desc_signature
) -> Tuple[str, Optional[str]]:
# https://spec.graphql.org/June2018/#sec-Interfaces
parser = Parser("interface " + sig, no_location=True)
parser.expect_token(TokenKind.SOF)
node = parser.parse_interface_type_definition()
parser.expect_token(TokenKind.EOF)
prefix = [nodes.Text("interface"), addnodes.desc_sig_space()]
signode += addnodes.desc_annotation(str(prefix), "", *prefix)
name = node.name.value
signode += addnodes.desc_name(name, name)
self._handle_signature_directives(signode, node.directives)
return self._resolve_names(name, signode)
class GQLInterfaceField(GQLField):
"""Represents the definition of a field on a GraphQL Interface.
See also:
https://spec.graphql.org/June2018/#sec-Interfaces
"""
obj_type = "interface:field"
parent_type = "interface"
class GQLScalar(GQLObject):
"""Represents the definition of a GraphQL Scalar.
See also:
https://spec.graphql.org/June2018/#sec-Scalars
"""
obj_type = "scalar"
def handle_signature(
self, sig: str, signode: desc_signature
) -> Tuple[str, Optional[str]]:
parser = Parser("scalar " + sig, no_location=True)
parser.expect_token(TokenKind.SOF)
node = parser.parse_scalar_type_definition()
parser.expect_token(TokenKind.EOF)
prefix = [nodes.Text("scalar"), addnodes.desc_sig_space()]
signode += addnodes.desc_annotation(str(prefix), "", *prefix)
name = node.name.value
signode += addnodes.desc_name(name, name)
self._handle_signature_directives(signode, node.directives)
return self._resolve_names(name, signode)
class GQLSchema(GQLParentObject):
"""Represents the definition of a GraphQL Schema.
See also:
https://spec.graphql.org/June2018/#sec-Schema
"""
obj_type = "schema"
option_spec: OptionSpec = {
"noindex": directives.flag,
"name": directives.unchanged,
}
required_arguments = 0
optional_arguments = 1
doc_field_types = [
OperationTypeField(
"operationtypes",
label="Operation types",
names=("optype",),
typerolename="type",
typenames=(),
),
]
def get_signatures(self) -> List[str]:
if self.arguments:
return super().get_signatures()
return [""]
def handle_signature(
self, sig: str, signode: desc_signature
) -> Tuple[str, Optional[str]]:
prefix = [nodes.Text("schema"), addnodes.desc_sig_space()]
signode += addnodes.desc_annotation(str(prefix), "", *prefix)
directives = sig
if directives:
parser = Parser(directives, no_location=True)
parser.expect_token(TokenKind.SOF)
directive_nodes = parser.parse_const_directives()
parser.expect_token(TokenKind.EOF)
self._handle_signature_directives(signode, directive_nodes)
name = self.options.get("name", DEFAULT_SCHEMA_NAME)
signode["fullname"] = name
return (name, None)
class GQLType(GQLParentObject):
"""Represents the definition of a GraphQL Type Object.
See also:
https://spec.graphql.org/June2018/#sec-Objects
"""
obj_type = "type"
def handle_signature(
self, sig: str, signode: desc_signature
) -> Tuple[str, Optional[str]]:
# https://spec.graphql.org/June2018/#sec-Objects
parser = Parser("type " + sig, no_location=True)
parser.expect_token(TokenKind.SOF)
node = parser.parse_object_type_definition()
parser.expect_token(TokenKind.EOF)
prefix = [nodes.Text("type"), addnodes.desc_sig_space()]
signode += addnodes.desc_annotation(str(prefix), "", *prefix)
name = node.name.value
signode += addnodes.desc_name(name, name)
interfaces = node.interfaces
if interfaces:
signode += addnodes.desc_sig_space()
signode += addnodes.desc_sig_keyword("", "implements")
signode += addnodes.desc_sig_space()
for i, interface in enumerate(interfaces):
if i != 0:
signode += addnodes.desc_sig_space()
signode += addnodes.desc_sig_operator("", "&")
signode += addnodes.desc_sig_space()
interface_type = interface.name.value
signode += type_to_xref(interface_type, self.env, reftype="interface")
self._handle_signature_directives(signode, node.directives)
return self._resolve_names(name, signode)
class GQLTypeField(GQLField):
"""Represents the definition of a field on a GraphQL Type Object.
See also:
https://spec.graphql.org/June2018/#sec-Objects
"""
obj_type = "type:field"
parent_type = "type"
class GQLUnion(GQLObject):
"""Represents the definition of a GraphQL Union.
See also:
https://spec.graphql.org/June2018/#sec-Unions
"""
obj_type = "union"
def handle_signature(
self, sig: str, signode: desc_signature
) -> Tuple[str, Optional[str]]:
parser = Parser("union " + sig, no_location=True)
parser.expect_token(TokenKind.SOF)
node = parser.parse_union_type_definition()
parser.expect_token(TokenKind.EOF)
prefix = [nodes.Text("union"), addnodes.desc_sig_space()]
signode += addnodes.desc_annotation(str(prefix), "", *prefix)
name = node.name.value
signode += addnodes.desc_name(name, name)
self._handle_signature_directives(signode, node.directives)
member_nodes = node.types
if member_nodes:
for i, member_node in enumerate(member_nodes):
if i == 0:
signode += addnodes.desc_sig_space()
signode += addnodes.desc_sig_operator("", "=")
signode += addnodes.desc_sig_space()
else:
signode += addnodes.desc_sig_space()
signode += addnodes.desc_sig_operator("", "|")
signode += addnodes.desc_sig_space()
member_type = member_node.name.value
signode += type_to_xref(member_type, self.env)
return self._resolve_names(name, signode)
class GraphQLSchemaIndex(Index):
"""The index generator for the GraphQL domain."""
name = "index"
localname = "GraphQL Object Index"
shortname = "index"
def _anchor(self, fullname: str) -> str:
return fullname.lower().split("(", 1)[0]
def generate(
self, docnames: Optional[Iterable[str]] = None
) -> Tuple[List[Tuple[str, List[IndexEntry]]], bool]:
content: Dict[str, List[IndexEntry]] = {}
for fullname, _, objtype, docname, node_id, __ in sorted(
self.domain.get_objects()
):
# Only index top level objects to eliminate name collisions
if objtype in ("enumvalue", "field"):
continue
name = fullname
subtype = 0 # Always zero because we don't index child types
anchor = node_id
extra = ""
qualifier = ""
descr = ""
entry = IndexEntry(name, subtype, docname, anchor, extra, qualifier, descr)
entries = content.setdefault(fullname[0].lower(), [])
entries.append(entry)
sorted_content = sorted(content.items())
return (sorted_content, True)
class GQLXRefRole(XRefRole):
def process_link(
self,
env: BuildEnvironment,
refnode: Element,
has_explicit_title: bool,
title: str,
target: str,
) -> Tuple[str, str]:
title, target = super().process_link(
env, refnode, has_explicit_title, title, target
)
if not has_explicit_title and title.startswith(f"{DEFAULT_SCHEMA_NAME}."):
title = title.split(".", 1)[-1]
refnode["gql:schema"] = env.ref_context.get("gql:schema")
return (title, target)
class GraphQLDomain(Domain):
"""The definition of the GraphQL Sphinx Domain."""
name = "gql"
label = "GraphQL"
object_types: Dict[str, ObjType] = {
"directive": ObjType("directive", "directive"),
"enum": ObjType("enum", "enum"),
"enum:value": ObjType("enum-value", "enum"),
"input": ObjType("input", "input"),
"input:field": ObjType("input-field", "input"),
"interface": ObjType("interface", "interface"),
"interface:field": ObjType("interface-field", "interface"),
"scalar": ObjType("scalar", "scalar"),
"schema": ObjType("schema", "schema"),
"type": ObjType("type", "type"),
"type:field": ObjType("type-field", "type"),
"union": ObjType("union", "union"),
}
directives: Dict[str, Type[Directive]] = {
"directive": GQLDirective,
"enum": GQLEnum,
"enum:value": GQLEnumValue,
"input": GQLInput,
"input:field": GQLInputField,
"interface": GQLInterface,
"interface:field": GQLInterfaceField,
"scalar": GQLScalar,
"schema": GQLSchema,
"type": GQLType,
"type:field": GQLTypeField,
"union": GQLUnion,
}
# mypy complains because many types are allowed other than XRefRole.
# However this class isn't going to be used for subclassing
# so violating variance rules is acceptable.
roles: Dict[str, XRefRole] = { # type: ignore[assignment]
"directive": GQLXRefRole(),
"enum": GQLXRefRole(),
"enum:value": GQLXRefRole(),
"input": GQLXRefRole(),
"input:field": GQLXRefRole(),
"interface": GQLXRefRole(),
"interface:field": GQLXRefRole(),
"scalar": GQLXRefRole(),
"schema": GQLXRefRole(),
"type": GQLXRefRole(),
"type:field": GQLXRefRole(),
"union": GQLXRefRole(),
}
initial_data: Dict[str, Dict[str, ObjectEntry]] = {
"directive": {},
"enum": {},
"enum:value": {},
"input": {},
"input:field": {},
"interface": {},
"interface:field": {},
"scalar": {},
"schema": {},
"type": {},
"type:field": {},
"union": {},
}
indices = [GraphQLSchemaIndex]
def clear_doc(self, docname: str) -> None:
for object_type in self.object_types:
type_data = self.data[object_type]
for fullname, entry in list(type_data.items()):
if entry.docname == docname:
del type_data[fullname]
def resolve_xref(
self,
env: BuildEnvironment,
fromdocname: str,
builder: Builder,
typ: str,
target: str,
node: pending_xref,
contnode: Element,
) -> Optional[Element]:
patterns = [target]
# If the xref was created in the context of the schema,
# allow references to other names in the schema
# without needing to specify the name of the schema.
schema_name = node.get("gql:schema")
if schema_name and not target.startswith(f"{schema_name}."):
patterns.append(f"{schema_name}.{target}")
# If the xref was created outside the context of the schema,
# allow references to names in the schema with the default name
# without needing to use the name of the schema.
if not schema_name and not target.startswith(f"{DEFAULT_SCHEMA_NAME}."):
patterns.append(f"{DEFAULT_SCHEMA_NAME}.{target}")
for object_type in self.object_types:
type_data = self.data[object_type]
for fullname, entry in list(type_data.items()):
if fullname in patterns:
# mypy error caused by incomplete docutils type annotations:
# https://github.com/python/typeshed/issues/1269
return make_refnode( # type: ignore[no-any-return]
builder,
fromdocname,
entry.docname,
entry.node_id,
[contnode],
fullname,
)
return None
def resolve_any_xref(
self,
env: BuildEnvironment,
fromdocname: str,
builder: Builder,
target: str,
node: pending_xref,
contnode: Element,
) -> List[Tuple[str, Element]]:
"""Resolve the pending_xref ``node`` with the given ``target``.
The reference comes from an "any" or similar role,
which means that Sphinx doesn't know the type.
For now we don't resolve "any" xref nodes.
"""
return []
def get_objects(self) -> Iterator[Tuple[str, str, str, str, str, int]]:
for object_type in self.object_types:
type_data = self.data[object_type]
for fullname, entry in list(type_data.items()):
yield (
fullname,
fullname,
object_type,