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

Add GraphQL transport #71

Merged
merged 2 commits into from
Dec 11, 2024
Merged

Add GraphQL transport #71

merged 2 commits into from
Dec 11, 2024

Conversation

marcelldls
Copy link
Contributor

An additional "backend" for FastCS which provides a GraphQL api.
The API is fully discoverable and demoable from the autogenerated "GraphiQL gui" at "{domain}/graphql".
I choose to use the FastAPI "Strawberry" integration as it is the recommended library by FastAPI.

The following considersations have been taken to conform with best practices:

  • Attributes to be read using a query (also demonstates how subcontrollers are nested)

Example:

Query request:
query{
    DesiredPosition
    subcontroller {
        DesiredPosition
    }
}

Query response:
{
  "data": {
    "subcontroller": {
      "DesiredPosition": 1.0,
    }
    "DesiredPosition": 1.0,
  }
}
  • Attributes to be written using a mutation

Example:

Mutation request:
mutation{
  DesiredPosition(value: 2.0)
}

Mutation response:
{
  "data": {
    "DesiredPosition": 2.0
  }
}
  • Commands can cause server side effects so they are triggered using a mutation as per convention
    • A boolean response is returned to confirm the command was executed successfully

Example:

Mutation request:
mutation{
  BlinkLED
}

Mutation response:
{
  "data": {
    "BlinkLED": true
  }
}

Further:

  • I follow a similar pattern to the Tango "backend" to create the server and testing, so any feedback from the exisiting PRs should feed into this
  • I have added nested subcontrollers to test arbitrary subcontroller nesting as this is a harder problem for the graphql request structure - the tests of other backends should also apply this and need to be updated accordingly

@marcelldls marcelldls requested a review from GDYendell November 6, 2024 16:40
@marcelldls marcelldls force-pushed the graphql-backend branch 4 times, most recently from ee08dd5 to 8d61dde Compare November 12, 2024 11:01
@GDYendell
Copy link
Contributor

I have a had a quick look, but I am going to ignore this until we iterate on and merge some of the other recent PRs first, as you suggest.

@marcelldls
Copy link
Contributor Author

I have a had a quick look, but I am going to ignore this until we iterate on and merge some of the other recent PRs first, as you suggest.

Lets do that. When we are done iterating, we can enjoy some of the graph recursion xD

Copy link

codecov bot commented Nov 21, 2024

Codecov Report

Attention: Patch coverage is 91.30435% with 10 lines in your changes missing coverage. Please review.

Project coverage is 88.06%. Comparing base (e2dfc01) to head (ef5c8e3).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/fastcs/transport/graphQL/graphQL.py 94.11% 5 Missing ⚠️
src/fastcs/transport/graphQL/adapter.py 78.57% 3 Missing ⚠️
src/fastcs/launch.py 60.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #71      +/-   ##
==========================================
+ Coverage   87.76%   88.06%   +0.30%     
==========================================
  Files          28       31       +3     
  Lines        1193     1307     +114     
==========================================
+ Hits         1047     1151     +104     
- Misses        146      156      +10     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@marcelldls marcelldls marked this pull request as ready for review November 21, 2024 15:29
@marcelldls
Copy link
Contributor Author

Apparently camelCase is the convention for field names https://www.apollographql.com/docs/graphos/schema-design/guides/naming-conventions#casing so i have adjusted accordingly

Copy link
Contributor

@GDYendell GDYendell left a comment

Choose a reason for hiding this comment

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

First pass comments

src/fastcs/backends/graphQL/graphQL.py Outdated Show resolved Hide resolved
src/fastcs/backends/graphQL/graphQL.py Outdated Show resolved Hide resolved
src/fastcs/backends/graphQL/graphQL.py Outdated Show resolved Hide resolved
src/fastcs/backends/graphQL/graphQL.py Outdated Show resolved Hide resolved
src/fastcs/backends/graphQL/graphQL.py Outdated Show resolved Hide resolved
src/fastcs/backends/graphQL/graphQL.py Outdated Show resolved Hide resolved
src/fastcs/backends/graphQL/graphQL.py Outdated Show resolved Hide resolved
src/fastcs/backends/graphQL/graphQL.py Outdated Show resolved Hide resolved
src/fastcs/backends/graphQL/graphQL.py Outdated Show resolved Hide resolved
src/fastcs/backends/graphQL/graphQL.py Outdated Show resolved Hide resolved
@marcelldls
Copy link
Contributor Author

I will rebase this on monday

@marcelldls
Copy link
Contributor Author

@GDYendell Rebased. Also did the rename of create_type as discussed this morning

@GDYendell GDYendell changed the title Attempt graphql backend Add GraphQL transport Dec 10, 2024
marcelldls and others added 2 commits December 10, 2024 15:48
Apply upstream feedback

Remove fastapi layer from gql

Process review comments

Rebase

create_type -> create_strawberry_type
@marcelldls
Copy link
Contributor Author

Hmm - the tests passed now that its in my branch... LGTM I guess

@marcelldls
Copy link
Contributor Author

marcelldls commented Dec 10, 2024

Ah I guess the rebase might have solved it

@GDYendell
Copy link
Contributor

Note that puts still do not work here because of the problem where the tasks are added to the wrong event loop. So we either need to add something like

def _create_sender_callback(self, attribute, controller):
    async def callback(value):
        future = asyncio.run_coroutine_threadsafe(
            attribute.sender.put(controller, attribute, value), self._loop
        )
        await asyncio.wrap_future(future)

    return callback

or we need to figure out #98.

I think we should just merge this for now, though.

@marcelldls
Copy link
Contributor Author

The puts issue is across transports so I agree we should merge this and look towards #98 which I believe solves the problem

Copy link
Contributor

@GDYendell GDYendell left a comment

Choose a reason for hiding this comment

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

Questionable self approval...

image

@GDYendell GDYendell merged commit 39e55d4 into main Dec 11, 2024
17 checks passed
@GDYendell GDYendell deleted the graphql-backend branch December 11, 2024 11:20
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.

2 participants