graphene icon indicating copy to clipboard operation
graphene copied to clipboard

Found different types with the same name in the schema

Open LeOndaz opened this issue 3 years ago • 3 comments
trafficstars

Bear with me please

Let's assume I have a type called Product in schema1 and a type with the same name in schema2 and I want to expose both schemas via the same API, I get an error saying that I can't have two types with the same name, even though I'm using TOTALLY different types from different files

# file 
class Product(...):
   pass

# another file
class Product(...):
   pass

The workaround is to rename the type to something like ProductX or smth, but I want to know why does this specific behavior happen, is there any kind of global registry that I'm not aware of? I've ran through the code but I found nothing

LeOndaz avatar Jan 30 '22 02:01 LeOndaz

@LeOndaz I didn't find any issues with a different name:. For example I created another example in the examples folder named simple_example:

import graphene

from simple_example import Patron as Patron2

class Patron(graphene.ObjectType):
    id = graphene.ID()
    name = graphene.String()
    age = graphene.Int()

class Query(graphene.ObjectType):

    patron = graphene.Field(Patron)
    patron2 = graphene.Field(Patron2)

    def resolve_patron(root, info):
        return Patron(id=1, name="Syrus", age=27)

    def resolve_patron2(root, info):
        return Patron2(id=2, name="Miles", age=30)

schema = graphene.Schema(query=Query)
query = """
    query something{
      patron {
        id
        name
        age
      }
      patron2 {
        id
        name
        age
      }
    }
"""

if __name__ == "__main__":
    result = schema.execute(query)
    print(result.data)

This imports the Patron model from simple_example and create an alias for it to prevent a name clash.

❯ python3 examples/simple_example2.py
{'patron': {'id': '1', 'name': 'Syrus', 'age': 27}, 'patron2': {'id': '2', 'name': 'Miles', 'age': 30}}

Is that something you considered?

theodesp avatar Jun 14 '22 19:06 theodesp

Note that you cannot have duplicate Fields as per GraphQL spec:

https://spec.graphql.org/June2018/#sec-Language.Fields

Fields in the top‐level selection set of an operation often represent some information that is globally accessible to your application and its current viewer. Some typical examples of these top fields include references to a current logged‐in viewer, or accessing certain types of data referenced by a unique identifier.

theodesp avatar Jun 14 '22 19:06 theodesp

What you want probably is a schema merging function with conflict resolution such as this one: https://www.graphql-tools.com/docs/schema-stitching/stitch-combining-schemas#duplicate-types

Not sure if it's supported here.

theodesp avatar Jun 14 '22 19:06 theodesp