File size: 4,850 Bytes
c76addc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import requests
import os
import json
from utils import TEMP_DIR

def connect_graphql(graphql_url, api_token, graphql_token_header, session_hash):
    try:
        # Create the GraphQL Introspection Query
        query = """

                query IntrospectionQuery {

                    __schema {

                    queryType { name }

                    mutationType { name }

                    subscriptionType { name }

                    types {

                        ...FullType

                    }

                    directives {

                        name

                        description

                        locations

                        args {

                        ...InputValue

                        }

                    }

                    }

                }

                fragment FullType on __Type {

                    kind

                    name

                    description

                    fields(includeDeprecated: true) {

                    name

                    description

                    args {

                        ...InputValue

                    }

                    type {

                        ...TypeRef

                    }

                    isDeprecated

                    deprecationReason

                    }

                    inputFields {

                    ...InputValue

                    }

                    interfaces {

                    ...TypeRef

                    }

                    enumValues(includeDeprecated: true) {

                    name

                    description

                    isDeprecated

                    deprecationReason

                    }

                    possibleTypes {

                    ...TypeRef

                    }

                }

                fragment InputValue on __InputValue {

                    name

                    description

                    type { ...TypeRef }

                    defaultValue

                }

                fragment TypeRef on __Type {

                    kind

                    name

                    ofType {

                    kind

                    name

                    ofType {

                        kind

                        name

                        ofType {

                        kind

                        name

                        ofType {

                            kind

                            name

                            ofType {

                            kind

                            name

                            ofType {

                                kind

                                name

                                ofType {

                                kind

                                name

                                }

                            }

                            }

                        }

                        }

                    }

                    }

                }

                """
        print("Connecting to GraphQL Endpoint")
    
        # Access a database
        headers = {"Content-Type": "application/json"}
        if graphql_token_header and api_token:
            headers[graphql_token_header] = api_token
        response = requests.post(graphql_url, headers=headers, json={"query": query})
        response.raise_for_status()

        introspection_result = response.json()

        client_schema = introspection_result["data"]["__schema"]

        #Generate the list of types
        type_names_query = """

                query IntrospectionQuery {

                    __schema {

                        types {

                            name

                        }

                    }

                }

            """
        types_response = requests.post(graphql_url, headers=headers, json={"query": type_names_query})

        types_response_results =types_response.json()

        types_names = types_response_results["data"]

        type_names = []
        for name in types_names["__schema"]["types"]:
            type_names.append(name["name"])

        session_path = 'graphql'

        dir_path = TEMP_DIR / str(session_hash) / str(session_path)
        os.makedirs(dir_path, exist_ok=True)

        with open(f'{dir_path}/schema.json', 'w') as fp:
            json.dump(client_schema, fp, indent=2)

        return ["success","<p style='color:green;text-align:center;font-size:18px;'>GraphQL API connected successful</p>", type_names]
    except Exception as e:
        print("GraphQL CONNECTION ERROR")
        print(e)
        return ["error",f"<p style='color:red;text-align:center;font-size:18px;font-weight:bold;'>ERROR: {e}</p>"]