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'}}
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
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'}}
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.'}] }
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.'}] }
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'}}
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'}}
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' } }
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'}}
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.'}] }
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'}}
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".'}] }
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".'}] }
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.'}] }
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' } }
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.'}] }
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}] }] }
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' } }
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' } }
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' } }
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_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'}}
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
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}] }] }
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' } }
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' } }
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".' }] }
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)
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".' } ] }
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
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' } }
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()
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'''\
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')
def test_pretty_printing_supports_pretty_printing(): wsgi = graphql_wsgi(TestSchema, pretty=True) c = Client(wsgi) response = c.get('/', {'query': '{test}'}) assert response.body == '''\