예제 #1
0
def test_GET_functionality_allows_GET_with_query_param():
    wsgi = graphql_wsgi(TestSchema)

    c = Client(wsgi)
    response = c.get('/', {'query': '{test}'})

    assert response.json == {'data': {'test': 'Hello World'}}
예제 #2
0
파일: views.py 프로젝트: ecreall/nova-ideo
def graphqlview(context, request):  #pylint: disable=W0613
    token = request.headers.get('X-Api-Key', '')
    is_private = getattr(request.root, 'only_for_members', False)
    if is_private and not auth_user(token, request):
        response = HTTPUnauthorized()
        response.content_type = 'application/json'
        return response

    if request.method == 'OPTIONS':
        response = Response(status=200, body=b'')
        response.headerlist = []  # we have to reset headerlist
        response.headerlist.extend(
            (
                ('Access-Control-Allow-Origin', '*'),
                ('Access-Control-Allow-Headers', 'Content-Type'),
            )
        )
    else:
        solver = graphql_wsgi(schema)
        response = solver(request)
        response.headerlist.append(
            ('Access-Control-Allow-Origin', '*')
        )

    return response
예제 #3
0
def test_POST_functionality_allows_POST_with_url_encoding():
    wsgi = graphql_wsgi(TestSchema)

    c = Client(wsgi)

    response = c.post('/', {'query': b'{test}'})

    assert response.json == {'data': {'test': 'Hello World'}}
예제 #4
0
def test_error_handling_handles_unsupported_http_methods():
    wsgi = graphql_wsgi(TestSchema, pretty=True)

    c = Client(wsgi)
    response = c.put('/?query={test}', status=405)

    assert response.json == {
        'errors': [{'message': 'GraphQL only supports GET and POST requests.'}]
    }
예제 #5
0
def test_error_handling_handles_errors_caused_by_a_lack_of_query():
    wsgi = graphql_wsgi(TestSchema, pretty=True)

    c = Client(wsgi)
    response = c.get('/', status=400)

    assert response.json == {
        'errors': [{'message': 'Must provide query string.'}]
    }
예제 #6
0
def test_POST_functionality_url_encoded_query_with_GET_variable_values():
    wsgi = graphql_wsgi(TestSchema)

    c = Client(wsgi)

    response = c.post(
        "/?variables=%s" % json.dumps({'who': 'Dolly'}),
        {'query': b'query helloWho($who: String){ test(who: $who) }'})

    assert response.json == {'data': {'test': 'Hello Dolly'}}
예제 #7
0
def test_POST_functionaly_POST_raw_text_query_with_GET_variable_values():
    wsgi = graphql_wsgi(TestSchema)

    c = Client(wsgi)

    response = c.post("/?variables=%s" % json.dumps({'who': 'Dolly'}),
                      b'query helloWho($who: String){ test(who: $who) }',
                      content_type='application/graphql')

    assert response.json == {'data': {'test': 'Hello Dolly'}}
예제 #8
0
def test_POST_functionality_allows_other_UTF_charsets():
    wsgi = graphql_wsgi(TestSchema)

    c = Client(wsgi)

    response = c.post('/',
                      u'{ test(who: "World") }'.encode('utf_16_le'),
                      content_type='application/graphql; charset=utf-16')

    assert response.json == {'data': {'test': 'Hello World'}}
def test_GET_functionality_allows_GET_with_query_param():
    wsgi = graphql_wsgi(TestSchema)

    c = Client(wsgi)
    response = c.get('/', {'query': '{test}'})

    assert response.json == {
        'data': {
            'test': 'Hello World'
        }
    }
예제 #10
0
def test_GET_functionality_allows_GET_with_variable_values():
    wsgi = graphql_wsgi(TestSchema)

    c = Client(wsgi)
    response = c.get(
        '/', {
            'query': 'query helloWho($who: String){ test(who: $who) }',
            'variables': json.dumps({'who': 'Dolly'})
        })

    assert response.json == {'data': {'test': 'Hello Dolly'}}
예제 #11
0
def test_error_handling_handles_plain_POST_text():
    wsgi = graphql_wsgi(TestSchema, pretty=True)

    c = Client(wsgi)
    response = c.post('/?variables=%s' % json.dumps({'who': 'Dolly'}),
                      'query helloWho($who: String){ test(who: $who) }',
                      content_type='text/plain',
                      status=400)
    assert response.json == {
        'errors': [{'message': 'Must provide query string.'}]
    }
