File tree 8 files changed +3940
-0
lines changed
8 files changed +3940
-0
lines changed Original file line number Diff line number Diff line change
1
+ {
2
+ "presets" : [" @babel/preset-env" ]
3
+ }
Original file line number Diff line number Diff line change
1
+ node_modules
Original file line number Diff line number Diff line change
1
+ {
2
+ "name" : " graphql-mongo-server" ,
3
+ "version" : " 1.0.0" ,
4
+ "description" : " " ,
5
+ "main" : " index.js" ,
6
+ "scripts" : {
7
+ "start" : " nodemon --exec babel-node src/index.js"
8
+ },
9
+ "keywords" : [],
10
+ "author" : " " ,
11
+ "license" : " ISC" ,
12
+ "devDependencies" : {
13
+ "@babel/cli" : " ^7.4.3" ,
14
+ "@babel/core" : " ^7.4.3" ,
15
+ "@babel/node" : " ^7.2.2" ,
16
+ "@babel/preset-env" : " ^7.4.3" ,
17
+ "nodemon" : " ^1.18.11"
18
+ },
19
+ "dependencies" : {
20
+ "apollo-server-express" : " ^2.4.8" ,
21
+ "express" : " ^4.16.4" ,
22
+ "graphql" : " ^14.2.1" ,
23
+ "mongoose" : " ^5.5.1"
24
+ }
25
+ }
Original file line number Diff line number Diff line change
1
+ import { ApolloServer , gql } from "apollo-server-express" ;
2
+ import express from "express" ;
3
+ import mongoose from "mongoose" ;
4
+ import { resolvers } from "./resolvers" ;
5
+ import { typeDefs } from "./typeDefs" ;
6
+
7
+ const startServer = async ( ) => {
8
+ const app = express ( ) ;
9
+
10
+ const server = new ApolloServer ( {
11
+ typeDefs,
12
+ resolvers
13
+ } ) ;
14
+
15
+ server . applyMiddleware ( { app } ) ;
16
+
17
+ await mongoose . connect ( "mongodb://localhost:27017/test3" , {
18
+ useNewUrlParser : true
19
+ } ) ;
20
+
21
+ app . listen ( { port : 4000 } , ( ) =>
22
+ console . log ( `🚀 Server ready at http://localhost:4000${ server . graphqlPath } ` )
23
+ ) ;
24
+ } ;
25
+
26
+ startServer ( ) ;
Original file line number Diff line number Diff line change
1
+ import mongoose from "mongoose" ;
2
+
3
+ export const Cat = mongoose . model ( "Cat" , { name : String } ) ;
Original file line number Diff line number Diff line change
1
+ import { Cat } from "./models/Cat" ;
2
+
3
+ export const resolvers = {
4
+ Query : {
5
+ hello : ( ) => "hi" ,
6
+ cats : ( ) => Cat . find ( )
7
+ } ,
8
+ Mutation : {
9
+ createCat : async ( _ , { name } ) => {
10
+ const kitty = new Cat ( { name } ) ;
11
+ await kitty . save ( ) ;
12
+ return kitty ;
13
+ }
14
+ }
15
+ } ;
Original file line number Diff line number Diff line change
1
+ import { gql } from "apollo-server-express" ;
2
+
3
+ export const typeDefs = gql `
4
+ type Query {
5
+ hello: String!
6
+ cats: [Cat!]!
7
+ }
8
+
9
+ type Cat {
10
+ id: ID!
11
+ name: String!
12
+ }
13
+
14
+ type Mutation {
15
+ createCat(name: String!): Cat!
16
+ }
17
+ ` ;
You can’t perform that action at this time.
0 commit comments