Browse Source

Added database query

Efren Yevale Varela 3 years ago
parent
commit
fc7a6d3016
1 changed files with 33 additions and 4 deletions
  1. 33 4
      app.js

+ 33 - 4
app.js

@@ -9,6 +9,7 @@ const http       = require("http");
 const jwksClient = require("jwks-rsa");
 const jwt        = require("jsonwebtoken");
 
+const { MongoClient }                            = require("mongodb");
 const { ApolloServer, AuthenticationError, gql } = require("apollo-server-express");
 const { ApolloServerPluginDrainHttpServer }      = require("apollo-server-core");
 
@@ -25,18 +26,40 @@ if (app.get("env") === "production") {
 const httpServer = http.createServer(app);
 
 const typeDefs = gql`
-  type Hello {
-    message: String
+  "Party definition."
+  type Party {
+    "Unique party identifier"
+    _id: ID!
+
+    "Full name, concatenation of first, last, and middle."
+    name: String!
+
+    """
+    Auth0 user identifier.
+    Application access granted when set.
+    """
+    user_id: String
+
+    "On-boarded in the application?."
+    onboarded: Boolean!
   }
 
+  "Query definitions."
   type Query {
-    hello: Hello
+    "Get party by identifier."
+    getParty(_id: ID!): Party!
   }
 `;
 
 const resolvers = {
   Query: {
-    hello: _ => ({ message: "Hello World!" })
+    getParty: async (parent, args, { user, dataSources }) => {
+      const parties = dataSources.mongodb.collection("parties");
+
+      const party = await parties.findOne({ _id: args._id });
+
+      return party;
+    }
   }
 };
 
@@ -54,9 +77,15 @@ const authentication = async context => {
   return { user };
 }
 
+const mongodb = new MongoClient(process.env.MONGODB_STRING, { useNewUrlParser: true, useUnifiedTopology: true });
+mongodb.connect();
+
 const apollo = new ApolloServer({
   typeDefs,
   resolvers,
+  dataSources: _ => ({
+    mongodb: mongodb.db(process.env.MONGODB_DB)
+  }),
   plugin: [ApolloServerPluginDrainHttpServer({ httpServer })],
   context: authentication
 });