예제 #12
0
def test_POST_functionality_supports_POST_JSON_query_with_JSON_variables():
    wsgi = graphql_wsgi(TestSchema)

    c = Client(wsgi)

    response = c.post_json(
        '/', {
            'query': 'query helloWho($who: String){ test(who: $who) }',
            'variables': json.dumps({'who': 'Dolly'})
        })

    assert response.json == {'data': {'test': 'Hello Dolly'}}
예제 #13
0
def test_error_handling_handles_unknown_encoding():
    wsgi = graphql_wsgi(TestSchema, pretty=True)

    c = Client(wsgi)

    response = c.post('/',
                      '!@#$%^*(&^$%#@',
                      headers={'Content-Encoding': 'garbage'},
                      status=415)
    assert response.json == {
        'errors': [{'message': 'Unsupported content encoding "garbage".'}]
    }
예제 #14
0
def test_error_handling_handles_unsupported_utf_charset():
    wsgi = graphql_wsgi(TestSchema, pretty=True)

    c = Client(wsgi)

    response = c.post('/',
                      '{ test(who: "World") }',
                      content_type='application/graphql; charset=utf-53',
                      status=415)
    assert response.json == {
        'errors': [{'message': 'Unsupported charset "UTF-53".'}]
    }
예제 #15
0
def test_error_handling_handles_invalid_JSON_bodies():
    wsgi = graphql_wsgi(TestSchema, pretty=True)

    c = Client(wsgi)
    response = c.post('/',
                      '{"query":',
                      content_type='application/json',
                      status=400)

    assert response.json == {
        'errors': [{'message': 'POST body sent invalid JSON.'}]
    }
예제 #16
0
def test_POST_functionality_allows_POST_with_JSON_encoding():
    wsgi = graphql_wsgi(TestSchema)

    c = Client(wsgi)

    response = c.post_json('/', {'query': '{test}'})

    assert response.json == {
        'data': {
            'test': 'Hello World'
        }
    }
예제 #17
0
def test_error_handling_handles_poorly_formed_variables():
    wsgi = graphql_wsgi(TestSchema, pretty=True)

    c = Client(wsgi)

    response = c.get('/', {
        'variables': 'who:You',
        'query': 'query helloWho($who: String){ test(who: $who) }'
    }, status=400)

    assert response.json == {
        'errors': [{'message': 'Variables are invalid JSON.'}]
    }
예제 #18
0
def test_error_handling_functionality_handles_field_errors_caught_by_graphql():
    wsgi = graphql_wsgi(TestSchema, pretty=True)

    c = Client(wsgi)
    response = c.get('/', {'query': '{thrower}'})

    assert response.json == {
        'data': None,
        'errors': [{
            'message': 'Throws!',
            'locations': [{'line': 1, 'column': 2}]
        }]
    }
예제 #19
0
def test_POST_functionality_url_encoded_query_with_GET_variable_values():
    wsgi = graphql_wsgi(TestSchema)

    c = Client(wsgi)

    response = c.post("/?variables=%s" % json.dumps({'who': 'Dolly'}), {
        'query': 'query helloWho($who: String){ test(who: $who) }'
    })

    assert response.json == {
        'data': {
            'test': 'Hello Dolly'
        }
    }
예제 #20
0
def test_POST_functionaly_POST_raw_text_query_with_GET_variable_values():
    wsgi = graphql_wsgi(TestSchema)

    c = Client(wsgi)

    response = c.post("/?variables=%s" % json.dumps({'who': 'Dolly'}),
                      'query helloWho($who: String){ test(who: $who) }',
                      content_type='application/graphql')

    assert response.json == {
        'data': {
            'test': 'Hello Dolly'
        }
    }
예제 #21
0
def test_GET_functionality_allows_GET_with_variable_values():
    wsgi = graphql_wsgi(TestSchema)

    c = Client(wsgi)
    response = c.get('/', {
        'query': 'query helloWho($who: String){ test(who: $who) }',
        'variables': json.dumps({'who': 'Dolly'})
    })

    assert response.json == {
        'data': {
            'test': 'Hello Dolly'
        }
    }
