profusion / sgqlc

Simple GraphQL Client
https://sgqlc.readthedocs.io/
ISC License
506 stars 85 forks source link

Getting an `assert iface_name in self.written_types, iface_name` when trying to generate the schema for DatahubAPI #189

Closed luisvicentesanchez closed 1 year ago

luisvicentesanchez commented 2 years ago

I'm getting the following error when I try to generate the schema for the Datahub GraphQL API:

  File "/*******/bin/sgqlc-codegen", line 8, in <module>
    sys.exit(main())
  File "/*******/lib/python3.9/site-packages/sgqlc/codegen/__init__.py", line 130, in main
    args.func(args)
  File "/*******/lib/python3.9/site-packages/sgqlc/codegen/schema.py", line 681, in handle_command
    gen.write()
  File "/*******/lib/python3.9/site-packages/sgqlc/codegen/schema.py", line 164, in write
    self.write_types()
  File "/*******/lib/python3.9/site-packages/sgqlc/codegen/schema.py", line 261, in write_types
    mapped(t)
  File "/*******/lib/python3.9/site-packages/sgqlc/codegen/schema.py", line 483, in write_type_object
    assert iface_name in self.written_types, iface_name
AssertionError: EntityWithRelationships

The interface is the following https://datahubproject.io/docs/graphql/interfaces#entitywithrelationships

Is this error related to the order in which the content of the schema is processed? Is there anything I can do to fix it?

luisvicentesanchez commented 2 years ago

It seems that the problem here is indeed the ordering. I commented https://github.com/profusion/sgqlc/blob/master/sgqlc/codegen/schema.py#L483 and the codegen can create both the schema and operations. When I try to use the operations I get an error like:

Traceback (most recent call last):
  File "/***********/testing.py", line 2, in <module>
    import acryl_datahub_schema
  File "/***********/datahub_schema.py", line 4490, in <module>
    class Chart(sgqlc.types.Type, EntityWithRelationships, Entity):
NameError: name 'EntityWithRelationships' is not defined
barbieri commented 2 years ago

Hum... I'll need some time to investigate it properly, but removing the assertion is not correct (because it will cause a runtime error as you spotted).

Meanwhile, if you can investigate yourself, see if the ordering issue is caused by a dependency cycle or just a missing "topo-sort"... I don't recall if I added some topology sort based on the usage, trusting "the input is already sorted". A quick way to check is reorder the introspection JSON and move the EntityWithRelationships before its users.

luisvicentesanchez commented 2 years ago

Yes, I just removed the assertion just to "preview" how the library would fit in our use case. I will try to re-order the JSON and I will let you know if that solves the problem.

Evgenus commented 2 years ago

any update on this?

joegoldbeck commented 2 years ago

Ran into this as well. Re-ordering types to move EntityWithRelationships to the very top of types and then also using the code from #209 (which removes an alphabetic sort) fixes it for me :)

ty @Evgenus @barbieri !

benmosher commented 1 year ago

I think this is a quirk resulting from optimizations within Python's list sort.

FWIW: I added this locally to sort all interfaces ahead of everything else, and it fixes it for me:

    # fields without interfaces first, then order the interface
    # implementor after the interface declaration
    def depend_cmp(a, b):
        if a["kind"] != b["kind"]:
            if a["kind"] == "INTERFACE":
                return -1
            elif b["kind"] == "INTERFACE":
                return 1
barbieri commented 1 year ago

could you please check #219

barbieri commented 1 year ago

merged, pleas reopen if still an issue