How can we specify graphql schema to use a custom db for saving objects in Django?

70 Views Asked by At

Graphql Schema Mutations happen to use the default DB specified in settings.py. Since I have a Django project with multiple apps and db's , how can I specify graphql mutation to use a different db and not the default one.

class CreateCategory(graphene.Mutation):
    class Arguments:
        name = graphene.String(required=True)
        slug = graphene.String(required=True)

    category = graphene.Field(CategoryType)

    @classmethod
    def mutate(cls, root, info, name, slug):
        category = Category()
        category.name = name
        category.slug = slug
        print(category)
        try:
            category.save()
        except:
            traceback.print_exc()
        return CreateCategory(category=category)
1

There are 1 best solutions below

0
FlipperPA On

If all of it GraphQL models are in a single app, consider using Django’s database routers: https://docs.djangoproject.com/en/4.1/topics/db/multi-db/