|
15 | 15 | from demo.models import BaseEntity |
16 | 16 | from sqlmodel_graphql import GraphQLHandler |
17 | 17 |
|
18 | | -# GraphiQL HTML (loaded via CDN) |
19 | | -GRAPHIQL_HTML = """ |
20 | | -<!DOCTYPE html> |
21 | | -<html lang="en"> |
22 | | -<head> |
23 | | - <meta charset="utf-8" /> |
24 | | - <meta name="viewport" content="width=device-width, initial-scale=1" /> |
25 | | - <title>GraphiQL - SQLModel GraphQL Demo</title> |
26 | | - <style> |
27 | | - body { margin: 0; } |
28 | | - #graphiql { height: 100dvh; } |
29 | | - .loading { |
30 | | - height: 100%; |
31 | | - display: flex; |
32 | | - align-items: center; |
33 | | - justify-content: center; |
34 | | - font-size: 2rem; |
35 | | - font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; |
36 | | - } |
37 | | - </style> |
38 | | - <link rel="stylesheet" href="https://esm.sh/graphiql/dist/style.css" /> |
39 | | - <link rel="stylesheet" href="https://esm.sh/@graphiql/plugin-explorer/dist/style.css" /> |
40 | | - <script type="importmap"> |
41 | | - { |
42 | | - "imports": { |
43 | | - "react": "https://esm.sh/react@19.1.0", |
44 | | - "react/jsx-runtime": "https://esm.sh/react@19.1.0/jsx-runtime", |
45 | | - "react-dom": "https://esm.sh/react-dom@19.1.0", |
46 | | - "react-dom/client": "https://esm.sh/react-dom@19.1.0/client", |
47 | | - "@emotion/is-prop-valid": "data:text/javascript,", |
48 | | - "graphiql": "https://esm.sh/graphiql?standalone&external=react,react-dom,@graphiql/react,graphql", |
49 | | - "graphiql/": "https://esm.sh/graphiql/", |
50 | | - "@graphiql/plugin-explorer": "https://esm.sh/@graphiql/plugin-explorer?standalone&external=react,@graphiql/react,graphql", |
51 | | - "@graphiql/react": "https://esm.sh/@graphiql/react?standalone&external=react,react-dom,graphql,@emotion/is-prop-valid", |
52 | | - "@graphiql/toolkit": "https://esm.sh/@graphiql/toolkit?standalone&external=graphql", |
53 | | - "graphql": "https://esm.sh/graphql@16.11.0" |
54 | | - } |
55 | | - } |
56 | | - </script> |
57 | | -</head> |
58 | | -<body> |
59 | | - <div id="graphiql"> |
60 | | - <div class="loading">Loading GraphiQL...</div> |
61 | | - </div> |
62 | | - <script type="module"> |
63 | | - import React from 'react'; |
64 | | - import ReactDOM from 'react-dom/client'; |
65 | | - import { GraphiQL, HISTORY_PLUGIN } from 'graphiql'; |
66 | | - import { createGraphiQLFetcher } from '@graphiql/toolkit'; |
67 | | - import { explorerPlugin } from '@graphiql/plugin-explorer'; |
68 | | -
|
69 | | - const fetcher = createGraphiQLFetcher({ url: '/graphql' }); |
70 | | - const plugins = [HISTORY_PLUGIN, explorerPlugin()]; |
71 | | -
|
72 | | - function App() { |
73 | | - return React.createElement(GraphiQL, { |
74 | | - fetcher: fetcher, |
75 | | - plugins: plugins, |
76 | | - defaultQuery: `# Welcome to SQLModel GraphQL Demo! |
77 | | -# |
78 | | -# Try these queries: |
79 | | -
|
80 | | -# Get all users |
81 | | -query GetUsers { |
82 | | - users(limit: 10) { |
83 | | - id |
84 | | - name |
85 | | - email |
86 | | - } |
87 | | -} |
88 | | -
|
89 | | -# Get users with their posts (relationship) |
90 | | -query GetUsersWithPosts { |
91 | | - users(limit: 5) { |
92 | | - id |
93 | | - name |
94 | | - email |
95 | | - posts { |
96 | | - id |
97 | | - title |
98 | | - content |
99 | | - } |
100 | | - } |
101 | | -} |
102 | | -
|
103 | | -# Get a specific user by ID |
104 | | -query GetUser { |
105 | | - user(id: 1) { |
106 | | - id |
107 | | - name |
108 | | - email |
109 | | - } |
110 | | -} |
111 | | -
|
112 | | -# Get all posts |
113 | | -query GetPosts { |
114 | | - posts(limit: 10) { |
115 | | - id |
116 | | - title |
117 | | - content |
118 | | - author_id |
119 | | - } |
120 | | -} |
121 | | -
|
122 | | -# Get posts with their authors (relationship) |
123 | | -query GetPostsWithAuthors { |
124 | | - posts(limit: 5) { |
125 | | - id |
126 | | - title |
127 | | - content |
128 | | - author { |
129 | | - id |
130 | | - name |
131 | | - email |
132 | | - } |
133 | | - } |
134 | | -} |
135 | | -
|
136 | | -# Get posts by author |
137 | | -query GetPostsByAuthor { |
138 | | - posts_by_author(author_id: 1, limit: 10) { |
139 | | - id |
140 | | - title |
141 | | - content |
142 | | - } |
143 | | -} |
144 | | -
|
145 | | -# Create a new user |
146 | | -mutation CreateUser { |
147 | | - create_user(name: "Charlie", email: "charlie@example.com") { |
148 | | - id |
149 | | - name |
150 | | - email |
151 | | - } |
152 | | -} |
153 | | -
|
154 | | -# Create a new post |
155 | | -mutation CreatePost { |
156 | | - create_post(title: "My New Post", content: "Hello GraphQL!", author_id: 1) { |
157 | | - id |
158 | | - title |
159 | | - content |
160 | | - author_id |
161 | | - } |
162 | | -} |
163 | | -` |
164 | | - }); |
165 | | - } |
166 | | -
|
167 | | - const container = document.getElementById('graphiql'); |
168 | | - const root = ReactDOM.createRoot(container); |
169 | | - root.render(React.createElement(App)); |
170 | | - </script> |
171 | | -</body> |
172 | | -</html> |
173 | | -""" |
174 | | - |
175 | 18 |
|
176 | 19 | class GraphQLRequest(BaseModel): |
177 | 20 | """GraphQL request model.""" |
@@ -213,7 +56,7 @@ async def lifespan(app: FastAPI): |
213 | 56 | @app.get("/graphql", response_class=HTMLResponse) |
214 | 57 | async def graphiql_playground(): |
215 | 58 | """GraphiQL interactive query interface.""" |
216 | | - return GRAPHIQL_HTML |
| 59 | + return handler.get_graphiql_html() |
217 | 60 |
|
218 | 61 |
|
219 | 62 | @app.post("/graphql") |
|
0 commit comments