Skip to content
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
27 changes: 24 additions & 3 deletions api/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,40 @@

module.exports = {
Query: {

pets(_,{input},ctx){
return ctx.models.Pet.findMany(input)
},
pet(_,{input},ctx){
console.log('Query =>pet')
return ctx.models.Pet.findOne(input)
}
},
Mutation: {

newPet(_,{input},ctx){
const pet = ctx.models.Pet.create(input)
return pet
},
updatePet(_,{input},ctx){
const pet = ctx.models.Pet.update(input.id,input.updates)
return pet

}
},
Pet: {
owner(_,__,ctx){
console.log('PET => owner')
return ctx.models.User.findOne()
},
img(pet) {
return pet.type === 'DOG'
? 'https://placedog.net/300/300'
: 'http://placekitten.com/300/300'
}
},
User: {

pets(_,__,ctx){
console.log('User =>pets')
return ctx.models.Pet.findMany()
}
}
}
32 changes: 31 additions & 1 deletion api/src/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,37 @@ 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!
img:String
owners:User!
}

input PetInput {
name: String
type: String
}
input NewPetInput{
name: String!
type: String!
}

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

type Mutation{
newPet(input:NewPetInput!):Pet!
}

`;

Expand Down
10 changes: 9 additions & 1 deletion api/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@ 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
21 changes: 20 additions & 1 deletion db.json
Original file line number Diff line number Diff line change
@@ -1 +1,20 @@
{}
{
"user": {
"id":"bckdfuwyfwfwhgkbvlvifofh",
"username": "frontendmaster"
},
"pet":[
{
"id": "1",
"createdAt": "453548358",
"name": " moose",
"type": "DOG"
},
{
"id": "2",
"createdAt": "5725697358",
"name": " dire",
"type": "CATS"
}
]
}
Loading