-
Notifications
You must be signed in to change notification settings - Fork 0
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
Custom root level queries and mutations via inheritance #77
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# How To: Extending Platformics Generated Code | ||
Platformics will generate a GraphQL API that exposes CRUD operations for each entity type in your LinkML schema. It is often the case, however, that you will need to expose more functionality in your API than just CRUD. There are a few options for extending generated code to add custom business logic to your application. | ||
|
||
## Adding root-level queries and mutations | ||
Platformics will generate basic read and aggregate queries in `api/queries.py`. These are inherited by a query class in `custom_queries.py`, which will gain access to all the codegenned queries as well as allow you to implement your own queries. To add a new root level field (query), add the field to the query class in `custom_queries.py`: | ||
|
||
``` | ||
@strawberry.type | ||
class Query(BaseQuery): | ||
foo: str = my_custom_field | ||
``` | ||
The resolver for the custom field should be decorated with `@strawberry.field`: | ||
``` | ||
@strawberry.field | ||
def my_custom_field(self) -> str: | ||
return "foo" | ||
``` | ||
|
||
Similarly, for custom mutations, root level mutations can be added to the `Mutation` class in `custom_mutations.py`, which inherits from the codegenned `Mutation` class in `api/mutations.py` | ||
|
||
``` | ||
@strawberry.type | ||
class Mutation(BaseMutation): | ||
bar: str = my_custom_mutation | ||
``` | ||
The resolver for the custom mutation should be decorated with `@strawberry.mutation`: | ||
``` | ||
@strawberry.mutation | ||
def my_custom_mutation(self) -> str: | ||
return "bar" | ||
``` | ||
Both the custom query and mutation classes are imported into `main.py` and used by Strawberry to generate an API at runtime: | ||
``` | ||
from custom_mutations import Mutation | ||
from custom_queries import Query | ||
|
||
schema = strawberry.Schema(query=Query, mutation=Mutation, config=get_strawberry_config(), extensions=[HandleErrors()]) | ||
``` | ||
|
||
### Overriding queries and mutations | ||
By creating a field with the same name as an existing codegenned query or mutation, you can override the codegenned resolver. | ||
``` | ||
@strawberry.mutation | ||
def my_override_mutation(self) -> str: | ||
return "Sorry, I overrode the mutation" | ||
|
||
@strawberry.type | ||
class Mutation(BaseMutation): | ||
create_sample: str = my_override_mutation | ||
``` | ||
|
||
## Using your own templates | ||
TODO: fill this out. Existing functionality. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import strawberry | ||
from api.mutations import Mutation as BaseMutation | ||
|
||
|
||
@strawberry.mutation | ||
def my_custom_mutation(self) -> str: | ||
return "bar" | ||
|
||
@strawberry.mutation | ||
def my_override_mutation(self) -> str: | ||
return "Sorry, I overrode the mutation" | ||
|
||
|
||
@strawberry.type | ||
class Mutation(BaseMutation): | ||
bar: str = my_custom_mutation | ||
create_sample: str = my_override_mutation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import strawberry | ||
from api.queries import Query as BaseQuery | ||
|
||
|
||
@strawberry.field | ||
def my_custom_field(self) -> str: | ||
return "foo" | ||
|
||
|
||
@strawberry.type | ||
class Query(BaseQuery): | ||
foo: str = my_custom_field |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: you can do ```python for python syntax highlighting!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ooh!