-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add skip and include directive in introspection schema
- Loading branch information
1 parent
2be6aaa
commit d9b85de
Showing
4 changed files
with
123 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
from typing import Dict | ||
|
||
from graphql import GraphQLSchema | ||
from graphql import build_client_schema as build_client_schema_orig | ||
|
||
__all__ = ["build_client_schema"] | ||
|
||
|
||
INCLUDE_DIRECTIVE_JSON = { | ||
"name": "include", | ||
"description": ( | ||
"Directs the executor to include this field or fragment " | ||
"only when the `if` argument is true." | ||
), | ||
"locations": ["FIELD", "FRAGMENT_SPREAD", "INLINE_FRAGMENT"], | ||
"args": [ | ||
{ | ||
"name": "if", | ||
"description": "Included when true.", | ||
"type": { | ||
"kind": "NON_NULL", | ||
"name": "None", | ||
"ofType": {"kind": "SCALAR", "name": "Boolean", "ofType": "None"}, | ||
}, | ||
"defaultValue": "None", | ||
} | ||
], | ||
} | ||
|
||
SKIP_DIRECTIVE_JSON = { | ||
"name": "skip", | ||
"description": ( | ||
"Directs the executor to skip this field or fragment " | ||
"when the `if` argument is true." | ||
), | ||
"locations": ["FIELD", "FRAGMENT_SPREAD", "INLINE_FRAGMENT"], | ||
"args": [ | ||
{ | ||
"name": "if", | ||
"description": "Skipped when true.", | ||
"type": { | ||
"kind": "NON_NULL", | ||
"name": "None", | ||
"ofType": {"kind": "SCALAR", "name": "Boolean", "ofType": "None"}, | ||
}, | ||
"defaultValue": "None", | ||
} | ||
], | ||
} | ||
|
||
|
||
def build_client_schema(introspection: Dict) -> GraphQLSchema: | ||
"""This is an alternative to the graphql-core function | ||
:code:`build_client_schema` but with default include and skip directives | ||
added to the schema to fix | ||
`issue #278 <https://github.com/graphql-python/gql/issues/278>`_ | ||
.. warning:: | ||
This function will be removed once the issue | ||
`graphql-js#3419 <https://github.com/graphql/graphql-js/issues/3419>`_ | ||
has been fixed and ported to graphql-core so don't use it | ||
outside gql. | ||
""" | ||
|
||
directives = introspection["__schema"]["directives"] | ||
|
||
if not any(directive["name"] == "skip" for directive in directives): | ||
directives.append(SKIP_DIRECTIVE_JSON) | ||
|
||
if not any(directive["name"] == "include" for directive in directives): | ||
directives.append(INCLUDE_DIRECTIVE_JSON) | ||
|
||
return build_client_schema_orig(introspection, assume_valid=False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters