Ejemplo n.º 1
0
def dump_graphql_keys(dct):
    """
        Convert a dict to a graphql input parameter keys in the form
        Also camelizes keys if the are slugs and handles complex types. If a value has read=IGNORE it is omitted
        key1
        key2
        key3
        key4 {
            subkey1
            ...
        }
        ...
    :param dct: keyed by field
    :return:
    """
    from rescape_graphene.graphql_helpers.schema_helpers import IGNORE, DENY
    return R.join('\n', R.values(R.map_with_obj(
        dump_graphene_type,
        R.filter_dict(
            lambda key_value: not R.compose(
                lambda v: R.contains(v, [IGNORE, DENY]),
                lambda v: R.prop_or(None, 'read', v)
            )(key_value[1]),
            dct
        )
    )))
def resolver_for_feature_collection(resource, context, **kwargs):
    """
        Like resolver but takes care of converting the geos value stored in the field to a dict that
        has the values we want to resolve, namely type and features.
    :param {string} resource: The instance whose json field data is being resolved
    :param {ResolveInfo} context: Graphene context which contains the fields queried in field_asts
    :return: {DataTuple} Standard resolver return value
    """

    # Take the camelized keys. We don't store data fields slugified. We leave them camelized
    selections = R.map(lambda sel: sel.name.value, context.field_asts[0].selection_set.selections)
    # Recover the json by parsing the string provided by GeometryCollection and mapping the geometries property to features
    json = R.compose(
        # Map the value GeometryCollection to FeatureCollection for the type property
        R.map_with_obj(lambda k, v: R.if_else(
            R.equals('type'),
            R.always('FeatureCollection'),
            R.always(v)
        )(k)),
        # Map geometries to features: [{type: Feature, geometry: geometry}]
        lambda dct: R.merge(
            # Remove geometries
            R.omit(['geometries'], dct),
            # Add features containing the geometries
            dict(features=R.map(
                lambda geometry: dict(type='Feature', geometry=geometry),
                R.prop_or([], 'geometries', dct))
            )
        ),
    )(ast.literal_eval(R.prop(context.field_name, resource).json))
    # Identify the keys that are actually in resource[json_field_name]
    all_selections = R.filter(
        lambda key: key in json,
        selections
    )
    # Pick out the values that we want
    result = R.pick(all_selections, json)

    # Return in the standard Graphene DataTuple
    return namedtuple('DataTuple', R.keys(result))(*R.values(result))
Ejemplo n.º 3
0
from rescape_python_helpers import ramda as R
from rescape_graphene import resolver_for_dict_field, resolver_for_dict_list
from graphene import ObjectType, String, Float, List, Field, Int, Boolean

stage_data_fields = dict(key=dict(type=String),
                         name=dict(type=String),
                         targets=dict(type=String,
                                      type_modifier=lambda typ: List(typ)))

StageDataType = type(
    'StageDataType',
    (ObjectType, ),
    R.map_with_obj(
        # If we have a type_modifier function, pass the type to it, otherwise simply construct the type
        lambda k, v: R.prop_or(lambda typ: typ(), 'type_modifier', v)
        (R.prop('type', v)),
        stage_data_fields))

resource_settings_data_fields = dict(
    defaultLocation=dict(type=Float, type_modifier=lambda typ: List(Float)),
    unit=dict(type=String),
    columns=dict(type=String, type_modifier=lambda typ: List(typ)),
    stageKey=dict(type=String),
    valueKey=dict(type=String),
    locationKey=dict(type=String),
    nodeNameKey=dict(type=String),
    nodeColorKey=dict(type=String),
    linkColorKey=dict(type=String),
    stages=dict(
        type=StageDataType,
        graphene_type=StageDataType,
 def make_fields_unique_if_needed(scope_obj):
     # If a field needs to be unique, like a key, call it's unique_with method
     return R.map_with_obj(
         lambda key, value: R.item_str_path_or(
             R.identity, f'field_config.{key}.unique_with',
             related_model_scope_config)(scope_obj), scope_obj)