예제 #22
0
def test_POST_functionality_allows_other_UTF_charsets():
    wsgi = graphql_wsgi(TestSchema)

    c = Client(wsgi)

    response = c.post('/',
                      u'{ test(who: "World") }'.encode('utf_16_le'),
                      content_type='application/graphql; charset=utf-16')

    assert response.json == {
        'data': {
            'test': 'Hello World'
        }
    }
예제 #23
0
def test_POST_functionality_variables_in_json_POST_body_not_encoded():
    wsgi = graphql_wsgi(TestSchema)

    c = Client(wsgi)

    response = c.post_json(
        '/', {
            'query': 'query helloWho($who: String){ test(who: $who) }',
            'variables': {
                'who': 'Dolly'
            }
        })

    assert response.json == {'data': {'test': 'Hello Dolly'}}
예제 #24
0
def graphql_api(request):
    if request.method == 'OPTIONS':
        response = Response(status=200, body=b'')
        response.headerlist = []  # we have to reset headerlist
        response.headerlist.extend((
            ('Access-Control-Allow-Origin', '*'),
            ('Access-Control-Allow-Headers', 'Content-Type'),
        ))
    else:
        solver = graphql_wsgi(schema)
        response = solver(request)
        response.headerlist.append(('Access-Control-Allow-Origin', '*'))

    return response
예제 #25
0
def test_error_handling_handles_syntax_errors_caught_by_graphql():
    wsgi = graphql_wsgi(TestSchema, pretty=True)

    c = Client(wsgi)
    response = c.get('/', {'query': 'syntaxerror'}, status=400)

    assert response.json == {
        'data': None,
        'errors': [{
            'message': ('Syntax Error GraphQL request (1:1) '
                        'Unexpected Name "syntaxerror"\n\n1: syntaxerror\n'
                        '   ^\n'),
            'locations': [{'line': 1, 'column': 1}]
        }]
    }
예제 #26
0
def test_POST_functionality_supports_POST_JSON_query_with_JSON_variables():
    wsgi = graphql_wsgi(TestSchema)

    c = Client(wsgi)

    response = c.post_json('/', {
        'query': 'query helloWho($who: String){ test(who: $who) }',
        'variables': json.dumps({'who': 'Dolly'})
    })

    assert response.json == {
        'data': {
            'test': 'Hello Dolly'
        }
    }
예제 #27
0
def test_POST_functionality_variables_in_json_POST_body_not_encoded():
    wsgi = graphql_wsgi(TestSchema)

    c = Client(wsgi)

    response = c.post_json('/', {
        'query': 'query helloWho($who: String){ test(who: $who) }',
        'variables': {'who': 'Dolly'}
    })

    assert response.json == {
        'data': {
            'test': 'Hello Dolly'
        }
    }
예제 #28
0
def test_error_handling_unknown_field():
    wsgi = graphql_wsgi(TestSchema, pretty=True)

    c = Client(wsgi)
    # I think this should actually be a 200 status
    response = c.get('/?query={unknown}', status=400)
    # locations formatting appears to be different here...
    assert response.json == {
        'data':
        None,
        'errors': [{
            "locations": [{
                'line': 1,
                'column': 2
            }],
            "message": u'Cannot query field "unknown" on type "Root".'
        }]
    }
예제 #29
0
파일: view.py 프로젝트: n0izn0iz/assembl
def graphql_api(request):
    slug = request.matchdict['discussion_slug']
    # Check permission
    discussion = models.Discussion.query.filter(
        models.Discussion.slug == slug).one()
    if discussion is None:
        raise HTTPUnauthorized()

    discussion_id = discussion.id
    # set discussion_id in request.matchdict which is use as context_value
    request.matchdict['discussion_id'] = discussion_id
    user_id = authenticated_userid(request) or Everyone
    permissions = get_permissions(user_id, discussion_id)
    if not discussion.user_can(user_id, CrudPermissions.READ, permissions):
        raise HTTPUnauthorized()

    solver = graphql_wsgi(Schema)
    return solver(request)
