File tree Expand file tree Collapse file tree 5 files changed +759
-0
lines changed
Expand file tree Collapse file tree 5 files changed +759
-0
lines changed Original file line number Diff line number Diff line change 1+ "use strict" ;
2+ const graphqlHTTP = require ( "express-graphql" ) ;
3+ const { buildSchema} = require ( "graphql" ) ;
4+ const express = require ( "express" ) ;
5+ const app = express ( ) ;
6+
7+ // Get the Mongoose models used for querying the database
8+ const { User, Group} = require ( "./models.js" ) ;
9+
10+ // Start up a GraphQL endpoint listening at /graphql
11+ app . use ( "/graphql" , graphqlHTTP ( {
12+ // We construct our GraphQL schema which has three types:
13+ // The User, Group, and Query types (through which all
14+ // queries for data are defined)
15+ schema : buildSchema ( `
16+ type Group {
17+ _id: String
18+ name: String
19+ }
20+
21+ type User {
22+ _id: String
23+ username: String
24+ group: Group
25+ }
26+
27+ type Query {
28+ user(id: String!): User
29+ users: [User]
30+ group(id: String!): Group
31+ groups: [Group]
32+ }
33+ ` ) ,
34+ // The methods that we'll use to get the data for our
35+ // main queries
36+ rootValue : {
37+ // Get a user based on the ID and return it as a Promise
38+ user ( { id} ) {
39+ return User . findById ( id ) ;
40+ } ,
41+ // Get an array of users and return them as a Promise
42+ users ( ) {
43+ return User . find ( { } ) ;
44+ } ,
45+ // Get a group based on the ID and return it as a Promise
46+ group ( { id} ) {
47+ return Group . findById ( id ) ;
48+ } ,
49+ // Get an array of groups and return them as a Promise
50+ groups ( ) {
51+ return Group . find ( { } ) ;
52+ } ,
53+ } ,
54+ // Display the GraphiQL web interface (for easy usage!)
55+ graphiql : true ,
56+ } ) ) ;
57+
58+ // Start the application, listening on port 3000
59+ app . listen ( 3000 ) ;
Original file line number Diff line number Diff line change 1+ "use strict" ;
2+ const mongoose = require ( "mongoose" ) ;
3+
4+ // Connect to the local MongoDB database named “testdb”
5+ mongoose . connect ( "mongodb://localhost/testdb" ,
6+ { useMongoClient : true } ) ;
7+
8+ // Use real promises
9+ mongoose . Promise = global . Promise ;
10+
11+ // Create a Group schema to be stored in the MongoDB database
12+ const GroupSchema = new mongoose . Schema ( {
13+ _id : String ,
14+ name : String ,
15+ } ) ;
16+
17+ // Turn that schema into a model that we can query
18+ const Group = mongoose . model ( "Group" , GroupSchema ) ;
19+
20+ // Create a User schema to be stored in the MongoDB database
21+ const UserSchema = new mongoose . Schema ( {
22+ _id : String ,
23+ username : String ,
24+ groupId : String ,
25+ } ) ;
26+
27+ // Retrieve the group associated with the user
28+ UserSchema . methods . group = function ( ) {
29+ // Use .exec() to ensure a true Promise is returned
30+ return Group . findById ( this . groupId ) . exec ( ) ;
31+ } ;
32+
33+ // Turn that schema into a model that we can query
34+ const User = mongoose . model ( "User" , UserSchema ) ;
35+
36+ module . exports = { User, Group} ;
You can’t perform that action at this time.
0 commit comments