datamodel-code-generator icon indicating copy to clipboard operation
datamodel-code-generator copied to clipboard

Generate single class for common types and shorten class name

Open rahul-gj opened this issue 4 months ago • 1 comments

Is your feature request related to a problem? Please describe. Currently we generate individual classes even for same type in schema.

for below example:

schema.graphql

type Query {
  studentQuery(ids: [ID!]!): [Student]
}

type Person {
  id: ID!
  name: String!
}

type Student {
  id: ID!
  name: String!
  father: Person!
  mother: Person!
  friends: [Person!]
}

query.graphql

query Enrolments($ids: [ID!]!) {
  studentQuery(ids: $ids) {
    id
    name
    father {
      id
      name
    }
    mother {
      id
      name
    }
    friends {
      id
      name
    }
  }
}

and pyproject.toml

[tool.ariadne-codegen]
queries_path = "query.graphql"
schema_path = "schema.graphql"

we generate following enrolments.py in graphql_client folder

...
class EnrolmentsStudentQuery(BaseModel):
    id: str
    name: str
    father: "EnrolmentsStudentQueryFather"
    mother: "EnrolmentsStudentQueryMother"
    friends: Optional[List["EnrolmentsStudentQueryFriends"]]


class EnrolmentsStudentQueryFather(BaseModel):
    id: str
    name: str


class EnrolmentsStudentQueryMother(BaseModel):
    id: str
    name: str


class EnrolmentsStudentQueryFriends(BaseModel):
    id: str
    name: str

Describe the solution you'd like I would like to have an option to remove duplicate classes based on same type with same fields for example would like to generate following enrolments.py in graphql_client folder for above inputs

class EnrolmentsStudentQuery(BaseModel):
    id: str
    name: str
    father: "Person"
    mother: "Person"
    friends: Optional[List["Person"]]


class Person(BaseModel):
    id: str
    name: str

Describe alternatives you've considered Need to modify generated classes as no alternative. Planning to use inheritance to override such common fields with manually generated classes.

Additional context N/A

rahul-gj avatar Feb 20 '24 11:02 rahul-gj