예제 #30
0
def test_error_handling_unknown_field():
    wsgi = graphql_wsgi(TestSchema, pretty=True)

    c = Client(wsgi)
    # I think this should actually be a 200 status
    response = c.get('/?query={unknown}', status=400)
    # locations formatting appears to be different here...
    assert response.json == {
        'data': None,
        'errors': [
            {
                "locations": [
                    {'line': 1,
                     'column': 2}
                ],
                "message": 'Cannot query field "unknown" on "Root".'
            }
        ]
    }
예제 #31
0
def graphqlview(context, request):  #pylint: disable=W0613
    token = request.headers.get('X-Api-Key', '')
    is_private = getattr(request.root, 'only_for_members', False)
    if is_private and not auth_user(token, request):
        response = HTTPUnauthorized()
        response.content_type = 'application/json'
        return response

    if request.method == 'OPTIONS':
        response = Response(status=200, body=b'')
        response.headerlist = []  # we have to reset headerlist
        response.headerlist.extend((
            ('Access-Control-Allow-Origin', '*'),
            ('Access-Control-Allow-Headers', 'Content-Type'),
        ))
    else:
        solver = graphql_wsgi(schema)
        response = solver(request)
        response.headerlist.append(('Access-Control-Allow-Origin', '*'))

    return response
예제 #32
0
def test_GET_functionality_allows_GET_with_operation_name():
    wsgi = graphql_wsgi(TestSchema)

    c = Client(wsgi)
    response = c.get('/', {
        'query': '''
          query helloYou { test(who: "You"), ...shared }
          query helloWorld { test(who: "World"), ...shared }
          query helloDolly { test(who: "Dolly"), ...shared }
          fragment shared on Root {
            shared: test(who: "Everyone")
          }''',
        'operationName': 'helloWorld'
    })

    assert response.json == {
        'data': {
            'test': 'Hello World',
            'shared': 'Hello Everyone'
        }
    }
예제 #33
0
def server():
    graphql = graphql_wsgi(StarWarsSchema)
    static = DirectoryApp('build', index_page=None)
    index = FileApp('index.html')
    graphiql = FileApp('graphiql.html')

    @wsgify
    def mount_graphql(request):
        if request.path_info_peek() == '':
            return request.get_response(index)
        if request.path_info_peek() == 'graphiql':
            return request.get_response(graphiql)
        popped = request.path_info_pop()
        if popped == 'graphql':
            return request.get_response(graphql)
        elif popped == 'static':
            return request.get_response(static)
        raise HTTPNotFound()

    server = make_server('127.0.0.1', 5000, mount_graphql)
    print "Python GraphQL server running on http://127.0.0.1:5000/graphql"
    print "React with Relay UI available on http://127.0.0.1:5000"
    server.serve_forever()
예제 #34
0
def test_pretty_printing_supports_pretty_printing():
    wsgi = graphql_wsgi(TestSchema, pretty=True)

    c = Client(wsgi)
    response = c.get('/', {'query': '{test}'})
    assert response.body == b'''\
예제 #35
0
from graphql_wsgi import graphql_wsgi
from .schema import schema

graphql = graphql_wsgi(schema.schema)


def graphqlview(context, request):
    return graphql(request)


def includeme(config):
    config.add_route('graphql', '/graphql')
    config.add_view(graphqlview, route_name='graphql')
    config.add_route('graphiql', '/graphiql')
    # TODO protect graphiql by a permission
    config.add_view(route_name='graphiql', renderer='graphiql.pt')
    config.add_static_view('graphiql', 'lac:graphql/build')
예제 #36
0
from graphql_wsgi import graphql_wsgi
from .schema import schema

graphql = graphql_wsgi(schema.schema)


def graphqlview(context, request):
    return graphql(request)


def includeme(config):
    config.add_route('graphql', '/graphql')
    config.add_view(graphqlview, route_name='graphql')
    config.add_route('graphiql', '/graphiql')
    # TODO protect graphiql by a permission
    config.add_view(route_name='graphiql', renderer='graphiql.pt')
    config.add_static_view('graphiql',
                           'lac:graphql/build')
예제 #37
0
def test_pretty_printing_supports_pretty_printing():
    wsgi = graphql_wsgi(TestSchema, pretty=True)

    c = Client(wsgi)
    response = c.get('/', {'query': '{test}'})
    assert response.body == '''\