Skip to content

Commit a3969cc

Browse files
committed
ready
1 parent f37ff03 commit a3969cc

File tree

6 files changed

+18
-133
lines changed

6 files changed

+18
-133
lines changed

api/src/resolvers.js

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,23 @@
1+
/**
2+
* Here are your Resolvers for your Schema. They must match
3+
* the type definitions in your scheama
4+
*/
5+
16
module.exports = {
27
Query: {
3-
pets(_, {input}, {models}) {
4-
return models.Pet.findMany(input || {})
5-
},
6-
pet(_, {id}, {models}) {
7-
return models.Pet.findOne({id})
8-
},
9-
user(_, __, {models}) {
10-
return models.User.findOne()
11-
}
8+
129
},
1310
Mutation: {
14-
addPet(_, {input}, {models, user}) {
15-
const pet = models.Pet.create({...input, user: user.id})
16-
return pet
17-
}
11+
1812
},
1913
Pet: {
20-
owner(pet, _, {models}) {
21-
return models.User.findOne({id: pet.user})
22-
},
2314
img(pet) {
2415
return pet.type === 'DOG'
2516
? 'https://placedog.net/300/300'
2617
: 'http://placekitten.com/300/300'
2718
}
2819
},
2920
User: {
30-
pets(user, _, {models}) {
31-
return models.Pet.findMany({user: user.id})
32-
}
21+
3322
}
3423
}

api/src/schema.js

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,14 @@
11
const { gql } = require('apollo-server')
22

3+
/**
4+
* Type Definitions for our Schema using the SDL.
5+
*/
36
const typeDefs = gql`
4-
enum PetType {
5-
CAT
6-
DOG
7+
type User {
8+
id: ID!
9+
username: String!
710
}
811
9-
type User {
10-
id: ID!
11-
username: String!
12-
pets: [Pet]!
13-
}
14-
15-
type Pet {
16-
id: ID!
17-
type: PetType!
18-
name: String!
19-
owner: User!
20-
img: String!
21-
createdAt: Int!
22-
}
23-
24-
input NewPetInput {
25-
name: String!
26-
type: PetType!
27-
}
28-
29-
input PetsInput {
30-
type: PetType
31-
}
32-
33-
type Query {
34-
user: User!
35-
pets(input: PetsInput): [Pet]!
36-
pet(id: ID!): Pet!
37-
}
38-
39-
type Mutation {
40-
addPet(input: NewPetInput!): Pet!
41-
}
4212
`;
4313

4414
module.exports = typeDefs

api/src/server.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,7 @@ const typeDefs = require('./schema')
33
const resolvers = require('./resolvers')
44
const {models, db} = require('./db')
55

6-
const server = new ApolloServer({
7-
typeDefs,
8-
resolvers,
9-
context() {
10-
const user = db.get('user').value()
11-
return {models, db, user}
12-
}
13-
})
6+
const server = new ApolloServer()
147

158
server.listen().then(({ url }) => {
169
console.log(`🚀 Server ready at ${url}`);

client/src/client.js

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,7 @@ import { InMemoryCache } from 'apollo-cache-inmemory'
33
import { HttpLink } from 'apollo-link-http'
44
import gql from 'graphql-tag'
55

6-
const typeDefs = gql`
7-
extend type Pet {
8-
vacinated: Boolean!
9-
}
10-
`;
116

12-
const resolvers = {
13-
Pet: {
14-
vacinated: () => true
15-
}
16-
};
17-
18-
const cache = new InMemoryCache()
19-
const link = new HttpLink({
20-
uri: 'http://localhost:4000/'
21-
})
22-
23-
const client = new ApolloClient({
24-
cache,
25-
link,
26-
typeDefs,
27-
resolvers
28-
})
7+
const client = new ApolloClient()
298

309
export default client

client/src/index.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ import './index.css'
88

99
const Root = () => (
1010
<BrowserRouter>
11-
<ApolloProvider client={client}>
12-
<App />
13-
</ApolloProvider>
11+
<App />
1412
</BrowserRouter>
1513
)
1614

client/src/pages/Pets.js

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,55 +5,11 @@ import NewPet from '../components/NewPet'
55
import { useQuery, useMutation } from '@apollo/react-hooks'
66
import Loader from '../components/Loader'
77

8-
const PET_DETAILS = gql`
9-
fragment PetDetails on Pet {
10-
id
11-
type
12-
name
13-
img
14-
vacinated @client
15-
}
16-
`
17-
18-
const GET_PETS = gql`
19-
query petsList($input: PetsInput) {
20-
pets(input: $input) {
21-
...PetDetails
22-
}
23-
}
24-
${PET_DETAILS}
25-
`
26-
27-
const CREATE_PET = gql`
28-
mutation CreatePet($input: NewPetInput!) {
29-
addPet(input: $input) {
30-
...PetDetails
31-
}
32-
}
33-
${PET_DETAILS}
34-
`;
35-
368
export default function Pets () {
379
const [modal, setModal] = useState(false)
38-
const pets = useQuery(GET_PETS)
39-
40-
const [createPet, newPet] = useMutation(CREATE_PET, {
41-
update(cache, { data: { addPet } }) {
42-
const { pets } = cache.readQuery({ query: GET_PETS })
43-
44-
cache.writeQuery({
45-
query: GET_PETS,
46-
data: { pets: pets.concat([addPet]) }
47-
})
48-
}
49-
})
50-
51-
if (pets.loading || newPet.loading) return <Loader />
52-
if (pets.error || newPet.error) return <p>ERROR</p>
53-
10+
5411
const onSubmit = input => {
5512
setModal(false)
56-
createPet({variables: {input}})
5713
}
5814

5915
const petsList = pets.data.pets.map(pet => (

0 commit comments

Comments
 (0)