diff --git a/packages/graphback-codegen-client/tests/GraphQLClientCreatorTest.ts b/packages/graphback-codegen-client/tests/GraphQLClientCreatorTest.ts index fd5f8be484..ee11578ad8 100644 --- a/packages/graphback-codegen-client/tests/GraphQLClientCreatorTest.ts +++ b/packages/graphback-codegen-client/tests/GraphQLClientCreatorTest.ts @@ -38,8 +38,7 @@ test('Test plugin engine graphql', async () => { "delete": true, } - const metadata = new GraphbackCoreMetadata({ crudMethods }, buildSchema(schemaText)); - metadata.setModelDefinitions(); + const { metadata } = setup(schemaText, { crudMethods, plugins: [new SchemaCRUDPlugin()] }) const plugin = new ClientCRUDPlugin({ outputFile: './tmp/generated.graphql', fragmentOnly: false }); expect(plugin.getDocuments(metadata)).toMatchSnapshot(); }); @@ -53,7 +52,8 @@ test('Test plugin engine to throw error when no file extension specified', async "delete": true, } - const metadata = new GraphbackCoreMetadata({ crudMethods }, buildSchema(schemaText)) + const { metadata } = setup(schemaText, { crudMethods, plugins: [new SchemaCRUDPlugin()] }) + const plugin = new ClientCRUDPlugin({ outputFile: './tmp/generated', fragmentOnly: false }); try { @@ -72,7 +72,7 @@ test('Test plugin engine to throw error when invalid file extension specified', "delete": true, } - const metadata = new GraphbackCoreMetadata({ crudMethods }, buildSchema(schemaText)) + const { metadata } = setup(schemaText, { crudMethods, plugins: [new SchemaCRUDPlugin()] }) const plugin = new ClientCRUDPlugin({ outputFile: './tmp/generated.crazyday', fragmentOnly: false }); try { diff --git a/packages/graphback-core/src/plugin/GraphbackCoreMetadata.ts b/packages/graphback-core/src/plugin/GraphbackCoreMetadata.ts index 0faea4a8d9..205c08af95 100644 --- a/packages/graphback-core/src/plugin/GraphbackCoreMetadata.ts +++ b/packages/graphback-core/src/plugin/GraphbackCoreMetadata.ts @@ -24,8 +24,7 @@ const defaultCRUDGeneratorConfig = { * Contains Graphback Core Models */ export class GraphbackCoreMetadata { - - private supportedCrudMethods: GraphbackCRUDGeneratorConfig + private supportedCrudMethods: GraphbackCRUDGeneratorConfig; private schema: GraphQLSchema; private resolvers: IResolvers; private models: ModelDefinition[]; @@ -58,14 +57,17 @@ export class GraphbackCoreMetadata { return this.resolvers; } + /** + * Get Graphback Models - GraphQL Types with additional CRUD configuration + */ public getModelDefinitions() { return this.models; } - + /** - * Get Graphback Models - GraphQL Types with additional CRUD configuration + * Creates and sets the model definitions from the current GraphQL schema */ - public setModelDefinitions() { + public buildAndSetModelDefinitions() { //Contains map of the models with their underlying CRUD configuration this.models = []; //Get actual user types diff --git a/packages/graphback-core/src/plugin/GraphbackPluginEngine.ts b/packages/graphback-core/src/plugin/GraphbackPluginEngine.ts index 0108208b1a..1e55307d1e 100644 --- a/packages/graphback-core/src/plugin/GraphbackPluginEngine.ts +++ b/packages/graphback-core/src/plugin/GraphbackPluginEngine.ts @@ -71,11 +71,10 @@ export class GraphbackPluginEngine { private createSchema() { //We need to apply all required changes to the schema we need //This is to ensure that every plugin can add changes to the schema - this.metadata.setModelDefinitions(); for (const plugin of this.plugins) { + this.metadata.buildAndSetModelDefinitions(); const newSchema = plugin.transformSchema(this.metadata); this.metadata.setSchema(newSchema); - this.metadata.setModelDefinitions(); } } diff --git a/packages/graphback-core/tests/GraphbackPluginEngineTest.ts b/packages/graphback-core/tests/GraphbackPluginEngineTest.ts deleted file mode 100644 index 7748b18924..0000000000 --- a/packages/graphback-core/tests/GraphbackPluginEngineTest.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { buildSchema } from "graphql"; -import { GraphbackPluginEngine } from "../src"; - -describe('GraphbackPluginEngineTest', () => { - const defaultModel = buildSchema(` - """ @model """ -type Note { - """ @id """ - _id: ID! - title: String! - description: String - """ - @oneToMany(field: 'note') - """ - comments: [Comment]! -} - -""" @model """ -type Comment { - """ @id """ - _id: ID! - text: String - description: String -}`); - - test('should create initial models', () => { - const pluginEngine = new GraphbackPluginEngine({ schema: defaultModel }); - const metadata = pluginEngine.createResources(); - const models = metadata.getModelDefinitions(); - - expect(models).toHaveLength(2); - }); -}) \ No newline at end of file diff --git a/packages/graphback-core/tests/MetadataTest.ts b/packages/graphback-core/tests/MetadataTest.ts index 6475052181..4c23ba3dde 100644 --- a/packages/graphback-core/tests/MetadataTest.ts +++ b/packages/graphback-core/tests/MetadataTest.ts @@ -1,8 +1,10 @@ import { buildSchema } from 'graphql'; -import { GraphbackCoreMetadata, GraphbackCRUDGeneratorConfig, getModelByName, GraphbackPluginEngine } from '../src' +import { SchemaCRUDPlugin } from '@graphback/codegen-schema'; +// eslint-disable-next-line import/no-extraneous-dependencies +import { GraphbackCRUDGeneratorConfig, getModelByName, GraphbackPluginEngine } from '@graphback/core' const setup = (model: string, config?: { crudMethods?: GraphbackCRUDGeneratorConfig }) => { - const pluginEngine = new GraphbackPluginEngine({ schema: buildSchema(model) }) + const pluginEngine = new GraphbackPluginEngine({ schema: buildSchema(model), plugins: [new SchemaCRUDPlugin()] }) const metadata = pluginEngine.createResources(); return { metadata } diff --git a/packages/graphback-core/tests/getSelectedFieldsFromResolverInfoTest.ts b/packages/graphback-core/tests/getSelectedFieldsFromResolverInfoTest.ts index a7b4f2039d..a63205e149 100644 --- a/packages/graphback-core/tests/getSelectedFieldsFromResolverInfoTest.ts +++ b/packages/graphback-core/tests/getSelectedFieldsFromResolverInfoTest.ts @@ -1,15 +1,14 @@ -import { GraphQLSchema, buildSchema, assertObjectType, GraphQLResolveInfo } from "graphql" -import { GraphbackCoreMetadata } from "../src/plugin/GraphbackCoreMetadata"; -import { ModelDefinition } from '../src/plugin/ModelDefinition' -import { getModelFieldsFromResolverFields } from '../src/plugin/getSelectedFieldsFromResolverInfo'; -import { GraphbackPluginEngine } from "../src"; +/* eslint-disable import/no-extraneous-dependencies */ +import { GraphQLSchema, buildSchema } from "graphql" +import { SchemaCRUDPlugin } from '@graphback/codegen-schema'; +import { GraphbackPluginEngine, GraphbackCoreMetadata, ModelDefinition, getModelFieldsFromResolverFields } from "@graphback/core"; describe('getSelectedFieldsFromResolverInfo', () => { const setup = (schemaAST: string): { schema: GraphQLSchema, metadata: GraphbackCoreMetadata } => { const schema = buildSchema(schemaAST); - const pluginEngine = new GraphbackPluginEngine({ schema }); + const pluginEngine = new GraphbackPluginEngine({ schema, plugins: [new SchemaCRUDPlugin()] }); const metadata = pluginEngine.createResources() diff --git a/packages/graphback-runtime-knex/tests/data/KnexDbDataProviderTest.ts b/packages/graphback-runtime-knex/tests/data/KnexDbDataProviderTest.ts index 215b97e8e9..de9737e02e 100644 --- a/packages/graphback-runtime-knex/tests/data/KnexDbDataProviderTest.ts +++ b/packages/graphback-runtime-knex/tests/data/KnexDbDataProviderTest.ts @@ -5,7 +5,7 @@ import { unlinkSync, existsSync } from 'fs'; import { buildSchema } from 'graphql'; import * as Knex from 'knex'; import { GraphbackDataProvider, GraphbackPluginEngine, QueryFilter } from '@graphback/core'; -import { SchemaCRUDPlugin } from '../../../graphback-codegen-schema/src'; +import { SchemaCRUDPlugin } from '../../../graphback-codegen-schema'; import { SQLiteKnexDBDataProvider } from '../../src/SQLiteKnexDBDataProvider'; import { migrateDB, removeNonSafeOperationsFilter } from '../../../graphql-migrations/src'; diff --git a/packages/graphback-runtime-knex/tests/data/db.sqlite b/packages/graphback-runtime-knex/tests/data/db.sqlite new file mode 100644 index 0000000000..f65a39b8cc Binary files /dev/null and b/packages/graphback-runtime-knex/tests/data/db.sqlite differ diff --git a/packages/graphback-runtime-mongodb/tests/MongoDataProviderTest.ts b/packages/graphback-runtime-mongodb/tests/MongoDataProviderTest.ts index e9ba0d5aea..4432181b80 100644 --- a/packages/graphback-runtime-mongodb/tests/MongoDataProviderTest.ts +++ b/packages/graphback-runtime-mongodb/tests/MongoDataProviderTest.ts @@ -2,7 +2,7 @@ import { ObjectID } from 'mongodb'; import { advanceTo, advanceBy } from "jest-date-mock"; import { QueryFilter, GraphbackPluginEngine } from '@graphback/core'; -import { SchemaCRUDPlugin } from '../../graphback-codegen-schema' +import { SchemaCRUDPlugin } from '@graphback/codegen-schema' import { MongoDBDataProvider } from '../src/MongoDBDataProvider'; import { Context, createTestingContext } from "./__util__"; diff --git a/packages/graphback-runtime-mongodb/tests/__util__.ts b/packages/graphback-runtime-mongodb/tests/__util__.ts index a85d62c0db..394b4de636 100644 --- a/packages/graphback-runtime-mongodb/tests/__util__.ts +++ b/packages/graphback-runtime-mongodb/tests/__util__.ts @@ -2,7 +2,7 @@ import { MongoMemoryServer } from 'mongodb-memory-server-global'; import { MongoClient, Db, Collection } from 'mongodb'; import { buildSchema } from 'graphql'; import { GraphbackPluginEngine } from '@graphback/core'; -import { SchemaCRUDPlugin } from "../../graphback-codegen-schema"; +import { SchemaCRUDPlugin } from '@graphback/codegen-schema'; import { MongoDBDataProvider } from '../src/MongoDBDataProvider'; export interface Context { diff --git a/templates/ts-apollo-postgres-backend/src/index.ts b/templates/ts-apollo-postgres-backend/src/index.ts index 4afdfa6e6c..722dafc579 100644 --- a/templates/ts-apollo-postgres-backend/src/index.ts +++ b/templates/ts-apollo-postgres-backend/src/index.ts @@ -8,6 +8,7 @@ import { buildGraphbackAPI } from 'graphback'; import { loadDBConfig, connectDB } from './db'; import { migrateDB, removeNonSafeOperationsFilter } from 'graphql-migrations'; import { createKnexDbProvider } from '@graphback/runtime-knex'; +import { SchemaCRUDPlugin } from '@graphback/codegen-schema' import { noteResolvers } from './resolvers/noteResolvers'; import { loadConfigSync } from 'graphql-config'; @@ -32,7 +33,10 @@ const db = connectDB(); const dbConfig = loadDBConfig(); const { typeDefs, resolvers, contextCreator } = buildGraphbackAPI(modelDefs, { - dataProviderCreator: createKnexDbProvider(db) + dataProviderCreator: createKnexDbProvider(db), + plugins: [ + new SchemaCRUDPlugin() + ] }); migrateDB(dbConfig, typeDefs, {