Advertisement
Guest User

Untitled

a guest
May 8th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1. /**
  2. * graphQL.js
  3. *
  4. * Created by jrootham on 18/04/16.
  5. *
  6. * Copyright © 2016 Jim Rootham
  7. */
  8.  
  9. import graphqlHTTP from "express-graphql";
  10. import {
  11. graphql,
  12. GraphQLSchema,
  13. GraphQLObjectType,
  14. GraphQLString,
  15. GraphQLNonNull,
  16. GraphQLBoolean
  17. } from 'graphql';
  18. import {hash} from "bcrypt"
  19. import {connect, User} from "../database/defineDB";
  20.  
  21. const GraphUser = new GraphQLObjectType({
  22. name: "GraphUser",
  23. description: "A user object",
  24. fields: () => {
  25. return {
  26. name: {
  27. type: GraphQLString,
  28. resolve: (_, __, session) => {
  29. console.log("resolve name", session);
  30. let name = "";
  31. if (session.signedOn) {
  32. return User.findById(session.userId).then (user => {
  33. return user.name;
  34. });
  35. }
  36.  
  37. console.log("name", name);
  38. return name;
  39. }
  40. },
  41. signedOn: {
  42. type: GraphQLBoolean,
  43. resolve: (_, __, session) => {
  44. return session.signedOn;
  45. }
  46. },
  47. existed: {
  48. type: GraphQLBoolean,
  49. resolve: (_, __, session) => {
  50. return session.existed;
  51. }
  52. }
  53. }
  54. }
  55. });
  56.  
  57. const query = new GraphQLObjectType({
  58. name: 'Queries',
  59. fields: () => {
  60. return {
  61. graphUser: {
  62. type: GraphUser
  63. }
  64. }
  65. }
  66. });
  67.  
  68. const mutation = new GraphQLObjectType({
  69. name: 'Mutations',
  70. description: "Modification actions",
  71. fields() {
  72. return {
  73. registerUser: {
  74. type: GraphUser,
  75. args: {
  76. name: {
  77. type: new GraphQLNonNull(GraphQLString)
  78. },
  79. password: {
  80. type: new GraphQLNonNull(GraphQLString)
  81. }
  82. },
  83. resolve(_, args, session) {
  84. console.log("resolve", args);
  85. User.findOne({where:{name:args.name}}).then(user => {
  86. console.log("After find", user);
  87. if (user === null) {
  88. const getHash = new Promise(
  89. resolve => {
  90. hash(args.password, 10, (err, hash) => {
  91. resolve(hash);
  92. });
  93. }
  94. );
  95.  
  96. const result = getHash.then(hash => {
  97. connect.models.user.create({
  98. name: args.name,
  99. password: hash
  100. }).then(user => {
  101. session.userId = user.id;
  102. session.signedOn = true;
  103. session.existed = false;
  104.  
  105. console.log("session user", session.userId);
  106. return user;
  107. });
  108.  
  109. console.log(result);
  110. return result;
  111. });
  112. }
  113. else {
  114. session.userId = 0;
  115. session.signedOn = false;
  116. session.existed = true;
  117. console.log("existed");
  118. return GraphUser;
  119. }
  120. });
  121. }
  122. }
  123. }
  124. }
  125. });
  126.  
  127. const schema = new GraphQLSchema({
  128. query: query,
  129. mutation: mutation
  130. });
  131.  
  132. export const useGraphQL = app => {
  133. app.use('/graphql', graphqlHTTP(request =>({
  134. schema: schema,
  135. context: request.session,
  136. formatError: error => ({
  137. message: error.message,
  138. locations: error.locations,
  139. stack: error.stack
  140. }),
  141. graphiql:true
  142. })));
  143. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement