Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate subscriptions as async generators #133

Merged
merged 19 commits into from
Apr 25, 2023
Merged

Generate subscriptions as async generators #133

merged 19 commits into from
Apr 25, 2023

Conversation

mat-sop
Copy link
Contributor

@mat-sop mat-sop commented Apr 14, 2023

resolves #65
Example of generated client:

class Client(AsyncBaseClient):
    async def get_counter(self) -> AsyncIterator[GetCounter]:
        query = gql(
            """
            subscription getCounter {
              counter
            }
            """
        )
        variables: dict[str, object] = {}
        async for data in self.execute_ws(query=query, variables=variables):
            yield GetCounter.parse_obj(data)

@mat-sop mat-sop self-assigned this Apr 14, 2023
@mat-sop mat-sop marked this pull request as draft April 14, 2023 11:43
@mat-sop mat-sop marked this pull request as ready for review April 20, 2023 15:08
@mat-sop mat-sop changed the title Draft: Generate subscriptions as async generators Generate subscriptions as async generators Apr 20, 2023
@mat-sop mat-sop requested a review from rafalp April 20, 2023 15:08
CHANGELOG.md Outdated Show resolved Hide resolved
ariadne_codegen/client_generators/client.py Outdated Show resolved Hide resolved
pyproject.toml Outdated
@@ -25,6 +25,7 @@ dependencies = [
"toml~=0.10",
"httpx~=0.23",
"pydantic~=1.10",
"websockets~=11.0",
Copy link
Contributor

Choose a reason for hiding this comment

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

Make this dependency opt-in, eg. pip install ariadne-codegen[subscriptions]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hm I'm not sure if it's the right way. AsyncBaseClient still depends on websockets even when no subscription was generated.

Copy link
Contributor

Choose a reason for hiding this comment

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

It should handle websockets not being installed. Usual approach is handling the import error and importing placeholder object that implements the subset of API but raises not implemented error from its methods.

@mat-sop mat-sop requested a review from rafalp April 24, 2023 09:08
EXAMPLE.md Outdated

For every provided query/mutation there is generated file, that contains models which correspond to return type of operation. File name is generated by converting query/mutation name to snake case. Root models has the same name as query/mutation, and depend classes use it as prefix in their names.
For every provided query/mutation/subscription there is generated file, that contains models which correspond to return type of operation. File name is generated by converting operation name to snake case. Root models has the same name as query/mutation/subscription, and depend classes use it as prefix in their names.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
For every provided query/mutation/subscription there is generated file, that contains models which correspond to return type of operation. File name is generated by converting operation name to snake case. Root models has the same name as query/mutation/subscription, and depend classes use it as prefix in their names.
For every provided query/mutation/subscription there is a generated file that contains models which correspond to return type of operation. File name is generated by converting operation name to snake case. Root models has the same name as query/mutation/subscription, and depend classes use it as prefix in their names.

Self = TypeVar("Self", bound="AsyncBaseClient")

GQL_SUBPROTOCOL = "graphql-transport-ws"
GQL_CONNECTION_INIT = "connection_init"
Copy link
Contributor

Choose a reason for hiding this comment

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

We could group message types under an Enum

Copy link
Contributor

@rafalp rafalp left a comment

Choose a reason for hiding this comment

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

We should mention in readme that websockets are optional dependency required for subscriptions.

@rafalp rafalp self-requested a review April 24, 2023 15:30
@mat-sop mat-sop merged commit a89e71d into main Apr 25, 2023
3 checks passed
@mat-sop mat-sop deleted the subscriptions branch April 25, 2023 08:53
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.

Support GraphQL subscriptions
2 participants