graphql-epoxy v0.1-alpha-0 release
Pre-releaseThis marks the first release of graphql-epoxy
. The APIs are subject to change, everything is subject to change. But I figured I should get it packaged so that I can start using it.
Things Done So Far
TypeRegistry
Define a type registry using:
from epoxy import TypeRegistry
R = TypeRegistry()
Object Type Definition
Using the above defined TypeRegistry
we can now define our first type:
class Human(R.ObjectType)
id = R.ID.NonNull
name = R.String
age = R.Int
interests = R.String.List
friends = R.Human.List
Interface Definition
We can even define interfaces:
class Pet(R.Interface):
name = R.String
class Dog(R.Implements.Pet):
bark = R.String
class Cat(R.Implements.Pet):
meow = R.String
Or even a type that implements multiple interfaces:
class Bean(R.Interface):
real = R.Boolean
hero = R.Boolean
class Character(R.Interface):
id = R.ID
name = R.String
friends_with = R.Character.List
lives_remaining = R.Field(R.Int)
class Human(R.Implements[R.Character, R.Bean]):
home_planet = R.String
Union Definition
A union can be implemented just as easily:
class PetUnion(R.Union[R.Dog, R.Cat]):
pass
Runtime Type Detection
The is_type_of
methods are automatically created for Objects. If you're bringing your own object, you simply need to tell the TypeRegistry
of it, using R.<type>.CanBe(klass)
. Remember, this does not need to be defined after you define the respective ObjectType.
@R.Dog.CanBe
class MyDog(object):
def __init__(self, name, bark):
self.name = name
self.bark = bark
Object Types as Containers
Additionally, you can use the ObjectType
s you just created as containers.
pets=[
Dog(name='Clifford', bark='Really big bark, because it\'s a really big dog.'),
Cat(name='Garfield', meow='Lasagna')
])
GraphQL Object Interoperability
Epoxy works great with regular GraphQL object types as well. You can just pass them where-ever you'd expect an R.<type>
, with the exception being that if you are defining a field within an ObjectType
or Interface
, you must wrap it in a call to R.Field
.
BuiltInType = GraphQLObjectType(
name='BuiltInType',
fields={
'someString': GraphQLField(GraphQLString)
}
)
BuiltInTypeTuple = namedtuple('BuiltInTypeData', 'someString')
R = TypeRegistry()
class Query(R.ObjectType):
built_in_type = R.Field(BuiltInType)
def resolve_built_in_type(self, obj, args, info):
return BuiltInTypeTuple('Hello World. I am a string.')
For more examples, check out the tests
folder.