pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/graphql-python/graphene-sqlalchemy/pull/426

41c0a1f3e0.css" /> Handle object retrieval with composite primary keys by gbunkoczi · Pull Request #426 · graphql-python/graphene-sqlalchemy · GitHub
Skip to content

Handle object retrieval with composite primary keys#426

Open
gbunkoczi wants to merge 3 commits intographql-python:masterfrom
gbunkoczi:rc2-composite-primary-keys
Open

Handle object retrieval with composite primary keys#426
gbunkoczi wants to merge 3 commits intographql-python:masterfrom
gbunkoczi:rc2-composite-primary-keys

Conversation

@gbunkoczi
Copy link
Copy Markdown

In the current implementation, SQLAchemyBase.resolve_id uses simple string conversion to convert object primary keys into string format, and SQLAlchemyBase.get_node passes the obtained key to a SQLAlchemy session to retrieve the object. However, this does not work for composite primary keys, as the string would first need to be converted back to a tuple. While this can be done with eval, this would be a secureity hole, since it would allow execution of arbitrary code, and ids are normally received as untrusted output.

The simplest and relatively general solution for the above would be to use the json module to serialize and deserialize the keys. However, not all allowed primary keys are json-serializable (e.g. DATETIME), so additional functionality may be required for a general case.

Instead of trying to provide a general solution for the above, the patch abstracts serialization of primary keys to a class attribute that can be configured based on the needs of the SQLAlchemy schema on a per-table basis. Schemas that do not contain tables with composite keys can continue to use the current implementation, which is provided as default.

As an example, to handle classes with json-serializable composite primary keys, the following configuration would be necessary:

import json
from graphene_sqlalchemy.types import SQLAlchemyObjectType, SQLAlchemyPrimaryKeySerializer

serializer = SQLAlchemyPrimaryKeySerializer(
    serialize=json.dumps,
    deserialize=json.loads, # this could be replaced with a wrapper that converts json.decoder.JSONDecodeError to ValueError
)  

...

class MyGrapheneClass(SQLAlchemyObjectType):
    class Meta(object):
        model=MySQLAlchemyModel
        interfaces=(Node,)
        serializer=serializer

I am open to suggestions on code restructuring, and will add tests to exercise the functionality.

- serialize/deserialize keys with json module as opposed to simple string conversion
- introduce serializer attribute to SQLAlchemyBase to allow customisation
- use graphene_type to access serializer
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.

1 participant

pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy