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

Commit

Permalink
refactor(core): decrease number of times model definitions are built
Browse files Browse the repository at this point in the history
  • Loading branch information
Enda Phelan committed Sep 17, 2020
1 parent 9de5e03 commit e85f90b
Show file tree
Hide file tree
Showing 14 changed files with 178 additions and 1,232 deletions.
33 changes: 21 additions & 12 deletions packages/graphback-codegen-client/tests/GraphQLClientCreatorTest.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
//tslint:disable-next-line: match-default-export-name no-implicit-dependencies
import { readFileSync } from 'fs';
import { GraphbackCoreMetadata } from '@graphback/core';
import { readFileSync, mkdirSync, rmdirSync } from 'fs';
import { GraphbackCoreMetadata, GraphbackCRUDGeneratorConfig, GraphbackPlugin, GraphbackPluginEngine } from '@graphback/core';
import { buildSchema } from 'graphql';
import { SchemaCRUDPlugin } from '@graphback/codegen-schema';
import { ClientCRUDPlugin } from '../src'

const schemaText = readFileSync(`${__dirname}/mock.graphql`, 'utf8')

test('Test plugin engine ts', async () => {
const crudMethods = {
"create": true,
"update": true,
"findOne": true,
"find": true,
"delete": true,
}
const setup = (schemaStr: string, { crudMethods, plugins }: { crudMethods: GraphbackCRUDGeneratorConfig, plugins: GraphbackPlugin[] }): { metadata: GraphbackCoreMetadata } => {
const pluginEngine = new GraphbackPluginEngine({ schema: buildSchema(schemaStr), plugins, config: { crudMethods } });

const metadata = new GraphbackCoreMetadata({ crudMethods }, buildSchema(schemaText))
return { metadata: pluginEngine.createResources() }
}

beforeEach(() => {
mkdirSync('./tmp')
})

afterEach(() => {
rmdirSync('./tmp', { recursive: true });
})

test('Test plugin engine ts', async () => {
const schemaPlugin = new SchemaCRUDPlugin()
const plugin = new ClientCRUDPlugin({ outputFile: './tmp/generated.ts', fragmentOnly: false });
const { metadata } = setup(schemaText, { crudMethods: {}, plugins: [schemaPlugin, plugin] })
expect(plugin.getDocuments(metadata)).toMatchSnapshot();
});

Expand All @@ -30,7 +38,8 @@ test('Test plugin engine graphql', async () => {
"delete": true,
}

const metadata = new GraphbackCoreMetadata({ crudMethods }, buildSchema(schemaText))
const metadata = new GraphbackCoreMetadata({ crudMethods }, buildSchema(schemaText));
metadata.setModelDefinitions();
const plugin = new ClientCRUDPlugin({ outputFile: './tmp/generated.graphql', fragmentOnly: false });
expect(plugin.getDocuments(metadata)).toMatchSnapshot();
});
Expand Down
179 changes: 53 additions & 126 deletions packages/graphback-codegen-schema/tests/GraphQLSchemaCreatorTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,81 +8,16 @@ import { SchemaCRUDPlugin } from '../src/SchemaCRUDPlugin';
const schemaText = readFileSync(`${__dirname}/mock.graphql`, 'utf8')

test('Test snapshot config gql', async () => {
const defautConfig = {
"create": true,
"update": true,
"findOne": true,
"find": true,
"delete": true,
"subCreate": true,
"subUpdate": true,
"subDelete": true
}


const schemaGenerator = new SchemaCRUDPlugin()
const metadata = new GraphbackCoreMetadata({
crudMethods: defautConfig
}, buildSchema(schemaText))
const schema = schemaGenerator.transformSchema(metadata)
expect(printSchemaWithDirectives(schema)).toMatchSnapshot();
});


test('Test snapshot config ts', async () => {
const defautConfig = {
"create": true,
"update": true,
"findOne": true,
"find": true,
"delete": true,
"subCreate": true,
"subUpdate": true,
"subDelete": true
}

const pluginEngine = new GraphbackPluginEngine({ schema: buildSchema(schemaText), plugins: [schemaGenerator] })
const metadata = pluginEngine.createResources()
const schema = metadata.getSchema()

const schemaGenerator = new SchemaCRUDPlugin()
const metadata = new GraphbackCoreMetadata({
crudMethods: defautConfig
}, buildSchema(schemaText))
const schema = schemaGenerator.transformSchema(metadata)
expect(printSchema(schema)).toMatchSnapshot();
expect(printSchemaWithDirectives(schema)).toMatchSnapshot();
});


test('Test snapshot config js', async () => {
const defautConfig = {
"create": true,
"update": true,
"findOne": true,
"find": true,
"delete": true,
"subCreate": true,
"subUpdate": true,
"subDelete": true
}


const schemaGenerator = new SchemaCRUDPlugin()
const metadata = new GraphbackCoreMetadata({
crudMethods: defautConfig
}, buildSchema(schemaText))
const schema = schemaGenerator.transformSchema(metadata)
expect(printSchema(schema)).toMatchSnapshot();
});

test('Test one side relationship schema query type generation', async () => {
const defautConfig = {
"create": false,
"update": false,
"findOne": true,
"find": true,
"delete": false,
"subCreate": false,
"subUpdate": false,
"subDelete": false
}

const modelText = `""" @model """
type Note {
Expand All @@ -104,29 +39,32 @@ test('Test one side relationship schema query type generation', async () => {
}
`;

const oneSidedSchema = buildSchema(modelText);
const schemaGenerator = new SchemaCRUDPlugin()
const metadata = new GraphbackCoreMetadata({
crudMethods: defautConfig
}, oneSidedSchema)
const pluginEngine = new GraphbackPluginEngine({
schema: buildSchema(modelText),
plugins: [schemaGenerator],
config: {
crudMethods: {
"create": false,
"update": false,
"findOne": true,
"find": true,
"delete": false,
"subCreate": false,
"subUpdate": false,
"subDelete": false
}
}
});

const transformedSchema = schemaGenerator.transformSchema(metadata)
expect(printSchema(transformedSchema)).toMatchSnapshot()
const metadata = pluginEngine.createResources()
const schema = metadata.getSchema()

expect(printSchema(schema)).toMatchSnapshot()
});


test('Model has missing relationship annotations', async () => {
const defautConfig = {
"create": false,
"update": false,
"findOne": true,
"find": true,
"delete": false,
"subCreate": false,
"subUpdate": false,
"subDelete": false
};

let modelText = `
""" @model """
type Note {
Expand All @@ -143,12 +81,10 @@ test('Model has missing relationship annotations', async () => {
}
`;

let schema = buildSchema(modelText);
let schemaGenerator = new SchemaCRUDPlugin();
let metadata = new GraphbackCoreMetadata({ crudMethods: defautConfig }, schema);

try {
schemaGenerator.transformSchema(metadata);
const schemaGenerator = new SchemaCRUDPlugin()
const pluginEngine = new GraphbackPluginEngine({ schema: buildSchema(modelText), plugins: [schemaGenerator] })
pluginEngine.createResources()
expect(true).toBeFalsy(); // should not reach here
} catch (error) {
expect(error.message).toEqual(`Missing relationship definition on: "Note.tests". Visit https://graphback.dev/docs/model/datamodel#relationships to see how you can define relationship in your business model.`);
Expand All @@ -170,29 +106,17 @@ test('Model has missing relationship annotations', async () => {
}
`;

schema = buildSchema(modelText);
schemaGenerator = new SchemaCRUDPlugin();
metadata = new GraphbackCoreMetadata({ crudMethods: defautConfig }, schema);

try {
schemaGenerator.transformSchema(metadata);
const schemaGenerator = new SchemaCRUDPlugin()
const pluginEngine = new GraphbackPluginEngine({ schema: buildSchema(modelText), plugins: [schemaGenerator] })
pluginEngine.createResources()
expect(true).toBeFalsy(); // should not reach here
} catch (error) {
expect(error.message).toEqual(`Missing relationship definition on: "Note.test". Visit https://graphback.dev/docs/model/datamodel#relationships to see how you can define relationship in your business model.`);
}
});

test('Non-model type has model-type field', () => {
const defautConfig = {
"create": true,
"update": true,
"findOne": true,
"find": true,
"delete": true,
"subCreate": true,
"subUpdate": true,
"subDelete": true
}

const modelText = `
type JWTAuthResult {
Expand All @@ -212,10 +136,10 @@ type Query {
}`

const schemaGenerator = new SchemaCRUDPlugin()
const metadata = new GraphbackCoreMetadata({
crudMethods: defautConfig
}, buildSchema(modelText))
const schema = schemaGenerator.transformSchema(metadata)
const pluginEngine = new GraphbackPluginEngine({ schema: buildSchema(modelText), plugins: [schemaGenerator] })
const metadata = pluginEngine.createResources();
const schema = metadata.getSchema();

expect(schema.getType('JWTAuthResult')).toBeDefined()
})

Expand All @@ -233,7 +157,6 @@ test('Creates CRUD resolvers for models', async () => {
});

test('field directives on relationship fields are mapped to schema', () => {
const schemaGenerator = new SchemaCRUDPlugin();
const modelAST = `directive @test on FIELD_DEFINITION
""" @model """
Expand All @@ -257,8 +180,10 @@ type Comment {
note: Note! @test
}`

const metadata = new GraphbackCoreMetadata({ crudMethods: {} }, buildSchema(modelAST));
const schema = schemaGenerator.transformSchema(metadata);
const schemaGenerator = new SchemaCRUDPlugin()
const pluginEngine = new GraphbackPluginEngine({ schema: buildSchema(modelAST), plugins: [schemaGenerator] })
const metadata = pluginEngine.createResources()
const schema = metadata.getSchema();

const noteType = schema.getType('Note') as GraphQLObjectType
const { comments, comment } = noteType.getFields()
Expand Down Expand Up @@ -292,9 +217,10 @@ test('schema does not generate filter input for unknown custom scalar', () => {
customField: MyCustomScalar
}`

const schemaGenerator = new SchemaCRUDPlugin();
const metadata = new GraphbackCoreMetadata({ crudMethods: {} }, buildSchema(modelAST));
const schema = schemaGenerator.transformSchema(metadata);
const schemaGenerator = new SchemaCRUDPlugin()
const pluginEngine = new GraphbackPluginEngine({ schema: buildSchema(modelAST), plugins: [schemaGenerator] })
const metadata = pluginEngine.createResources()
const schema = metadata.getSchema();

expect(schema.getType('MyCustomScalarInput')).toBeUndefined()
})
Expand All @@ -312,11 +238,11 @@ test('schema does not override custom createdAt and updatedAt fields when model
${field}: Int
}`;

const schemaGenerator = new SchemaCRUDPlugin();
const metadata = new GraphbackCoreMetadata({ crudMethods: {} }, buildSchema(modelAST));

try {
schemaGenerator.transformSchema(metadata);
const schemaGenerator = new SchemaCRUDPlugin()
const pluginEngine = new GraphbackPluginEngine({ schema: buildSchema(modelAST), plugins: [schemaGenerator] })
pluginEngine.createResources()
expect(true).toBeFalsy(); // should not reach here
} catch (error) {
expect(error.message).toEqual(`Type "Entity" annotated with @versioned, cannot contain custom "${field}" field since it is generated automatically. Either remove the @versioned annotation, change the type of the field to "GraphbackTimestamp" or remove the field.`)
Expand Down Expand Up @@ -356,10 +282,11 @@ test('schema does throw an error when model annotated with @versioned contain cu
}
`;

const schemaGenerator = new SchemaCRUDPlugin();
const metadata = new GraphbackCoreMetadata({ crudMethods: {} }, buildSchema(modelAST));
const schemaGenerator = new SchemaCRUDPlugin()
const pluginEngine = new GraphbackPluginEngine({ schema: buildSchema(modelAST), plugins: [schemaGenerator] })
const metadata = pluginEngine.createResources()
const schema = metadata.getSchema();

const schema = schemaGenerator.transformSchema(metadata);
expect(schema).toBeDefined()
})

Expand All @@ -376,10 +303,10 @@ test('Transient field is excluded from input types', () => {
}
`;

const schemaGenerator = new SchemaCRUDPlugin();
const metadata = new GraphbackCoreMetadata({ crudMethods: {} }, buildSchema(modelAST));

const schema = schemaGenerator.transformSchema(metadata);
const schemaGenerator = new SchemaCRUDPlugin()
const pluginEngine = new GraphbackPluginEngine({ schema: buildSchema(modelAST), plugins: [schemaGenerator] })
const metadata = pluginEngine.createResources()
const schema = metadata.getSchema();

const createUserInput = assertInputObjectType(schema.getType('CreateUserInput'))
expect(Object.keys(createUserInput.getFields())).toEqual(['id', 'name'])
Expand Down
Loading

0 comments on commit e85f90b

Please sign in to comment.