graphql-python / graphql-python/graphql-core-legacy
Abstract type must resolve to an Object type at runtime for field Query error
- Dominant language
- Python
- Stars
- 372
- Forks
- 175
- PR merge metrics
- No merged PRs in 30d
Description
I am trying to use unions and see the above error. Any help appreciated. Here's the schema:
```
schema {
query: Query
}
union Address = L3 | L2
type L2 {
mac: String
}
type L3 {
ip: String
}
type Query {
address(t: String): Address
}
```
Here's the example code:
```
import graphql, sys, json
class Query(object):
def resolve_address(self, args, context, info):
typ = args.get('t')
if typ == "L3":
return '10.1.1.10'
else:
return '00:aa:bb:cc:dd:ee'
######################################################################
def resolve_fn(obj, args, context, info):
# If obj is a mapping, return obj[field_name], else if
# obj.resolve_field_name() exists call it, else return obj.field_name.
field_name = info.field_name
if hasattr(obj, '__getitem__'):
return obj[field_name]
rfn = getattr(obj, "resolve_%s" % field_name, None)
if rfn and callable(rfn):
return rfn(args, context, info)
else:
return getattr(obj, field_name)
class MiddlewareManager(graphql.MiddlewareManager):
def get_field_resolver(self, field_resolver):
return resolve_fn
######################################################################
query = '{address(t:"L2"){... on L3{ip} ...on L2{mac}}}'
schema_source = open('schema.graphql').read()
schema = graphql.build_ast_schema(graphql.parse(schema_source))
r = graphql.graphql(schema, query,
root_value=Query(),
context_value=123,
middleware=MiddlewareManager())
print "errors:", r.errors
print "data:", r.data
print "json:", json.dumps(r.data)
```
Similar example written in Graphene works fine but I would like to use schema written in native grpahql format.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.