Advertisement
Guest User

gql?

a guest
Jan 26th, 2020
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. import { ISockEvent, Sock, DisconnectEvent } from "../lib/BetterWS/mod.ts";
  2. import { basename, extname, resolve } from "../deps.ts";
  3. /// <reference types="graphql" />
  4. import graphql from "./dist/graphql.js";
  5. import {readFile as justRead} from "../base/util.ts"
  6.  
  7. import { createRequire } from "https://deno.land/std/node/module.ts";
  8. // const require_ = createRequire(import.meta.url);
  9. // const GraphQL = require_("graphql");
  10. // const {makeExecutableSchema, IResolvers} = require_("graphql-tools");
  11.  
  12.  
  13. // type PluginsHandler = (data: string) => Promise<void>;
  14. // type PluginsMapper = {
  15. // [key in string]: PluginsHandler
  16. // }
  17. // type PluginsResolver = IResolvers<string, PluginsMapper>
  18. type PluginsResolver = {
  19. [key in string]: any
  20. }
  21. const { readDir } = Deno;
  22.  
  23.  
  24. const basePath = './resolver';
  25. const currentFolder = import.meta.url.split("/").slice(-2,-1)[0];
  26. const dirList = await readDir(resolve(`./${currentFolder}`,`${basePath}`));
  27. const typeDefs = await justRead(resolve(`./${currentFolder}`,`./scheme/index.graphql`))
  28. const resolvers: PluginsResolver = {};
  29. {
  30. (
  31. await Promise.all(
  32. dirList.filter(
  33. (file: Deno.FileInfo) => file.isFile()
  34. ).map(
  35. (file: Deno.FileInfo) => {
  36. const path = `${basePath}/${file.name}`;
  37. return import(path)
  38. }
  39. )
  40. )
  41. ).reduce(
  42. (accr, moduleObj, nth) => {
  43. const fileName = dirList[nth].name;
  44. const event = basename(fileName,extname(fileName));
  45. accr[event] = moduleObj.default;
  46. return accr;
  47. },
  48. resolvers
  49. );
  50. }
  51. const schema = graphql.buildSchema(typeDefs);
  52. //TODO : divied graphQL payload
  53. export class SocketRunner implements ISockEvent{
  54. async onMessage (socket:Sock, event: string, data: string): Promise<void> {
  55. if(event === 'graphql') {
  56. const result = await graphql.graphql(schema, data, resolvers);
  57. socket.send(JSON.stringify(result));
  58. }
  59. }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement