Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
06-GraphQL Schema Stitching with Apollo Server
- Loading branch information
Showing
8 changed files
with
162 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
let users = { | ||
1: { | ||
id: '1', | ||
username: 'Robin Wieruch', | ||
messageIds: [1], | ||
}, | ||
2: { | ||
id: '2', | ||
username: 'Dave Davids', | ||
messageIds: [2], | ||
}, | ||
}; | ||
|
||
let messages = { | ||
1: { | ||
id: '1', | ||
text: 'Hello World', | ||
userId: '1', | ||
}, | ||
2: { | ||
id: '2', | ||
text: 'By World', | ||
userId: '2', | ||
}, | ||
}; | ||
|
||
export default { | ||
users, | ||
messages, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import userResolvers from '../resolvers/user'; | ||
import messageResolvers from '../resolvers/message'; | ||
|
||
export default [userResolvers, messageResolvers]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import uuidv4 from 'uuid/v4'; | ||
|
||
export default { | ||
Query: { | ||
messages: (parent, args, { models }) => { | ||
return Object.values(models.messages); | ||
}, | ||
message: (parent, { id }, { models }) => { | ||
return models.messages[id]; | ||
}, | ||
}, | ||
|
||
Mutation: { | ||
createMessage: (parent, { text }, { me, models }) => { | ||
const id = uuidv4(); | ||
const message = { | ||
id, | ||
text, | ||
userId: me.id, | ||
}; | ||
|
||
models.messages[id] = message; | ||
models.users[me.id].messageIds.push(id); | ||
|
||
return message; | ||
}, | ||
|
||
deleteMessage: (parent, { id }, { models }) => { | ||
const { [id]: message, ...otherMessages } = models.messages; | ||
|
||
if (!message) { | ||
return false; | ||
} | ||
|
||
models.messages = otherMessages; | ||
|
||
return true; | ||
}, | ||
}, | ||
|
||
Message: { | ||
user: (message, args, { models }) => { | ||
return models.users[message.userId]; | ||
}, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
export default { | ||
Query: { | ||
users: (parent, args, { models }) => { | ||
return Object.values(models.users); | ||
}, | ||
user: (parent, { id }, { models }) => { | ||
return models.users[id]; | ||
}, | ||
me: (parent, args, { me }) => { | ||
return me; | ||
}, | ||
}, | ||
|
||
User: { | ||
messages: (user, args, { models }) => { | ||
return Object.values(models.messages).filter( | ||
message => message.userId === user.id, | ||
); | ||
}, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { gql } from 'apollo-server-express'; | ||
|
||
import userSchema from './user'; | ||
import messageSchema from './message'; | ||
|
||
const linkSchema = gql` | ||
type Query { | ||
_: Boolean | ||
} | ||
type Mutation { | ||
_: Boolean | ||
} | ||
type Subscription { | ||
_: Boolean | ||
} | ||
`; | ||
|
||
export default [linkSchema, userSchema, messageSchema]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { gql } from 'apollo-server-express'; | ||
|
||
export default gql` | ||
extend type Query { | ||
messages: [Message!]! | ||
message(id: ID!): Message! | ||
} | ||
extend type Mutation { | ||
createMessage(text: String!): Message! | ||
deleteMessage(id: ID!): Boolean! | ||
} | ||
type Message { | ||
id: ID! | ||
text: String! | ||
user: User! | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { gql } from 'apollo-server-express'; | ||
|
||
export default gql` | ||
extend type Query { | ||
users: [User!] | ||
user(id: ID!): User | ||
me: User | ||
} | ||
type User { | ||
id: ID! | ||
username: String! | ||
messages: [Message!] | ||
} | ||
`; |