Skip to content

How do you introspect the schema? #3

Closed

Description

@ashinohara

I see that the graphiql interface is able to introspect the schema, but I am not sure how you would export the schema form python. I see the introspect function on the Schema object, but that simply returns the objects from the type_map.

Activity

  1. syrusakbary commented on Oct 9, 2015

    @syrusakbary
    Member

    @ashinohara I've fixed the introspection method in the schema.
    Just update your graphene package to 0.1.3 version and run schema.introspect() in your graphene.Schema:

    import json
    
    schema = graphene.Schema(....)
    
    introspection_dict = schema.introspect()
    
    # Print the schema in the console
    print json.dump(introspection_dict)
    
    # Or save the schema into some file
    open('schema.json', ‘w’) as fp:
        json.dump(introspection_dict, fp)
  2. syrusakbary commented on Oct 9, 2015

    @syrusakbary
    Member

    Thanks @jhgg for showing me the way 😉

  3. jhgg commented on Oct 9, 2015

    @jhgg
    Member

    No problem!

  4. added a commit that references this issue on Aug 26, 2017
  5. yakneens commented on Oct 18, 2017

    @yakneens

    This should be automatically exposed by Graphene as a query that returns the file.

  6. isakal commented on Feb 2, 2020

    @isakal

    If you want to get schema in GraphQL SDL format:

    >>> from your_project.schema_definition import schema
    >>>  print(schema)
    schema {
      query: Query
      mutation: Mutation
    }
    
    type CreateUser {
      user: User
      message: String
      success: Boolean
    }
    # and so on ...
  7. Curiouspaul1 commented on Dec 29, 2020

    @Curiouspaul1

    If you want to get schema in GraphQL SDL format:

    >>> from your_project.schema_definition import schema
    >>>  print(schema)
    schema {
      query: Query
      mutation: Mutation
    }
    
    type CreateUser {
      user: User
      message: String
      success: Boolean
    }
    # and so on ...

    How does one save this to a file like schema.graphql or .gql

  8. 5chulzi commented on Feb 17, 2021

    @5chulzi

    If you want to get schema in GraphQL SDL format:

    >>> from your_project.schema_definition import schema
    >>>  print(schema)
    schema {
      query: Query
      mutation: Mutation
    }
    
    type CreateUser {
      user: User
      message: String
      success: Boolean
    }
    # and so on ...

    How does one save this to a file like schema.graphql or .gql

    Here you go:

    with open('schema.graphql', 'w') as f:
        f.write(str(schema))

    This works, because the schema class uses the def __str__(self) method to return the string representation of the underlying graphql schema.

  9. guiklimek commented on Oct 21, 2021

    @guiklimek

    Using the command: https://docs.graphene-python.org/projects/django/en/latest/introspection/

    ./manage.py graphql_schema --schema tutorial.quickstart.schema --out schema.graphql

  10. krish7919 commented on Nov 26, 2021

    @krish7919

    (Sorry to comment on a closed issue but this seems to be the right place)

    Is there a way for graphene to query Postgres directly and generate an SDL file ?

  11. msalah0e commented on Oct 22, 2022

    @msalah0e

    @syrusakbary Hi, each time I generate GQL schema, some fields' order changes.
    Screen Shot 2022-10-22 at 8 11 49 PM
    Any idea on how I generate it, to be in the same order?
    My goal is to trace schema changes in git.

  12. conao3 commented on Sep 30, 2023

    @conao3
    Contributor

    just a note, schema.introspect() is now missing and str(schema) is just default str serializer.

    If you want to get result of introspect query,

    import graphql
    
    graphql.introspection_from_schema(schema)  # -> TypedDict

    And you want to get SDL of schema,

    import graphql
    
    graphql.print_schema(schema)  # -> str
  13. conao3 commented on Oct 3, 2023

    @conao3
    Contributor

    Sorry, this is graphene repo, not graphql-core. ignore me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions