Skip to content

Commit

Permalink
Update graphql-server-typescript example to use graphql-modules
Browse files Browse the repository at this point in the history
  • Loading branch information
darkbasic committed Jun 10, 2021
1 parent f40419e commit 44ae64a
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 62 deletions.
48 changes: 11 additions & 37 deletions examples/graphql-server-typescript/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@ import {
import MongoDBInterface from '@accounts/mongo';
import { AccountsPassword } from '@accounts/password';
import { AccountsServer, ServerHooks } from '@accounts/server';
import { ApolloServer, makeExecutableSchema, SchemaDirectiveVisitor } from 'apollo-server';
import { ApolloServer, SchemaDirectiveVisitor } from 'apollo-server';
import gql from 'graphql-tag';
import { mergeResolvers, mergeTypeDefs } from '@graphql-tools/merge';
import mongoose from 'mongoose';
import { createApplication, createModule } from 'graphql-modules';
import { parse } from 'graphql';
import { printSchemaWithDirectives, getResolversFromSchema } from '@graphql-tools/utils';

const start = async () => {
// Create database connection
Expand Down Expand Up @@ -105,54 +102,31 @@ const start = async () => {
field: () => 'private',
}),
},
User: {
firstName(user, args, ctx) {
console.log('UserResolvers');
console.log(user);
return user.firstName;
},
},
};

/*
const myModule = createModule({
id: 'mine',
typeDefs,
resolvers,
});
*/

// Creates resolvers, type definitions, and schema directives used by accounts-js
const accountsSchema = createApplication({
const schema = createApplication({
modules: [
createAccountsCoreModule({ accountsServer }),
createAccountsPasswordModule({ accountsPassword }),
//myModule,
createModule({
id: 'myApp',
typeDefs,
resolvers,
}),
],
}).createSchemaForApollo();

const accountsTypeDefs = parse(printSchemaWithDirectives(accountsSchema));
const accountsResolvers = getResolversFromSchema(accountsSchema);

const schema = makeExecutableSchema({
typeDefs: mergeTypeDefs([typeDefs, accountsTypeDefs]),
resolvers: mergeResolvers([accountsResolvers as any, resolvers]),
schemaDirectives: {
// In order for the `@auth` directive to work
auth: AuthenticatedDirective,
} as any,
});

/*
SchemaDirectiveVisitor.visitSchemaDirectives(accountsSchema, {
SchemaDirectiveVisitor.visitSchemaDirectives(schema, {
auth: AuthenticatedDirective,
} as any);
*/

// Create the Apollo Server that takes a schema and configures internal stuff
const server = new ApolloServer({
schema,
//schema: accountsSchema,
/*schemaDirectives: {
auth: AuthenticatedDirective,
} as any,*/
context: ({ req, connection }) => {
return context({ req, connection }, { accountsServer });
},
Expand Down
5 changes: 0 additions & 5 deletions packages/graphql-api/src/modules/accounts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import getMutationTypeDefs from './schema/mutation';
import getSchemaDef from './schema/schema-def';
import { Query } from './resolvers/query';
import { Mutation } from './resolvers/mutation';
import { User as UserResolvers, LoginResult as LoginResultResolvers } from './resolvers/user';
import makeSchema from './schema/schema';
//import { AuthenticatedDirective } from '../../utils/authenticated-directive';
//import { context } from '../../utils';
Expand Down Expand Up @@ -60,10 +59,6 @@ export const createAccountsCoreModule = (config: AccountsCoreModuleConfig) =>
resolvers: {
[config.rootQueryName || 'Query']: Query,
[config.rootMutationName || 'Mutation']: Mutation,
//FIXME: remove me
User: UserResolvers,
//FIXME: remove me
LoginResult: LoginResultResolvers,
},
providers: [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@ import { MutationResolvers } from '../../../models';

export const Mutation: MutationResolvers = {
authenticate: async (_, args, ctx) => {
//FIXME: remove console.logs
console.log('authenticateResolvers');
const { serviceName, params } = args;
const { injector, infos } = ctx;

const authenticated = await injector
.get(AccountsServer)
.loginWithService(serviceName, params, infos);
console.log(authenticated.user);
return authenticated;
},
verifyAuthentication: async (_, args, ctx) => {
Expand Down
17 changes: 0 additions & 17 deletions packages/graphql-api/src/modules/accounts/resolvers/user.ts

This file was deleted.

0 comments on commit 44ae64a

Please sign in to comment.