Skip to content

Instantly share code, notes, and snippets.

@dotansimha
Last active April 1, 2020 07:32
Show Gist options
  • Save dotansimha/2516b2d8bbf3e58d230b8414ed51dc36 to your computer and use it in GitHub Desktop.
Save dotansimha/2516b2d8bbf3e58d230b8414ed51dc36 to your computer and use it in GitHub Desktop.
GraphQL Modules Example
import { GraphQLModule } from '@graphql-modules/core';
import { UserModule } from './user-module';
import { ChatModule } from './chat-module';
export const appModule = new GraphQLModule({
imports: [
UserModule,
ChatModule,
],
});
import { GraphQLModule } from '@graphql-modules/core';
import gql from 'graphql-tag';
export const ChatModule = new GraphQLModule({
typeDefs: gql`
# Query declared again, adding only the part of the schema that relevant
type Query {
myChats: [Chat]
}
# User declared again- extends any other `User` type that loaded into the appModule
type User {
chats: [Chat]
}
type Chat {
id: ID!
users: [User]
messages: [ChatMessage]
}
type ChatMessage {
id: ID!
content: String!
user: User!
}
`,
resolvers: {
Query: {
myChats: (root, args, { getChats, currentUser }) => getChats(currentUser),
},
User: {
// This module implements only the part of `User` it adds
chats: (user, args, { getChats }) => getChats(user),
},
},
});
import { appModule } from './modules/app';
import { ApolloServer } from 'apollo-server';
const { schema, context } = appModule;
const server = new ApolloServer({
schema,
context,
introspection: true,
});
server.listen();
import { GraphQLModule } from '@graphql-modules/core';
import gql from 'graphql-tag';
export const UserModule = new GraphQLModule({
typeDefs: gql`
type Query {
me: User
}
# This is a basic User, with just the basics of a user object
type User {
id: ID!
username: String!
email: String!
}
`,
resolvers: {
Query: {
me: (root, args, { currentUser ) => currentUser,
},
User: {
id: user => user._id,
username: user => user.username,
email: user => user.email.address,
},
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment