11import json
22
3- import graphene
43from flask import Flask , make_response
54from flask_graphql import GraphQLView
65from flask_sockets import Sockets
7- from rx import Observable
8-
9- from graphql_ws import GeventSubscriptionServer
10- from template import render_graphiql
11-
12-
13- class Query (graphene .ObjectType ):
14- base = graphene .String ()
15-
16-
17- class RandomType (graphene .ObjectType ):
18- seconds = graphene .Int ()
19- random_int = graphene .Int ()
20-
21-
22- class Subscription (graphene .ObjectType ):
23-
24- count_seconds = graphene .Int (up_to = graphene .Int ())
25-
26- random_int = graphene .Field (RandomType )
27-
28-
29- def resolve_count_seconds (root , info , up_to ):
30- return Observable .interval (1000 )\
31- .map (lambda i : "{0}" .format (i ))\
32- .take_while (lambda i : int (i ) <= up_to )
33-
34- def resolve_random_int (root , info ):
35- import random
36- return Observable .interval (1000 ).map (lambda i : RandomType (seconds = i , random_int = random .randint (0 , 500 )))
37-
38- schema = graphene .Schema (query = Query , subscription = Subscription )
396
7+ from graphql_ws .gevent import GeventSubscriptionServer
408
9+ from schema import schema
10+ from template import render_graphiql
4111
4212app = Flask (__name__ )
4313app .debug = True
@@ -48,12 +18,14 @@ def resolve_random_int(root, info):
4818def graphql_view ():
4919 return make_response (render_graphiql ())
5020
21+
5122app .add_url_rule (
5223 '/graphql' , view_func = GraphQLView .as_view ('graphql' , schema = schema , graphiql = False ))
5324
5425subscription_server = GeventSubscriptionServer (schema )
5526app .app_protocol = lambda environ_path_info : 'graphql-ws'
5627
28+
5729@sockets .route ('/subscriptions' )
5830def echo_socket (ws ):
5931 subscription_server .handle (ws )
0 commit comments