Ejemplo n.º 1
0
    async def resolve_type(self, root, info, **args):
        """This method resolves each field of each type
        If it's a scalar then the default resolver(from the graphql package) is executed,
        if not, we look for a method in our defined types"""
        graphql_type_to_return = get_named_type(info.return_type)
        graphql_parent_type = info.parent_type

        #get the class in charge of resolve the current field
        resolver_class = self._get_resolver_class(not root,
                                                  graphql_type_to_return,
                                                  graphql_parent_type)

        if not resolver_class and not is_leaf_type(graphql_type_to_return):
            msg = 'resolver class %s not found' % resolver_class.__name__
            self.logger.warning(msg)
            raise Exception(msg)

# instantiate class and get method resolver name
        resolver_class_instance = resolver_class(self.datasource)
        resolver_method_name = self._get_resolver_method(
            graphql_type_to_return, graphql_parent_type, info.field_name)

        # find and execute the resolver
        if not hasattr(resolver_class_instance, resolver_method_name):
            if is_leaf_type(graphql_type_to_return):
                return default_field_resolver(root, info, **args)

            msg = 'resolver method %s.%s not found' % (resolver_class.__name__,
                                                       resolver_method_name)
            self.logger.warning(msg)
            raise Exception(msg)

        try:
            query_resolver = getattr(resolver_class_instance,
                                     resolver_method_name)
            if asyncio.iscoroutinefunction(query_resolver):
                respose = await query_resolver(root, info, **args)
            else:
                respose = query_resolver(root, info, **args)

            del resolver_class_instance
            return respose
            #self.datasource.session.commit()
        except Exception as e:
            self.logger.error(str(e), exc_info=True)
            if self.datasource:
                self.datasource.session.rollback()
            raise
Ejemplo n.º 2
0
        def wrapper(filter_operation_info, context, parameters, *args, **kwargs):
            """Check that the type on which the operator operates is a scalar leaf type."""
            if 'operator' in kwargs:
                current_operator = kwargs['operator']
            else:
                # Because "operator" is from an enclosing scope, it is immutable in Python 2.x.
                current_operator = operator

            if not is_leaf_type(filter_operation_info.field_type):
                raise GraphQLCompilationError(u'Cannot apply "{}" filter to non-leaf type'
                                              u'{}'.format(current_operator, filter_operation_info))
            return f(filter_operation_info, context, parameters, *args, **kwargs)
Ejemplo n.º 3
0
    def _get_resolver_class(self, root, return_type, parent_type):
        """Return a Type module if exists, None overwise"""

        # Looking for the resolver class in charge to resolve the query/mutation
        if root and not is_leaf_type(return_type):
            resolver_class_name = return_type.name.lower()
        else:
            # the one to resolve is always the parent when I'm now quering from the root
            # or I'm a scalar
            resolver_class_name = parent_type.name.lower()

        resolver_class = load_type(
            os.path.join(self.types_path, resolver_class_name) + '.py')

        return resolver_class if resolver_class else None
Ejemplo n.º 4
0
    def _get_resolver_method(self, return_type, parent_type, field_name):
        """Returns the name of the method to be called in order to resolve the query
        :returns: string
        """
        # Look for same field name as defined in the Query or Mutation root objects
        if is_leaf_type(return_type):
            prefix = 'resolve_field_'
            resolver_method = field_name.lower()
        elif parent_type.name == 'Query':
            prefix = 'resolve_'
            resolver_method = field_name.lower()
        elif parent_type.name == 'Mutation':
            prefix = 'resolve_mutation_'
            resolver_method = field_name.lower()
        else:
            prefix = 'resolve_type_'
            resolver_method = return_type.name.lower()

        return '{prefix}{name}'.format(prefix=prefix, name=resolver_method)