Skip to content

Restore GraphQL API with Strawberry#141

Draft
paula-mg wants to merge 4 commits into
mainfrom
139-graphql-strawberry
Draft

Restore GraphQL API with Strawberry#141
paula-mg wants to merge 4 commits into
mainfrom
139-graphql-strawberry

Conversation

@paula-mg

@paula-mg paula-mg commented Sep 6, 2024

Copy link
Copy Markdown

Fixes #139

Comment thread src/scanspec/schema/schema.py Outdated
Comment on lines +40 to +43
# We make a strawberry input classs using the scanspec pydantic models
# This isn't possible because scanspec models are actually pydantic
# dataclasses. We should have a word with Tom about it and probably
# raise an issue on strawberry.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@callumforrester I am not tied to dataclass, but I have a requirement for positional args. I have a choice of directions for you:

  1. Continue using dataclasses, add support to strawberry, probably using something like what I needed to do to autodoc_pydantic
  2. Ditch dataclasses and make a BaseModel subclass with positional arg support. I think we could make this work both at runtime using something like this and at static analysis by overriding the dataclass_transform. The closed issue would suggest that pydantic would never accept such an approach upstream.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To elaborate on 2, this works for both static analysis and at runtime:

from __future__ import annotations

from abc import ABCMeta
from typing import Any

from pydantic import BaseModel, Field
from typing_extensions import dataclass_transform

# TODO: not sure about the others like NoInitField and PrivateAttr
@dataclass_transform(field_specifiers=(Field,))
class PosargsMeta(type(BaseModel), ABCMeta):
    def __new__(
        mcs,
        cls_name: str,
        bases: tuple[type[Any], ...],
        namespace: dict[str, Any],
        **kwargs: Any,
    ) -> type:
        cls = super().__new__(mcs, cls_name, bases, namespace, **kwargs)
        original_init = cls.__init__

        def patched_init(self, *args, **kwargs):
            for k, v in zip(cls.model_fields, args, strict=False):
                kwargs[k] = v
            original_init(self, **kwargs)

        cls.__init__ = patched_init
        return cls

class Spec(BaseModel, metaclass=PosargsMeta):
    pass

class Line(Spec):
    start: float = Field(description="Midpoint of the first point of the line")
    stop: float = Field(description="Midpoint of the last point of the line")
    num: int = Field(min=1, description="Number of frames to produce")

# pyright and pydantic are happy with this...
obj = Line(3, 4, 5)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no particular preference, we should discuss with @paula-mg since she'll be doing the work.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @callumforrester, I started working on this a while ago, could I still be working on this issue?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@coretl what's the current state of scanspec?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have no plans for modifications in the next few months, the next thing we need to do is make duration a special field on a Frame rather than just another key. Long term we need to look at exposing a function that gives the position at a given time.

@paula-mg paula-mg force-pushed the 139-graphql-strawberry branch from 27f65ae to 9675b79 Compare April 14, 2025 14:26
Remove unnecessary imports

Fix imports
@paula-mg paula-mg force-pushed the 139-graphql-strawberry branch from fccca65 to 4ac4022 Compare April 14, 2025 14:32
@paula-mg paula-mg changed the title 139 graphql strawberry Restore GraphQL API with Strawberry Apr 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Restore GraphQL API with Strawberry

3 participants