graphene-django icon indicating copy to clipboard operation
graphene-django copied to clipboard

graphql_schema output is no SDL format

Open fkromer opened this issue 3 years ago • 1 comments

I ran ./manage.py graphql_schema --schema <REFERENCE-TO-MY-SCHEMA> --out schema.graphql according to the corresponding docs section with django==3.0.4 and graphene-django==2.8.0 to output the schema in GraphQL Schema Definition Language (SDL) format. As far as I know the format is inferred from the --out file name extension (graphql). However I got the output in GraphQL introspection query result format. For info about the formats refer to e.g. here.

fkromer avatar Mar 10 '21 16:03 fkromer

Suggested workaround (schema.json in introspection format):

npm install graphql-introspection-json-to-sdl
mv schema.graphql schema.json
npx graphql-introspection-json-to-sdl schema.json > schema.graphql

EDIT: It turned out that this approach does not produce a SDL output which is compatible with usual mocking tools. E.g. with graphqleditor it's possible to import the schema but it raises an error Schema is missing root query. schema{ query: YourQueryType }. Add query type to schema. when trying to mock the schema. One has to add to schema.graphql:

schema {
  query: Query
  mutation: Mutation
  subscriptions: Subscription
}

fkromer avatar Mar 10 '21 16:03 fkromer

I cannot reproduce this, so perhaps the behavior changed in a more recent version of graphene-django. On the latest version of graphene-django with the cookbook-plain example project in the repo (https://github.com/graphql-python/graphene-django/tree/main/examples/cookbook-plain), I ran ./manage.py graphql_schema --out schema.graphql and it produced a file with the following (truncated for brevity's sake):

type Query {
  recipe(id: Int, title: String): RecipeType
  allRecipes: [RecipeType]
  recipeingredient(id: Int): RecipeIngredientType
  allRecipeingredients: [RecipeIngredientType]
  category(id: Int, name: String): CategoryType
  allCategories: [CategoryType]
  ingredient(id: Int, name: String): IngredientType
  allIngredients: [IngredientType]
  _debug: DjangoDebug
}

type RecipeType {
  id: ID!
  title: String!
  instructions: String!
  amounts: [RecipeIngredientType!]!
}
...

The above is valid SDL syntax and not introspection format. To your point about missing the schema query definition, as noted in the official GraphQL spec:

While any type can be the root operation type for a GraphQL operation, the type system definition language can omit the schema definition when the query, mutation, and subscription root types are named "Query", "Mutation", and "Subscription" respectively.

So since the graphql_schema command produces an .graphql file in SDL syntax that uses root types of type Query and type Mutation, the separate schema { ... } block is not needed.

sjdemartini avatar Jun 11 '23 19:06 sjdemartini