deno.land / x / pothos@release-1713397530 / server-example.ts

server-example.ts
نووسراو ببینە
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
import { Application, Router } from 'https://deno.land/x/oak@v7.3.0/mod.ts';// Currently the way helix exports graphiql doesn't work in deno, so we import the other parts directly, and import a very simple playground file from a gist.import { shouldRenderGraphiQL } from 'https://cdn.jsdelivr.net/gh/contrawork/graphql-helix@master/packages/deno/should-render-graphiql.ts';import { processRequest } from 'https://cdn.jsdelivr.net/gh/contrawork/graphql-helix@master/packages/deno/process-request.ts';import { getGraphQLParameters } from 'https://cdn.jsdelivr.net/gh/contrawork/graphql-helix@master/packages/deno/get-graphql-parameters.ts';import playground from 'https://gist.githubusercontent.com/hayes/5c99f7b4f71234452036fd88e142a825/raw/655245a052b10c2912a803c8a6d537096b73c10b/playground.ts';// Pothosimport SchemaBuilder from './packages/core/mod.ts';
// Create app and routerconst app = new Application();const router = new Router();
// Create a very simple schemaconst builder = new SchemaBuilder({});
builder.queryType({ fields: (t) => ({ hello: t.string({ args: { name: t.arg.string({}), }, resolve: (parent, { name }) => `hello, ${name || 'World'}`, }), }),});
const schema = builder.toSchema();
// Mount a route to serve the graphql API and playgroundrouter.all('/graphql', async (context) => { // parse request const request = { body: await context.request.body({}).value, headers: context.request.headers, method: context.request.method, query: context.request.url.searchParams, };
if (shouldRenderGraphiQL(request)) { context.response.body = playground({ endpoint: 'localhost:8080/graphql' });
return; } // Extract the GraphQL parameters from the request const { operationName, query, variables } = getGraphQLParameters(request);
// Validate and execute the query const result = await processRequest({ operationName, query, variables, request, schema, });
if (result.type === 'RESPONSE') { // We set the provided status and headers and just the send the payload back to the client result.headers.forEach(({ name, value }) => context.response.headers.set(name, value));
context.response.status = result.status; context.response.body = result.payload; } else { // Omitting other response types for brevity, see graphql-helix docs for more a complete implementation throw new Error('Unsupported result type'); }});
app.use(router.routes());app.use(router.allowedMethods());
console.log('Go to http://localhost:8080/graphql to open the playground');await app.listen({ port: 8080 });
pothos

Version Info

Tagged at
2 weeks ago