Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
fix: apply feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Enda Phelan committed Sep 17, 2020
1 parent e85f90b commit 959e2bc
Show file tree
Hide file tree
Showing 11 changed files with 29 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
12 changes: 7 additions & 5 deletions packages/graphback-core/src/plugin/GraphbackCoreMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions packages/graphback-core/src/plugin/GraphbackPluginEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down
33 changes: 0 additions & 33 deletions packages/graphback-core/tests/GraphbackPluginEngineTest.ts

This file was deleted.

6 changes: 4 additions & 2 deletions packages/graphback-core/tests/MetadataTest.ts
Original file line number Diff line number Diff line change
@@ -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 }
Expand Down
Original file line number Diff line number Diff line change
@@ -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()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -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__";

Expand Down
2 changes: 1 addition & 1 deletion packages/graphback-runtime-mongodb/tests/__util__.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 5 additions & 1 deletion templates/ts-apollo-postgres-backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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, {
Expand Down

0 comments on commit 959e2bc

Please sign in to comment.