Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Draft] Transition to graphql-modules v1 #1148

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions examples/accounts-boost/src/microservice/app-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ import { mergeSchemas } from '@graphql-tools/merge';
import { makeExecutableSchema } from '@graphql-tools/schema';
import { introspectSchema, makeRemoteExecutableSchema } from '@graphql-tools/wrap';
import fetch from 'node-fetch';
import { AuthenticatedDirective, context } from '@accounts/graphql-api';

const accountsServerUri = 'http://localhost:4003/';

(async () => {
const accounts = (
await accountsBoost({
tokenSecret: 'terrible secret',
micro: true, // setting micro to true will instruct `@accounts/boost` to only verify access tokens without any additional session logic
})
).graphql();
const accounts = await accountsBoost({
tokenSecret: 'terrible secret',
micro: true, // setting micro to true will instruct `@accounts/boost` to only verify access tokens without any additional session logic
});

const accountsApp = accounts.graphql();

// // Note: the following steps are optional and only required if you want to stitch the remote accounts schema with your apps schema.

Expand Down Expand Up @@ -91,9 +92,19 @@ const accountsServerUri = 'http://localhost:4003/';
const apolloServer = await new ApolloServer({
schema: mergeSchemas({
schemas: [executableLocalSchema, executableRemoteSchema],
schemaDirectives: accounts.schemaDirectives as any,
schemaDirectives: {
// In order for the `@auth` directive to work
auth: AuthenticatedDirective,
} as any,
}),
context: ({ req }) => accounts.context({ req }),
context: ({ req, connection }) => {
return context(
{ req, connection },
{
accountsServer: accounts.accountsServer,
}
);
},
}).listen();

console.log(`GraphQL server running at ${apolloServer.url}`);
Expand Down
25 changes: 16 additions & 9 deletions examples/accounts-boost/src/monolithic/app-server.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import accountsBoost, { authenticated } from '@accounts/boost';
import { AuthenticatedDirective, context } from '@accounts/graphql-api';
import { mergeTypeDefs, mergeResolvers } from '@graphql-tools/merge';
import { ApolloServer } from 'apollo-server';

(async () => {
const accounts = (
await accountsBoost({
tokenSecret: 'terrible secret',
})
).graphql();
const accounts = await accountsBoost({
tokenSecret: 'terrible secret',
});
const accountsApp = accounts.graphql();

const typeDefs = `
type PrivateType @auth {
Expand Down Expand Up @@ -46,13 +46,20 @@ import { ApolloServer } from 'apollo-server';
};

const apolloServer = new ApolloServer({
typeDefs: mergeTypeDefs([typeDefs, accounts.typeDefs]),
resolvers: mergeResolvers([accounts.resolvers, resolvers]),
typeDefs: mergeTypeDefs([typeDefs, ...accountsApp.typeDefs]),
resolvers: mergeResolvers([accountsApp.resolvers, resolvers]),
schemaDirectives: {
// In order for the `@auth` directive to work
...accounts.schemaDirectives,
auth: AuthenticatedDirective,
},
context: ({ req, connection }) => {
return context(
{ req, connection },
{
accountsServer: accounts.accountsServer,
}
);
},
context: ({ req }) => accounts.context({ req }),
} as any)
.listen()
.then((res) => {
Expand Down
2 changes: 1 addition & 1 deletion examples/graphql-server-typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
"@accounts/password": "^0.32.0",
"@accounts/rest-express": "^0.32.0",
"@accounts/server": "^0.32.0",
"@graphql-modules/core": "0.7.17",
"@graphql-tools/merge": "6.2.5",
"apollo-server": "2.16.0",
"graphql": "14.6.0",
"graphql-modules": "1.4.2",
"mongoose": "5.9.25",
"tslib": "2.1.0"
},
Expand Down
52 changes: 31 additions & 21 deletions examples/graphql-server-typescript/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { DatabaseManager } from '@accounts/database-manager';
import { AccountsModule } from '@accounts/graphql-api';
import {
AuthenticatedDirective,
context,
createAccountsCoreModule,
createAccountsPasswordModule,
} from '@accounts/graphql-api';
import MongoDBInterface from '@accounts/mongo';
import { AccountsPassword } from '@accounts/password';
import { AccountsServer, ServerHooks } from '@accounts/server';
import { ApolloServer, makeExecutableSchema } 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';

const start = async () => {
// Create database connection
Expand Down Expand Up @@ -56,11 +61,6 @@ const start = async () => {
// If you throw an error here it will be returned to the client.
});

// Creates resolvers, type definitions, and schema directives used by accounts-js
const accountsGraphQL = AccountsModule.forRoot({
accountsServer,
});

const typeDefs = gql`
type PrivateType @auth {
field: String
Expand All @@ -77,18 +77,14 @@ const start = async () => {
lastName: String!
}

type Query {
extend type Query {
# Example of how to get the userId from the context and return the current logged in user or null
me: User
publicField: String
# You can only query this if you are logged in
privateField: String @auth
privateType: PrivateType
}

type Mutation {
_: String
}
`;

const resolvers = {
Expand All @@ -108,18 +104,32 @@ const start = async () => {
},
};

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

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

// Create the Apollo Server that takes a schema and configures internal stuff
const server = new ApolloServer({
schema,
context: accountsGraphQL.context,
/*schemaDirectives: {
auth: AuthenticatedDirective,
} as any,*/
context: ({ req, connection }) => {
return context({ req, connection }, { accountsServer });
},
});

server.listen(4000).then(({ url }) => {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
"prettier": "2.2.0",
"typescript": "3.9.5"
},
"resolutions": {},
"collective": {
"type": "opencollective",
"url": "https://opencollective.com/accounts-js",
Expand Down
2 changes: 1 addition & 1 deletion packages/boost/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
"@accounts/graphql-api": "^0.32.0",
"@accounts/server": "^0.32.0",
"@accounts/types": "^0.32.0",
"@graphql-modules/core": "0.7.17",
"apollo-server": "^2.9.3",
"graphql": "14.6.0",
"graphql-modules": "1.4.2",
"jsonwebtoken": "^8.5.1",
"lodash": "^4.17.20",
"tslib": "2.1.0"
Expand Down
30 changes: 22 additions & 8 deletions packages/boost/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import { DatabaseManager } from '@accounts/database-manager';
import { AccountsModule, authenticated } from '@accounts/graphql-api';
import {
createAccountsCoreModule,
createAccountsPasswordModule,
authenticated,
context,
} from '@accounts/graphql-api';
import { AccountsServer, AccountsServerOptions } from '@accounts/server';
import { AuthenticationService } from '@accounts/types';
import { ApolloServer } from 'apollo-server';
import { verify } from 'jsonwebtoken';
import { get, isString, merge } from 'lodash';
import { Application, createApplication } from 'graphql-modules';

export { AccountsModule };
export { createAccountsCoreModule, createAccountsPasswordModule };

export { AccountsServerOptions };

Expand Down Expand Up @@ -107,21 +113,29 @@ const defaultAccountsBoostListenOptions: AccountsBoostListenOptions = {
export class AccountsBoost {
public accountsServer: AccountsServer;
public apolloServer: ApolloServer;
public accountsGraphQL: typeof AccountsModule;
public accountsGraphQL: Application;
private options: AccountsBoostOptions;

constructor(options: AccountsBoostOptions, services: { [key: string]: AuthenticationService }) {
this.accountsServer = new AccountsServer(options, services);
this.options = options;
this.accountsGraphQL = AccountsModule.forRoot({
accountsServer: this.accountsServer,
this.accountsGraphQL = createApplication({
modules: [createAccountsCoreModule({ accountsServer: this.accountsServer })],
});

const { schema, context } = this.accountsGraphQL;
const { schema } = this.accountsGraphQL;

this.apolloServer = new ApolloServer({
schema,
context,
context: ({ req, connection }) => {
return context(
{ req, connection },
{
accountsServer: this.accountsServer,
}
);
},
//schemaDirectives
});
}

Expand All @@ -135,7 +149,7 @@ export class AccountsBoost {
return res;
}

public graphql(): typeof AccountsModule {
public graphql(): Application {
// Cache `this.accountsGraphQL` to avoid regenerating the schema if the user calls `accountsBoost.graphql()` multple times.
if (this.accountsGraphQL) {
return this.accountsGraphQL;
Expand Down
1 change: 1 addition & 0 deletions packages/graphql-api/codegen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ generates:
config:
noNamespaces: true
noSchemaStitching: true
contextType: ./modules/accounts/#AccountsContextGraphQLModules
plugins:
- add:
content: '/* eslint-disable */'
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql-api/introspection.json

Large diffs are not rendered by default.

10 changes: 4 additions & 6 deletions packages/graphql-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,11 @@
"@accounts/password": "^0.32.0",
"@accounts/server": "^0.32.0 || ^0.33.0",
"@accounts/types": "^0.32.0",
"@graphql-modules/core": "0.7.17",
"graphql": "^14.6.0 || ^15.0.0",
"graphql-tag": "^2.10.0"
"graphql-modules": "^1.0.0",
"graphql": "^14.6.0 || ^15.0.0"
},
"dependencies": {
"@graphql-tools/merge": "6.2.13",
"@graphql-tools/utils": "7.9.0",
"@graphql-tools/utils": "7.10.0",
"request-ip": "2.1.3",
"tslib": "2.1.0"
},
Expand All @@ -57,10 +55,10 @@
"@graphql-codegen/typescript-operations": "1.17.16",
"@graphql-codegen/typescript-resolvers": "1.19.1",
"@graphql-codegen/typescript-type-graphql": "1.18.4",
"@graphql-modules/core": "0.7.17",
"@types/jest": "26.0.23",
"@types/request-ip": "0.0.35",
"graphql": "14.6.0",
"graphql-modules": "1.4.2",
"jest": "26.6.3",
"ts-jest": "26.5.6",
"ts-node": "9.1.1"
Expand Down
Loading