Skip to content

SequelizeJSON support #137

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

Closed
SpeciesDecipit opened this issue Aug 29, 2020 · 4 comments · Fixed by #255
Closed

SequelizeJSON support #137

SpeciesDecipit opened this issue Aug 29, 2020 · 4 comments · Fixed by #255
Labels
type: question or discussion Issue discussing or asking a question about gql

Comments

@SpeciesDecipit
Copy link

SpeciesDecipit commented Aug 29, 2020

Hello!

Does GQL package (3.0.0a1) have the support of making queries with DSL for SequelizeJSON type? For example:

{
  user_raw_search(where: {county: {eq: "Pulaski"}}) {
    user_id
  }
}

Where where argument is SequelizeJSON

Because in case of using in such way:

dsl.query(
        dsl.Query.user_raw_search(where={"county": {"eq": "Pulaski"}}).select(
            dsl.User.user_id,
        ),
)['user_raw_search']

I got an error: Cannot convert value to AST: {'county': {'eq': 'Pulaski'}}.

GraphQL schema:

directive @cacheControl(
  maxAge: Int
  scope: CacheControlScope
) on FIELD_DEFINITION | OBJECT | INTERFACE
directive @specifiedBy(url: String!) on SCALAR
enum CacheControlScope {
  PUBLIC
  PRIVATE
}

type Query {
  user(id: ID!): User
  users(order: String, limit: Int, offset: Int): [User]
  user_search(where: UserQuery!, order: String, limit: Int, offset: Int): [User]
  user_raw_search(
    where: SequelizeJSON!
    order: String
    limit: Int
    offset: Int
  ): [User]
}

scalar SequelizeJSON

scalar Upload

type User {
  active: Boolean
  address: String
  address2: String
  address3: String
  city: String
  county: String
  dob: String
  email: String
  firstName: String
  gender: String
  id: ID
  lang: String
  lastName: String
  memberId: String
  phoneNumber: String
  sb360userId: String
  state: String
  zip: String
}

input UserQuery {
  active: Boolean
  address: String
  address2: String
  address3: String
  city: String
  county: String
  dob: String
  email: String
  firstName: String
  gender: String
  id: ID
  lang: String
  lastName: String
  memberId: String
  phoneNumber: String
  sb360userId: String
  state: String
  zip: String
}
@KingDarBoja KingDarBoja added the type: question or discussion Issue discussing or asking a question about gql label Aug 29, 2020
@KingDarBoja
Copy link
Contributor

KingDarBoja commented Aug 29, 2020

This is not supported at the moment but maybe with #33 there is a chance to implement SequelizeJSON scalar :)

More info (I think this is the source): https://github.com/mickhansen/graphql-sequelize on this file.

@SpeciesDecipit
Copy link
Author

@KingDarBoja Okey, thank you for so fast response. I am going to move just RAW GraphQL queries.

@fpieper
Copy link

fpieper commented Dec 21, 2020

@KingDarBoja Okey, thank you for so fast response. I am going to move just RAW GraphQL queries.

Maybe, I did understand you wrong, but I am using regular python dictionaries and it seems to work (you can use any Python code which results in a serializable dict):

ds.mutation_root.update_suppliers(
    where=filter_none_values({
        'id': {'_eq': supplier_id},
        'contact_person_id': user_id and {'_eq': user_id}
    }),
    _set=serialize(supplier)
).select(
    ds.supplier_mutation_response.affected_rows,
    ds.supplier_mutation_response.returning.select(
        ds.supplier.id
    )
)

filter_none_values is my own helper function, which filters None values from a dictionary (allows easy construction of optional fields)
serialize is my own helper function which serializes

from functools import singledispatch


def filter_none_values(data: dict):
    return {k: v for k, v in data.items() if v is not None}


@singledispatch
def serialize(ob):
    return ob


@serialize.register(dict)
def _serialize_dict(ob):
    return {k: serialize(v) for k, v in ob.items()}


@serialize.register(list)
def _serialize_list(ob):
    return [serialize(v) for v in ob]


@serialize.register(Enum)
def _serialize_enum(ob):
    return ob.name


@serialize.register(date)
def _serialize_date(ob):
    return ob.isoformat()

Besides that, I am using SimpleNamespace (with a nested helper) to convert the dict response to a python object with regular fields for easier access:

from functools import singledispatch
from types import SimpleNamespace


@singledispatch
def nested_namespace(ob):
    return ob


@nested_namespace.register(dict)
def _nested_dict(ob):
    return SimpleNamespace(**{k: nested_namespace(v) for k, v in ob.items()})


@nested_namespace.register(list)
def _nested_list(ob):
    return [nested_namespace(v) for v in ob]

You can use it like:

response = await session.execute(query)
return nested_namespace(response)

Hopefully in the future gql will support these kind of functionality natively (#104 this goes in that direction), but for now I can workaround with my helper functions.

I hope, I was able to help you 👍

By the way, I am using:

gql               3.0.0a5
graphql-core      3.1.2

@leszekhanusz
Copy link
Collaborator

@ SpeciesDecipit
With PR #255, you should now be able to add complex objects as inputs in the DSL module.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question or discussion Issue discussing or asking a question about gql
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants