strawberry-graphql / strawberry

A GraphQL library for Python that leverages type annotations 🍓
https://strawberry.rocks
MIT License
3.96k stars 528 forks source link

Strawberry schema printer does not have the same order as graphql core #1006

Open Ambro17 opened 3 years ago

Ambro17 commented 3 years ago

When using the api SDL (Schema definition language) as a review tool for schema, changes it's really useful to print the root first, then the children nodes. Graphql-core respects that, but strawberry's implementation does not

Minimal demo

import strawberry
import typing
from graphql import print_schema

@strawberry.type
class Book:
    title: str
    author: str

def books():
    return []

@strawberry.type
class Query:
    books: typing.List[Book] = strawberry.field(resolver=books)

schema = strawberry.Schema(query=Query)
with open('schema_strawberry.gql', 'w+') as f:
    f.write(str(schema))

with open('schema_core.gql', 'w+') as f:
    f.write(print_schema(schema._schema))

After executing python bug_demo.py one can see that strawberry prints this

type Book {
  title: String!
  author: String!
}

type Query {
  books: [Book!]!
}

Instead of graphql-core output

type Query {
  books: [Book!]!
}

type Book {
  title: String!
  author: String!
}

Versions

strawberry-graphql==0.65.3 graphql-core==3.1.5 Python 3.8.5

Upvote & Fund

Fund with Polar

patrick91 commented 1 year ago

@Ambro17 is this still valid for you? You want the schema to print the root types at the top right?

I wonder if we can take this opportunity to also maybe order the rest of types

jkimbo commented 1 year ago

This function from graphql-core might be useful here: https://github.com/graphql-python/graphql-core/blob/5f6a1944cf6923f6249d1575f5b3aad87e629c66/src/graphql/utilities/lexicographic_sort_schema.py