Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server #51

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions api/src/db/db.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,34 @@
{
"user": {
"id": "jBWMVGjm50l6LGwepDoty",
"username": "frontendmaster"
"username": "frontendmaster",
"pets": [{
"id": 1
}, {
"id":3
}]
},
"pet": []
}
"pet": [
{
"id": 1,
"createdAt": "2011",
"name": "Rosco",
"type": "DOG",
"user": "jBWMVGjm50l6LGwepDoty"
},
{
"id": 2,
"createdAt": "2016",
"name": "Bob",
"type": "CAT",
"user": "jBWMVGjm50l6LGwepDoty"
},
{
"id": 3,
"createdAt": 1661250142758,
"name": "Cosmo",
"type": "DOG",
"user": "jBWMVGjm50l6LGwepDoty"
}
]
}
49 changes: 41 additions & 8 deletions api/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,52 @@

module.exports = {
Query: {

pets(_, {input}, context) {
console.log('Q.Pets')
const result = context.models.Pet.findMany(input)
return result
},

pet(_, {input}, context) {
console.log('Q.Pet')
const result = context.models.Pet.findOne(input)
return result
},

user(_, __, context) {
console.log('Q.User')
return context.user
}
},
Mutation: {

createPet(_, {input}, context) {
const resultPet = context.models.Pet.create(input)
return resultPet
}
},
Pet: {
img(pet) {
return pet.type === 'DOG'
? 'https://placedog.net/300/300'
: 'http://placekitten.com/300/300'
user(pet, __, context) {
console.log('PET.user')
return context.user
}
},
User: {

}
pets(user, __, context) {
console.log('USER.pets')
const petResult = user.pets.map((pet) => {
return context.models.Pet.findOne(pet)
})
return petResult

// let petArr = []
// petIds.forEach((pet) => {
// const petVal = context.models.Pet.findMany(pet)
// petArr.push(petVal)
// })
// console.log(petArr, 'PETARR')
// return petArr.flat()

}
},

}
34 changes: 33 additions & 1 deletion api/src/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,40 @@ const { gql } = require('apollo-server')
* Type Definitions for our Schema using the SDL.
*/
const typeDefs = gql`

type User {
id: ID!
username: String!
pets: [Pet]!
}

type Pet {
id: ID!
createdAt: String!
name: String!
type: String!
user: User
}

input PetInput {
type: String
name: String
}

type Query {
pets(input: PetInput): [Pet]!
pet(input: PetInput): Pet
user: User!
}

input CreatePetInput {
name: String!
type: String!
}

type Mutation {
createPet(input: CreatePetInput!): Pet!
}
`;
// get pet by ID or name

module.exports = typeDefs
9 changes: 8 additions & 1 deletion api/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@ const typeDefs = require('./schema')
const resolvers = require('./resolvers')
const {models, db} = require('./db')

const server = new ApolloServer()
const server = new ApolloServer({
typeDefs,
resolvers,
context() {
const user = models.User.findOne()
return {models, db, user}
}
})

server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
Expand Down
37 changes: 37 additions & 0 deletions demo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const {ApolloServer} = require('apollo-server')
const gql = require('graphql-tag')


const typeDefs = gql`
type User {
email: String!
avatar: String!
friends: [User]!
}

type Query {
me: User!
}
`

const resolvers = {
Query: {
me() {
return {
email: '[email protected]',
avatar: 'YODA',
friends: ['darth']

}
}
}
}

// const server = new ApolloServer({
// typeDefs,
// resolvers
// })

// server.listen(4000).then(() => {
// console.log('listening on 4000')
// })
Loading