wyfo / apischema

JSON (de)serialization, GraphQL and JSON schema generation using Python typing.

Home Page:https://wyfo.github.io/apischema/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support to convert GraphQL schema from file to JSON

RedHeadphone opened this issue · comments

Convert whole schema from graphql.schema file to JSON

For eg.
graphql.schema file

type Query {
   greeting:String
   students:[Student]
}

type Student {
   id:ID!
   firstName:String
   lastName:String
   password:String
   collegeId:String
}

output

{
  "query": {
    "greeting": "String",
    "students":["Student"]
  },
  "definitions":{
    "Student":{
      "id":{"type": "string", "format": "uuid"},
      "firstName":"String",
      "lastName":"String",
      "password":"String",
      "collegeId":"String"
    }
  }
}

There is a Javascript library for that https://github.com/charlypoly/graphql-to-json-schema , we can have same in this library.

Actually, I'm not sure this feature would fit in the library, for the following reason:

  • apischema is a code-first library, the only thing it manipulate is annotated Python code, not stringified schema.
  • I may be wrong, but it seems to me that this use case is a CLI thing, where you pass your GraphQL schema to an executable which output the corresponding JSON schema. In this regard, as there is already another library to do it, even if it is JS, I would go with it, because apischema is not intended for this kind of use case.
  • As mentioned in the first two points, this feature is completely orthogonal to apischema code base, I think there is literally nothing that could be reused. If a Python library is really needed, it could and I think it should be an independent library.
  • (I never wanted to add code generation to apischema, because, in addition to be orthogonal to the current code base, it can only be a lossy operation; it can't handle flattened classes for example. This schema conversion is kind of lossy too, so it's not the kind of thing I like to do.)

Could you tell me more about your use case, and why you think it would be a good feature for apischema?

I actually tried using apischema to make json graphql schema from strayberry-graphql schema but I kept getting error, and seemed it would be better if apischema takes graphql.schema file (generated by strawberry-graphql) and make json schema from that file directly.

Its fine if the library doesn't wants to change approach, even I think separate library dedicated only to convert graphql schema to json schema should be there (as you mentioned CLI feature, can have that as option too). If I will get time, I will make it as side project. Thank you