-
Notifications
You must be signed in to change notification settings - Fork 228
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
flask_sqlalchemy example : different types with the same name in the schema: EmployeeConnection, EmployeeConnection. #153
Comments
+1 |
2 similar comments
+1 |
+1 |
I suspect that #98 will fix this. |
+1 |
1 similar comment
+1 |
+1 Seems like this error only occurs, when using btw. tested with Python 3.6 and 3.7 |
+1 with python3.6 |
2 similar comments
+1 with python3.6 |
+1 with python3.6 |
any quick fix to get a working example going? |
+1 |
@suxin1 - your fix doesn't work for me, i got this error: |
@ivanvorona So tricky. I actually did not test it. Sorry for my bad assumption. I'll post what i did in the original post. |
@suxin1 thank you for your code but i was lazy reading it :) anyway, the fix is as simple as replacing "EmployeeConnection" with "EmployeeConnections", following post helped me identify root cause: |
@ivanvorona @suxin1 Good work, renaming to EmployeeConnections fix the example ! However for my own understanding, why this error occurs for EmployeeConnection and not for RoleConnection and DepartmentConnection ? I just try to identify the real issue, but I don't understand why the EmployeeConnection exists at the first place. |
@ivanvorona Looks like there is nothing special about that plural s. Name |
@tomahim It should be the relationship field (the only significant difference between the two models 😅 ). Graphene-SQLAlchemy produces a connection to resolve |
+1 |
What about changing auto-naming here:
to something having distinct auto-generated suffix/prefix? |
I solved this problem by changing a few names in schema.py, here is my code:
But why? I do not know, just lucky. |
Two questions I have:
Having the same connection schema or slightly differing ones both with similar names sounds confusing and not ideal. |
I think I answered my own questions by looking at
So if you wanted to fix the example "correctly", don't define the "Connection" classes, and use the generated ones: class Query(graphene.ObjectType):
node = relay.Node.Field()
all_employees = SQLAlchemyConnectionField(Employee._meta.connection)
all_departments = SQLAlchemyConnectionField(Department._meta.connection, sort=None) |
Work for me |
Expanding on previous comments, here's a working example of a custom connection type ( from graphene import Int, NonNull, ObjectType
from graphene.relay import Connection, Node
from graphene_sqlalchemy import SQLAlchemyObjectType, SQLAlchemyConnectionField
# Implements the totalCount field in a connection.
class CountedConnection(Connection):
class Meta:
# Being abstract is important because we can't reference the
# node type here without creating a circular reference. Also, it
# makes reuse easy.
#
# The node type will be populated later with
# `CountedConnection.create_class()` in `Foo`'s
# `__init_subclass_with_meta__()`.
abstract = True
total_count = NonNull(Int)
def resolve_total_count(self, info, **kwargs):
return self.length
# FooConnection is autogenerated due to the Node interface.
class Foo(SQLAlchemyObjectType):
class Meta:
model = FooModel
interfaces = (Node,)
connection_class = CountedConnection
# The connection field can be customized too.
class FooConnectionField(SQLAlchemyConnectionField):
pass
class Query(ObjectType):
fooList = FooConnectionField(Foo) |
This helped fix this: graphql-python/graphene-sqlalchemy#153 (comment)
Ignore fixes with renaming, this comment is on the right track.
Example has been since fixed with:
So you do not have to access private variable This issue should be closed along with pull requests #162. |
Rename Connections Classes :- |
I was facing this issue trying to reuse an enum and I do not use Relay like most of the people here. After looking through the source code which was referenced in this comment , I found a method to reuse the auto-generated types if you need to reference them.
I'll share my solution in-case someone down the road needs this.
This allowed me to reference the enum from my SQLAlchemy models that was converted into a graphene type.
TLDR: Use |
Oh man this really needs to be documented somewhere. Wasted too much time on this and separately found this exact solution. Thanks for sharing it!! |
Hi,
I followed the instruction to use the flask_sqlalchemy example with a Python 2.7 version, when I run the app.py script, I've got the following error :
What am I doing wrong ?
Thank you
The text was updated successfully, but these errors were encountered: