|
| 1 | +/* eslint-disable no-console */ |
| 2 | +import fastify from 'fastify'; |
| 3 | +import { getGraphQLParameters, processRequest, renderGraphiQL, shouldRenderGraphiQL } from 'graphql-helix'; |
| 4 | +import { envelop, useLogger, useSchema, useTiming } from '@envelop/core'; |
| 5 | +import { makeExecutableSchema } from '@graphql-tools/schema'; |
| 6 | + |
| 7 | +const sleep = (t = 1000) => new Promise(resolve => setTimeout(resolve, t)); |
| 8 | + |
| 9 | +const schema = makeExecutableSchema({ |
| 10 | + typeDefs: /* GraphQL */ ` |
| 11 | + type Query { |
| 12 | + hello: String! |
| 13 | + greetings: [String!] |
| 14 | + me: User! |
| 15 | + } |
| 16 | +
|
| 17 | + type User { |
| 18 | + id: ID! |
| 19 | + name: String! |
| 20 | + friends: [User!]! |
| 21 | + bio: String! |
| 22 | + } |
| 23 | +
|
| 24 | + type Subscription { |
| 25 | + clock: String |
| 26 | + } |
| 27 | + `, |
| 28 | + resolvers: { |
| 29 | + Query: { |
| 30 | + hello: () => 'World', |
| 31 | + greetings: async function* () { |
| 32 | + for (const greeting of ['hi', 'ho', 'sup', 'ola', 'bonjour']) { |
| 33 | + yield greeting; |
| 34 | + await sleep(); |
| 35 | + } |
| 36 | + }, |
| 37 | + me: () => ({ id: '1', name: 'Vanja' }), |
| 38 | + }, |
| 39 | + User: { |
| 40 | + bio: async () => { |
| 41 | + await sleep(1500); |
| 42 | + return 'I like turtles'; |
| 43 | + }, |
| 44 | + friends: async function* () { |
| 45 | + for (const user of [ |
| 46 | + { id: '2', name: 'Angela' }, |
| 47 | + { id: '3', name: 'Christopher' }, |
| 48 | + { id: '4', name: 'Titiana' }, |
| 49 | + { id: '5', name: 'Leonard' }, |
| 50 | + { id: '6', name: 'Ernesto' }, |
| 51 | + ]) { |
| 52 | + yield user; |
| 53 | + await sleep(1000); |
| 54 | + } |
| 55 | + }, |
| 56 | + }, |
| 57 | + Subscription: { |
| 58 | + clock: { |
| 59 | + subscribe: async function* () { |
| 60 | + while (true) { |
| 61 | + yield { clock: new Date().toString() }; |
| 62 | + await sleep(); |
| 63 | + } |
| 64 | + }, |
| 65 | + }, |
| 66 | + }, |
| 67 | + }, |
| 68 | +}); |
| 69 | + |
| 70 | +const graphiQLContent = /* GraphQL */ ` |
| 71 | + ## |
| 72 | + ## Welcome to the envelop graphql-helix demo. |
| 73 | + ## |
| 74 | + ## We prepared some operations for you to try out :) |
| 75 | + ## |
| 76 | +
|
| 77 | + # Basic query, nothing fancy here :) |
| 78 | + query BasicQuery { |
| 79 | + hello |
| 80 | + } |
| 81 | +
|
| 82 | + # Query using stream |
| 83 | + # |
| 84 | + # stream can be used on fields that return lists. |
| 85 | + # The resolver on the backend uses an async generator function for yielding the values. |
| 86 | + # In this demo there is a sleep of one second before the next value is yielded. |
| 87 | + # |
| 88 | + # stream is useful in scenarios where a lot of items must be sent to the client, but you want to show something as soon as possible |
| 89 | + # e.g. a social media feed. |
| 90 | + # |
| 91 | + # The initialCount argument specifies the amount of items sent within the initial chunk. |
| 92 | + query StreamQuery { |
| 93 | + greetings @stream(initialCount: 1) |
| 94 | + } |
| 95 | +
|
| 96 | + # Query using defer |
| 97 | + # |
| 98 | + # defer can be used on fragments in order to defer sending the result to the client, if it takes longer than the rest of the resolvers to yield an value. |
| 99 | + # The User.bio resolver on the backend uses a sleep of 2 seconds for deferring the resolution of the value. |
| 100 | + # Stream is useful when a certain resolver on your backend is slow, but not mandatory for showing something meaningful to your users. |
| 101 | + # An example for this would be a slow database call or third-party service. |
| 102 | + query DeferQuery { |
| 103 | + me { |
| 104 | + id |
| 105 | + name |
| 106 | + ... on User @defer { |
| 107 | + bio |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | +
|
| 112 | + # Query using both stream and defer |
| 113 | + # |
| 114 | + # Both directives can be used on the same operation! |
| 115 | + query MixedStreamAndDefer { |
| 116 | + me { |
| 117 | + id |
| 118 | + name |
| 119 | + ... on User @defer { |
| 120 | + bio |
| 121 | + } |
| 122 | + friends @stream(initialCount: 1) { |
| 123 | + id |
| 124 | + name |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | +
|
| 129 | + # Basic Subscription |
| 130 | + # |
| 131 | + # A subscription is a persistent connection between the graphql client and server and can be used for pushing events to the client. |
| 132 | + # |
| 133 | + # This subscription publishes the current date string every second. |
| 134 | + # Subscriptions are similar to defer and stream implemented via async generators. |
| 135 | + # Any event source such as Redis PubSub or MQTT can be wrapped in an async generator and used for backing the subscription. |
| 136 | + # The published event value is then passed on to the execution algorithm similar to mutations and subscriptions. |
| 137 | + subscription BasicSubscription { |
| 138 | + clock |
| 139 | + } |
| 140 | +`; |
| 141 | + |
| 142 | +const getEnveloped = envelop({ |
| 143 | + plugins: [useSchema(schema), useLogger(), useTiming()], |
| 144 | +}); |
| 145 | +const app = fastify(); |
| 146 | + |
| 147 | +app.route({ |
| 148 | + method: ['GET', 'POST'], |
| 149 | + url: '/graphql', |
| 150 | + async handler(req, res) { |
| 151 | + const { parse, validate, contextFactory, execute, schema } = getEnveloped(); |
| 152 | + const request = { |
| 153 | + body: req.body, |
| 154 | + headers: req.headers, |
| 155 | + method: req.method, |
| 156 | + query: req.query, |
| 157 | + }; |
| 158 | + |
| 159 | + if (shouldRenderGraphiQL(request)) { |
| 160 | + res.type('text/html'); |
| 161 | + res.send( |
| 162 | + renderGraphiQL({ |
| 163 | + defaultQuery: graphiQLContent |
| 164 | + .split('\n') |
| 165 | + .slice(1) |
| 166 | + .map(line => line.replace(' ', '')) |
| 167 | + .join('\n'), |
| 168 | + }) |
| 169 | + ); |
| 170 | + } else { |
| 171 | + const request = { |
| 172 | + body: req.body, |
| 173 | + headers: req.headers, |
| 174 | + method: req.method, |
| 175 | + query: req.query, |
| 176 | + }; |
| 177 | + const { operationName, query, variables } = getGraphQLParameters(request); |
| 178 | + const result = await processRequest({ |
| 179 | + operationName, |
| 180 | + query, |
| 181 | + variables, |
| 182 | + request, |
| 183 | + schema, |
| 184 | + parse, |
| 185 | + validate, |
| 186 | + execute, |
| 187 | + contextFactory, |
| 188 | + }); |
| 189 | + |
| 190 | + if (result.type === 'RESPONSE') { |
| 191 | + res.status(result.status); |
| 192 | + res.send(result.payload); |
| 193 | + } else if (result.type === 'MULTIPART_RESPONSE') { |
| 194 | + res.raw.writeHead(200, { |
| 195 | + Connection: 'keep-alive', |
| 196 | + 'Content-Type': 'multipart/mixed; boundary="-"', |
| 197 | + 'Transfer-Encoding': 'chunked', |
| 198 | + }); |
| 199 | + |
| 200 | + req.raw.on('close', () => { |
| 201 | + result.unsubscribe(); |
| 202 | + }); |
| 203 | + |
| 204 | + res.raw.write('---'); |
| 205 | + |
| 206 | + await result.subscribe(result => { |
| 207 | + const chunk = Buffer.from(JSON.stringify(result), 'utf8'); |
| 208 | + const data = [ |
| 209 | + '', |
| 210 | + 'Content-Type: application/json; charset=utf-8', |
| 211 | + 'Content-Length: ' + String(chunk.length), |
| 212 | + '', |
| 213 | + chunk, |
| 214 | + ]; |
| 215 | + |
| 216 | + if (result.hasNext) { |
| 217 | + data.push('---'); |
| 218 | + } |
| 219 | + |
| 220 | + res.raw.write(data.join('\r\n')); |
| 221 | + }); |
| 222 | + |
| 223 | + res.raw.write('\r\n-----\r\n'); |
| 224 | + res.raw.end(); |
| 225 | + } else { |
| 226 | + res.raw.writeHead(200, { |
| 227 | + 'Content-Type': 'text/event-stream', |
| 228 | + Connection: 'keep-alive', |
| 229 | + 'Cache-Control': 'no-cache', |
| 230 | + }); |
| 231 | + |
| 232 | + req.raw.on('close', () => { |
| 233 | + result.unsubscribe(); |
| 234 | + }); |
| 235 | + |
| 236 | + await result.subscribe(result => { |
| 237 | + res.raw.write(`data: ${JSON.stringify(result)}\n\n`); |
| 238 | + }); |
| 239 | + } |
| 240 | + } |
| 241 | + }, |
| 242 | +}); |
| 243 | + |
| 244 | +app.listen(3000, () => { |
| 245 | + console.log(`GraphQL server is running on http://127.0.0.1:3000/graphql`); |
| 246 | +}); |
0 commit comments