Skip to content

Django ForeignKeys Cache #807

@oscarmcm

Description

@oscarmcm

Hey! I have been using strawberry and strawberry django for a while now. But recently I started noticing some weird behavior when creating related nested objects.

This is the simple mutation that I use:

mutation CreateNFA {
  createNewsAlert(
    input: {
      newsAlert: {
        name: "DEMO",
        sendOn: DAILY, 
        sendAt: "14:00",
        timezone: AMERICA_CHICAGO, 
        recipients: [
          "[email protected]",
          "[email protected]"
        ]
      },
      sections: [
        {feedId: "341", savedViewId: "265", name: "Headlines"}
      ]
    }
  ) {
    ... on NewsAlertType {
      id
      name
      sections {
        id
        name
      }
    }
    ... on OperationInfo {
      __typename
      messages {
        message
      }
    }
  }
}

When I run the mutation the response comes as:

{
  "data": {
    "createNewsAlert": {
      "__typename": "NewsAlertType",
      "id": "1059",
      "name": "DEMO",
      "sections": [],
      "alertLogo": null
    }
  }
}

As you notice there's no sections at all, but then if I do:

query {
  newsAlerts(filters:  {
     id: "1059"
  }) {
    id
    name
    sections {
      id
      name
    }
  }
}

The query does contains the created section:

{
  "data": {
    "newsAlerts": [
      {
        "id": "1059",
        "name": "DEMO",
        "sections": [
          {
            "id": "1356",
            "name": "Headlines"
          }
        ]
      }
    ]
  }
}

The mutation code is not crazy at all:

def create_news_alert_resolver(
    info: strawberry.Info,
    news_alert: NewsAlertInput,
    sections: list[NewsAlertSectionInput],
) -> NewsAlertType:
    tenant: "Tenant" = getattr(info, "tenant")
    if not news_alert.recipients or len(news_alert.recipients) == 0:
        raise ValidationError("At least one recipient is required.")

    alert: NewsAlert = NewsAlert.objects.create(
        tenant=tenant,
        name=news_alert.name,
        description=news_alert.description or "",
        is_active=news_alert.is_active,
        send_on=news_alert.send_on,
        send_at=news_alert.send_at,
        timezone=news_alert.timezone,
        recipients=news_alert.recipients,
    )
    
    NewsAlertSection.objects.bulk_create(
        [
            NewsAlertSection(
                news_alert=alert,
                feed_id=section_input.feed_id,
                saved_view_id=section_input.saved_view_id,
                name=section_input.name,
                article_limit=section_input.article_limit,
                sort_order=section_input.sort_order,
            )
            for section_input in sections
        ]
    )

    # Refresh to ensure the alert fields contain current data
    alert.refresh_from_db()

    return cast(NewsAlertType, alert)

@strawberry.type
class NewsAlertMutation:
    """GraphQL mutations for News Alerts."""

    @strawberry_django.mutation(
        handle_django_errors=True,
        extensions=[permissions.IsAuthenticated(), InputMutationExtension()],
    )
    @require_tenant
    @require_user
    def create_news_alert(
        self: Self,
        info: strawberry.Info,
        news_alert: NewsAlertInput,
        sections: list[NewsAlertSectionInput],
    ) -> NewsAlertType:
        """Create a news alert with its sections."""
        return create_news_alert_resolver(info, news_alert, sections)

System Information

  • Python version: 3.11.7
  • Strawberry version:
    "strawberry-graphql-django==0.65.1",
    "strawberry-graphql[debug-server]==0.276.2",

Any feedback or guidance here is very welcomed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingquestionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions