def test_prints_string_field_with_multiple_args():
    output = print_single_field_schema(
        GraphQLField(type=GraphQLString,
                     args=OrderedDict([('argOne', GraphQLArgument(GraphQLInt)),
                                       ('argTwo',
                                        GraphQLArgument(GraphQLString))])))

    assert output == '''
def test_prints_string_field_with_multiple_args_last_is_default():
    output = print_single_field_schema(
        GraphQLField(type=GraphQLString,
                     args=OrderedDict([
                         ('argOne', GraphQLArgument(GraphQLInt)),
                         ('argTwo', GraphQLArgument(GraphQLString)),
                         ('argThree',
                          GraphQLArgument(GraphQLBoolean, default_value=False))
                     ])))

    assert output == '''
def test_prints_input_type():
    InputType = GraphQLInputObjectType(
        name='InputType', fields={'int': GraphQLInputObjectField(GraphQLInt)})

    Root = GraphQLObjectType(
        name='Root',
        fields={
            'str':
            GraphQLField(GraphQLString,
                         args={'argOne': GraphQLArgument(InputType)})
        })

    Schema = GraphQLSchema(Root)
    output = print_for_test(Schema)

    assert output == '''
Example #4
0
 def build(cls):
     self = cls()
     output_fields = self.get_output_fields()
     output_fields.update(
         {'clientMutationId': GraphQLField(GraphQLNonNull(GraphQLString))})
     output_type = GraphQLObjectType(self.name + 'Payload',
                                     fields=output_fields)
     input_fields = self.get_input_fields()
     input_fields.update({
         'clientMutationId':
         GraphQLInputObjectField(GraphQLNonNull(GraphQLString))
     })
     input_arg = GraphQLArgument(
         GraphQLNonNull(
             GraphQLInputObjectType(name=self.name + 'Input',
                                    fields=input_fields)))
     return GraphQLField(output_type,
                         args={
                             'input': input_arg,
                         },
                         resolver=self._resolver)
def test_prints_string_field_with_non_null_int_arg():
    output = print_single_field_schema(
        GraphQLField(
            type=GraphQLString,
            args={'argOne': GraphQLArgument(GraphQLNonNull(GraphQLInt))}))
    assert output == '''
def test_prints_string_field_with_int_arg_with_default():
    output = print_single_field_schema(
        GraphQLField(
            type=GraphQLString,
            args={'argOne': GraphQLArgument(GraphQLInt, default_value=2)}))
    assert output == '''
Example #7
0
class EntityConnectionType(EntitySetType):
    
    @property
    def node_type(self):
        return EntityType.as_graphql(self)
    
    def get_edge_type(self):
        entity_name = super(EntitySetType, self).name
        name = "%sEdge" % entity_name
        if name in self.types_dict:
            return self.types_dict[name]
        edge_type = GraphQLObjectType(name, {
            'node': GraphQLField(self.node_type),
            'cursor': GraphQLField(GraphQLNonNull(GraphQLString))
        })
        self.types_dict[name] = edge_type
        return edge_type

    # FIXME
    # @property
    # def name(self):
    #     entity_name = super(EntityConnectionType, self).name
    #     return "%sConnection" % entity_name

    def get_page_info_type(self):
        return PageInfoType(self.types_dict).as_graphql()

    def as_graphql(self):
        entity_name = super(EntityConnectionType, self).name
        name = "%sConnection" % entity_name
        if name in self.types_dict:
            return self.types_dict[name]
        connection_type = GraphQLObjectType(name, {
            'pageInfo': GraphQLField(self.get_page_info_type()),
            'edges': GraphQLField(
                GraphQLList(self.get_edge_type()),
            ),
            'items': GraphQLField(
                GraphQLList(self.node_type),
            )
        })
        self.types_dict[name] = connection_type
        return connection_type
    
    arguments = {
        'before': GraphQLArgument(GraphQLString),
        'after': GraphQLArgument(GraphQLString),
        'first': GraphQLArgument(GraphQLInt),
        'last': GraphQLArgument(GraphQLInt),
    }
    
    def __call__(self, obj, kwargs, info):
        query = self.get_query(obj, **kwargs)
        page = self.paginate_query(query, **kwargs)
        edges = []
        for index, obj in enumerate(page):
            edges.append(as_object({
                'node': obj,
                'cursor': str(obj.id),
            }))
        
        get_id = lambda index: int(edges[index].cursor)
        has_next = edges and query.filter(lambda e: e.id > get_id(-1)).exists()
        has_prev = edges and query.filter(lambda e: e.id < get_id(0)).exists()
        
        return as_object({
            'pageInfo': as_object({
                'hasNextPage': has_next,
                'hasPreviousPage': has_prev,
            }),
            'edges': edges,
            'items': lambda: [e.node for e in edges],
        })
    
    def paginate_query(self, query, **kwargs):
        if 'before' in kwargs or 'last' in kwargs:
            cursor = kwargs.get('before')
            limit = kwargs.get('last')
            filter = lambda e: e.id < cursor
        else:
            filter = lambda e: e.id > cursor
            cursor = kwargs.get('after')
            limit = kwargs.get('first')

        if cursor is not None:
            cursor = int(cursor)
            # TODO only id is supported
            query = query.filter(filter)
        if limit is not None:
            query = query.limit(limit)
        return query
    
    def get_query(self, obj, order_by=None, **kwargs):
        # TODO forbid presence of both before and after in kwargs
        order_by = order_by or self.get_order_by()
        if 'before' in kwargs or 'last' in kwargs:
            order_by = order_by.desc()
        return EntitySetType.get_query(self, obj, order_by=order_by, **kwargs)        
Example #8
0
            GraphQLList(GraphQLString),
            description='Tags',
        ),
    },
    interfaces=[node_interface])

QueryRootType = GraphQLObjectType(
    name='Query',
    fields={
        'node':
        node_field,
        'User':
        GraphQLField(UserType, resolver=lambda *_: get_user()),
        'getPost':
        GraphQLField(PostType,
                     args={'id': GraphQLArgument(GraphQLInt)},
                     resolver=lambda obj, args, *_: get_post(args.get('id'))),
    })


class RelayMutation:
    name = None

    def get_input_fields(self):
        raise NotADirectoryError

    def get_output_fields(self):
        raise NotImplementedError

    def mutate(self, **params):
        raise NotImplementedError