Skip to content
Permalink
Browse files

06-GraphQL Schema Stitching with Apollo Server

  • Loading branch information...
rwieruch committed Oct 25, 2018
1 parent a10c54e commit 953ef4b2ac8edc7c6338fb73ecdc1446e9cbdc4d
@@ -2,134 +2,22 @@ import 'dotenv/config';
import cors from 'cors';
import uuidv4 from 'uuid/v4';
import express from 'express';
import { ApolloServer, gql } from 'apollo-server-express';
import { ApolloServer } from 'apollo-server-express';

import schema from './schema';
import resolvers from './resolvers';
import models from './models';

const app = express();

app.use(cors());

const schema = gql`
type Query {
users: [User!]
me: User
user(id: ID!): User
messages: [Message!]!
message(id: ID!): Message!
}
type Mutation {
createMessage(text: String!): Message!
deleteMessage(id: ID!): Boolean!
}
type User {
id: ID!
username: String!
messages: [Message!]
}
type Message {
id: ID!
text: String!
user: User!
}
`;

let users = {
1: {
id: '1',
username: 'Robin Wieruch',
messageIds: [1],
},
2: {
id: '2',
username: 'Dave Davids',
messageIds: [2],
},
};

const me = users[1];

let messages = {
1: {
id: '1',
text: 'Hello World',
userId: '1',
},
2: {
id: '2',
text: 'By World',
userId: '2',
},
};

const resolvers = {
Query: {
users: () => {
return Object.values(users);
},
user: (parent, { id }) => {
return users[id];
},
me: (parent, args, { me }) => {
return me;
},
messages: () => {
return Object.values(messages);
},
message: (parent, { id }) => {
return messages[id];
},
},

Mutation: {
createMessage: (parent, { text }, { me }) => {
const id = uuidv4();
const message = {
id,
text,
userId: me.id,
};

messages[id] = message;
users[me.id].messageIds.push(id);

return message;
},
deleteMessage: (parent, { id }) => {
const { [id]: message, ...otherMessages } = messages;

if (!message) {
return false;
}

messages = otherMessages;

return true;
},
},

User: {
messages: user => {
return Object.values(messages).filter(
message => message.userId === user.id,
);
},
},

Message: {
user: message => {
return users[message.userId];
},
},
};

const server = new ApolloServer({
typeDefs: schema,
resolvers,
context: {
me: users[1],
models,
me: models.users[1],
},
});

@@ -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,
};
@@ -0,0 +1,4 @@
import userResolvers from '../resolvers/user';
import messageResolvers from '../resolvers/message';

export default [userResolvers, messageResolvers];
@@ -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];
},
},
};
@@ -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,
);
},
},
};
@@ -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];
@@ -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!
}
`;
@@ -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!]
}
`;

0 comments on commit 953ef4b

Please sign in to comment.
You can’t perform that action at this time.