Last active
April 1, 2020 07:32
-
-
Save dotansimha/2516b2d8bbf3e58d230b8414ed51dc36 to your computer and use it in GitHub Desktop.
GraphQL Modules Example
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
import { GraphQLModule } from '@graphql-modules/core'; | |
import { UserModule } from './user-module'; | |
import { ChatModule } from './chat-module'; | |
export const appModule = new GraphQLModule({ | |
imports: [ | |
UserModule, | |
ChatModule, | |
], | |
}); |
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
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), | |
}, | |
}, | |
}); |
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
import { appModule } from './modules/app'; | |
import { ApolloServer } from 'apollo-server'; | |
const { schema, context } = appModule; | |
const server = new ApolloServer({ | |
schema, | |
context, | |
introspection: true, | |
}); | |
server.listen(); |
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
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