File tree Expand file tree Collapse file tree 6 files changed +18
-133
lines changed Expand file tree Collapse file tree 6 files changed +18
-133
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * Here are your Resolvers for your Schema. They must match
3
+ * the type definitions in your scheama
4
+ */
5
+
1
6
module . exports = {
2
7
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
+
12
9
} ,
13
10
Mutation : {
14
- addPet ( _ , { input} , { models, user} ) {
15
- const pet = models . Pet . create ( { ...input , user : user . id } )
16
- return pet
17
- }
11
+
18
12
} ,
19
13
Pet : {
20
- owner ( pet , _ , { models} ) {
21
- return models . User . findOne ( { id : pet . user } )
22
- } ,
23
14
img ( pet ) {
24
15
return pet . type === 'DOG'
25
16
? 'https://placedog.net/300/300'
26
17
: 'http://placekitten.com/300/300'
27
18
}
28
19
} ,
29
20
User : {
30
- pets ( user , _ , { models} ) {
31
- return models . Pet . findMany ( { user : user . id } )
32
- }
21
+
33
22
}
34
23
}
Original file line number Diff line number Diff line change 1
1
const { gql } = require ( 'apollo-server' )
2
2
3
+ /**
4
+ * Type Definitions for our Schema using the SDL.
5
+ */
3
6
const typeDefs = gql `
4
- enum PetType {
5
- CAT
6
- DOG
7
+ type User {
8
+ id: ID!
9
+ username: String!
7
10
}
8
11
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
- }
42
12
` ;
43
13
44
14
module . exports = typeDefs
Original file line number Diff line number Diff line change @@ -3,14 +3,7 @@ const typeDefs = require('./schema')
3
3
const resolvers = require ( './resolvers' )
4
4
const { models, db} = require ( './db' )
5
5
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 ( )
14
7
15
8
server . listen ( ) . then ( ( { url } ) => {
16
9
console . log ( `🚀 Server ready at ${ url } ` ) ;
Original file line number Diff line number Diff line change @@ -3,28 +3,7 @@ import { InMemoryCache } from 'apollo-cache-inmemory'
3
3
import { HttpLink } from 'apollo-link-http'
4
4
import gql from 'graphql-tag'
5
5
6
- const typeDefs = gql `
7
- extend type Pet {
8
- vacinated: Boolean!
9
- }
10
- ` ;
11
6
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 ( )
29
8
30
9
export default client
Original file line number Diff line number Diff line change @@ -8,9 +8,7 @@ import './index.css'
8
8
9
9
const Root = ( ) => (
10
10
< BrowserRouter >
11
- < ApolloProvider client = { client } >
12
- < App />
13
- </ ApolloProvider >
11
+ < App />
14
12
</ BrowserRouter >
15
13
)
16
14
Original file line number Diff line number Diff line change @@ -5,55 +5,11 @@ import NewPet from '../components/NewPet'
5
5
import { useQuery , useMutation } from '@apollo/react-hooks'
6
6
import Loader from '../components/Loader'
7
7
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
-
36
8
export default function Pets ( ) {
37
9
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
+
54
11
const onSubmit = input => {
55
12
setModal ( false )
56
- createPet ( { variables : { input} } )
57
13
}
58
14
59
15
const petsList = pets . data . pets . map ( pet => (
You can’t perform that action at this time.
0 commit comments