def test_define_enum_type(): gql = Schema() class EpisodeEnum(gql.EnumType): """One of the films in the Star Wars Trilogy""" __typename__ = 'Episode' NEWHOPE = gql.EnumValue(4, description='Released in 1977.') EMPIRE = gql.EnumValue(5, description='Released in 1980.') JEDI = gql.EnumValue(6, description='Released in 1983.') class QueryRoot(gql.QueryRoot): episode = gql.Field(EpisodeEnum) result = gql.execute('''{ type: __type(name: "Episode") { name description enumValues { name, description } } }''') assert not result.errors assert sort_lists(result.data) == sort_lists({ "type": { "name": "Episode", "description": "One of the films in the Star Wars Trilogy", "enumValues": [ {"name": "NEWHOPE", "description": "Released in 1977."}, {"name": "EMPIRE", "description": "Released in 1980."}, {"name": "JEDI", "description": "Released in 1983."}, ] } })
def test_define_object_type(): gql = Schema() class EpisodeEnum(gql.EnumType): """One of the films in the Star Wars Trilogy""" __typename__ = "Episode" NEWHOPE = gql.EnumValue(4, description="Released in 1977.") EMPIRE = gql.EnumValue(5, description="Released in 1980.") JEDI = gql.EnumValue(6, description="Released in 1983.") class QueryRoot(gql.QueryRoot): """description""" byPublicType = gql.Field(EpisodeEnum) byName = gql.Field("Episode") byInternalType = gql.Field(gql.String) byPublicTypeWrapped = gql.Field(gql.List(EpisodeEnum)) byNameWrapped = gql.Field(gql.List("Episode")) byInternalTypeWrapped = gql.Field(gql.List(gql.String)) result = gql.execute( """{ type: __type(name: "QueryRoot") { name description fields { name, type { ...TypeRef } } } } fragment TypeRef on __Type { kind, name ofType { kind, name ofType { kind, name, ofType } } }""" ) assert not result.errors episode_type = {"kind": "ENUM", "name": "Episode", "ofType": None} string_type = {"kind": "SCALAR", "name": "String", "ofType": None} assert sort_lists(result.data) == sort_lists( { "type": { "name": "QueryRoot", "description": "description", "fields": [ {"name": "byPublicType", "type": episode_type}, {"name": "byName", "type": episode_type}, {"name": "byInternalType", "type": string_type}, {"name": "byPublicTypeWrapped", "type": {"kind": "LIST", "name": None, "ofType": episode_type}}, {"name": "byNameWrapped", "type": {"kind": "LIST", "name": None, "ofType": episode_type}}, {"name": "byInternalTypeWrapped", "type": {"kind": "LIST", "name": None, "ofType": string_type}}, ], } } )
def test_field_arguments(): gql = Schema() class A(gql.ObjectType): pass class QueryRoot(gql.QueryRoot): field = gql.Field(gql.String, { 'byPublicType': gql.Argument(A), 'byName': gql.Argument('A'), 'byInternalType': gql.Argument(gql.String), 'byPublicTypeWrapped': gql.Argument(gql.List(A)), 'byNameWrapped': gql.Argument(gql.List('A')), 'byInternalTypeWrapped': gql.Argument(gql.List(gql.String)), }) result = gql.execute('''{ type: __type(name: "QueryRoot") { fields { args { name type { ...TypeRef } } } } } fragment TypeRef on __Type { kind, name ofType { kind, name ofType { kind, name } } }''') assert not result.errors a_type = {"kind": "OBJECT", "name": "A", "ofType": None} string_type = {"kind": "SCALAR", "name": "String", "ofType": None} assert sort_lists(result.data) == sort_lists({ "type": { "fields": [{"args": [ {"name": "byPublicType", "type": a_type}, {"name": "byName", "type": a_type}, {"name": "byInternalType", "type": string_type}, {"name": "byPublicTypeWrapped", "type": {"kind": "LIST", "name": None, "ofType": a_type}}, {"name": "byNameWrapped", "type": {"kind": "LIST", "name": None, "ofType": a_type}}, {"name": "byInternalTypeWrapped", "type": {"kind": "LIST", "name": None, "ofType": string_type}}, ]}] } })
def test_define_enum_type(): gql = Schema() class EpisodeEnum(gql.EnumType): """One of the films in the Star Wars Trilogy""" __typename__ = 'Episode' NEWHOPE = gql.EnumValue(4, description='Released in 1977.') EMPIRE = gql.EnumValue(5, description='Released in 1980.') JEDI = gql.EnumValue(6, description='Released in 1983.') class QueryRoot(gql.QueryRoot): episode = gql.Field(EpisodeEnum) result = gql.execute('''{ type: __type(name: "Episode") { name description enumValues { name, description } } }''') assert not result.errors assert sort_lists(result.data) == sort_lists({ "type": { "name": "Episode", "description": "One of the films in the Star Wars Trilogy", "enumValues": [ { "name": "NEWHOPE", "description": "Released in 1977." }, { "name": "EMPIRE", "description": "Released in 1980." }, { "name": "JEDI", "description": "Released in 1983." }, ] } })
def test_define_union_type(): gql = Schema() class A(gql.ObjectType): pass class B(gql.ObjectType): pass class UnionType(gql.UnionType): """description""" __typename__ = 'Union' types = [A, B] class QueryRoot(gql.QueryRoot): union = gql.Field(UnionType) result = gql.execute('''{ type: __type(name: "Union") { kind name description possibleTypes { name } } }''') assert not result.errors assert sort_lists(result.data) == sort_lists({ "type": { "name": "Union", "description": "description", "kind": "UNION", "possibleTypes": [ { "name": "A" }, { "name": "B" }, ] } })
def test_define_union_type(): gql = Schema() class A(gql.ObjectType): pass class B(gql.ObjectType): pass class UnionType(gql.UnionType): """description""" __typename__ = "Union" types = [A, B] class QueryRoot(gql.QueryRoot): union = gql.Field(UnionType) result = gql.execute( """{ type: __type(name: "Union") { kind name description possibleTypes { name } } }""" ) assert not result.errors assert sort_lists(result.data) == sort_lists( { "type": { "name": "Union", "description": "description", "kind": "UNION", "possibleTypes": [{"name": "A"}, {"name": "B"}], } } )
def test_define_object_type(): gql = Schema() class EpisodeEnum(gql.EnumType): """One of the films in the Star Wars Trilogy""" __typename__ = 'Episode' NEWHOPE = gql.EnumValue(4, description='Released in 1977.') EMPIRE = gql.EnumValue(5, description='Released in 1980.') JEDI = gql.EnumValue(6, description='Released in 1983.') class QueryRoot(gql.QueryRoot): """description""" byPublicType = gql.Field(EpisodeEnum) byName = gql.Field('Episode') byInternalType = gql.Field(gql.String) byPublicTypeWrapped = gql.Field(gql.List(EpisodeEnum)) byNameWrapped = gql.Field(gql.List('Episode')) byInternalTypeWrapped = gql.Field(gql.List(gql.String)) result = gql.execute('''{ type: __type(name: "QueryRoot") { name description fields { name, type { ...TypeRef } } } } fragment TypeRef on __Type { kind, name ofType { kind, name ofType { kind, name } } }''') assert not result.errors episode_type = {"kind": "ENUM", "name": "Episode", "ofType": None} string_type = {"kind": "SCALAR", "name": "String", "ofType": None} assert sort_lists(result.data) == sort_lists({ "type": { "name": "QueryRoot", "description": "description", "fields": [ { "name": "byPublicType", "type": episode_type }, { "name": "byName", "type": episode_type }, { "name": "byInternalType", "type": string_type }, { "name": "byPublicTypeWrapped", "type": { "kind": "LIST", "name": None, "ofType": episode_type } }, { "name": "byNameWrapped", "type": { "kind": "LIST", "name": None, "ofType": episode_type } }, { "name": "byInternalTypeWrapped", "type": { "kind": "LIST", "name": None, "ofType": string_type } }, ] } })
def test_field_arguments(): gql = Schema() class A(gql.ObjectType): pass class QueryRoot(gql.QueryRoot): field = gql.Field( gql.String, { 'byPublicType': gql.Argument(A), 'byName': gql.Argument('A'), 'byInternalType': gql.Argument(gql.String), 'byPublicTypeWrapped': gql.Argument(gql.List(A)), 'byNameWrapped': gql.Argument(gql.List('A')), 'byInternalTypeWrapped': gql.Argument(gql.List(gql.String)), }) result = gql.execute('''{ type: __type(name: "QueryRoot") { fields { args { name type { ...TypeRef } } } } } fragment TypeRef on __Type { kind, name ofType { kind, name ofType { kind, name } } }''') assert not result.errors a_type = {"kind": "OBJECT", "name": "A", "ofType": None} string_type = {"kind": "SCALAR", "name": "String", "ofType": None} assert sort_lists(result.data) == sort_lists({ "type": { "fields": [{ "args": [ { "name": "byPublicType", "type": a_type }, { "name": "byName", "type": a_type }, { "name": "byInternalType", "type": string_type }, { "name": "byPublicTypeWrapped", "type": { "kind": "LIST", "name": None, "ofType": a_type } }, { "name": "byNameWrapped", "type": { "kind": "LIST", "name": None, "ofType": a_type } }, { "name": "byInternalTypeWrapped", "type": { "kind": "LIST", "name": None, "ofType": string_type } }, ] }] } })