Skip to content

Instantly share code, notes, and snippets.

@dopry
Created February 15, 2023 15:13
Show Gist options
  • Save dopry/37c6bee35eec0d28483ad8a29fa793bf to your computer and use it in GitHub Desktop.
Save dopry/37c6bee35eec0d28483ad8a29fa793bf to your computer and use it in GitHub Desktop.
import json
from graphene_django.utils.testing import GraphQLTestCase
class GraphQLIntrospectionTestCase(GraphQLTestCase):
graphql = """
query IntrospectionQuery {
__schema {
queryType { name }
mutationType { name }
subscriptionType { name }
types { ...FullType }
directives {
name
description
locations
args {
...InputValue
}
}
}
}
fragment FullType on __Type {
kind
name
description
fields(includeDeprecated: true) {
name
description
args {
...InputValue
}
type {
...TypeRef
}
isDeprecated
deprecationReason
}
inputFields {
...InputValue
}
interfaces {
...TypeRef
}
enumValues(includeDeprecated: true) {
name
description
isDeprecated
deprecationReason
}
possibleTypes {
...TypeRef
}
}
fragment InputValue on __InputValue {
name
description
type {
...TypeRef
}
defaultValue
}
fragment TypeRef on __Type {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
}
}
}
}
}
}
}
}
"""
def test_introspection(self):
response = self.query(self.graphql, operation_name="IntrospectionQuery")
# This validates the status code and if you get errors
self.assertEqual(response.status_code, 200, response)
# Add some more asserts if you like
content = json.loads(response.content)
self.assertNotIn("errors", list(content.keys()), content)
types = list(map(lambda type: type["name"], content["data"]["__schema"]["types"]))
# print(list(types))
# Pages
self.assertIn("MyPage", types)
self.assertIn("MyReferencePage", types)
self.assertIn("MyServicePage", types)
# Depth 0
self.assertIn("OneColumnBlock", types)
self.assertIn("TwoColumnBlock", types)
self.assertIn("ThreeColumnBlock", types)
self.assertIn("SidebarBlock", types)
# Depth 2
self.assertIn("CardBlock", types)
# Leaf
self.assertIn("MyListBlock", types)
self.assertIn("MyCardBlock", types)
self.assertIn("PageCardBlock", types)
self.assertIn("ScriptBlock", types)
self.assertIn("StyleBlock", types)
# from pprint import pprint
# pprint( content["data"]["__schema"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment