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

Commit

Permalink
Merge branch 'master' into renovate/major-all
Browse files Browse the repository at this point in the history
  • Loading branch information
machi1990 committed Aug 21, 2020
2 parents 7cc0669 + ecf2e2a commit 80dfcb8
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 22 deletions.
2 changes: 2 additions & 0 deletions docs/releases.md
Expand Up @@ -39,6 +39,8 @@ Please follow individual releases for more information.

* Do not remove directives on Object Types in generated schema ([#1767](https://github.com/aerogear/graphback/issues/1767)), fixed by ([22c7a1da62e2752a21db4e4552e970aa93ba37ae](https://github.com/aerogear/graphback/pull/1810/commits/22c7a1da62e2752a21db4e4552e970aa93ba37ae))

* Do not silently overrides createdAt/updatedAt custom fields when model contains @versioned annotation [#1597](https://github.com/aerogear/graphback/issues/1597)

### Breaking Change

* Use `GraphbackDateTime` scalar for generated createdAt updatedAt fields ([#1349](https://github.com/aerogear/graphback/issues/1349), fixed by [#1862](https://github.com/aerogear/graphback/pull/1862))
Expand Down
17 changes: 14 additions & 3 deletions packages/graphback-codegen-schema/src/SchemaCRUDPlugin.ts
Expand Up @@ -350,13 +350,24 @@ export class SchemaCRUDPlugin extends GraphbackPlugin {
protected addVersionedMetadataFields(schemaComposer: SchemaComposer<any>, models: ModelDefinition[]) {
const timeStampInputName = getInputName(GraphbackTimestamp);
let timestampInputType: GraphQLInputObjectType; let timestampType: GraphQLScalarType;

models.forEach((model: ModelDefinition) => {
for (const model of models) {
const name = model.graphqlType.name;
const modelTC = schemaComposer.getOTC(name);
const desc = model.graphqlType.description;
const { markers } = metadataMap;
if (parseMetadata(markers.versioned, desc)) {
const updateField = model.fields[metadataMap.fieldNames.updatedAt];
const createAtField = model.fields[metadataMap.fieldNames.createdAt];
const errorMessage = (field: string) => `Type "${model.graphqlType.name}" 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.name}" or remove the field.`

if (createAtField && createAtField.type !== GraphbackTimestamp.name) {
throw new Error(errorMessage(metadataMap.fieldNames.createdAt));
}

if (updateField && updateField.type !== GraphbackTimestamp.name) {
throw new Error(errorMessage(metadataMap.fieldNames.updatedAt));
}

if (!timestampInputType) {
if (schemaComposer.has(GraphbackTimestamp.name)) {
timestampInputType = schemaComposer.getITC(timeStampInputName).getType();
Expand All @@ -380,7 +391,7 @@ export class SchemaCRUDPlugin extends GraphbackPlugin {
inputType.addFields(metadataInputFields);
}
}
});
};
}

/**
Expand Down
85 changes: 75 additions & 10 deletions packages/graphback-codegen-schema/tests/GraphQLSchemaCreatorTest.ts
@@ -1,6 +1,7 @@
import { readFileSync } from 'fs';
import { buildSchema, printSchema, GraphQLObjectType } from 'graphql';
import { GraphbackCoreMetadata, printSchemaWithDirectives, GraphbackPluginEngine } from '@graphback/core';
import { GraphbackCoreMetadata, printSchemaWithDirectives, GraphbackPluginEngine, metadataMap } from '@graphback/core';

import { SchemaCRUDPlugin } from '../src/SchemaCRUDPlugin';

const schemaText = readFileSync(`${__dirname}/mock.graphql`, 'utf8')
Expand Down Expand Up @@ -281,18 +282,82 @@ type Comment {

test('schema does not generate filter input for unknown custom scalar', () => {
const modelAST = `
scalar MyCustomScalar
"""
@model
"""
type TypeWithCustomScalar {
id: ID!
customField: MyCustomScalar
}`
scalar MyCustomScalar
"""
@model
"""
type TypeWithCustomScalar {
id: ID!
customField: MyCustomScalar
}`

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

expect(schema.getType('MyCustomScalarInput')).toBeUndefined()
})
})

test('schema does not override custom createdAt and updatedAt fields when model has @versioned annotation', () => {
const fields = Object.values(metadataMap.fieldNames);
for (const field of fields) {
const modelAST = `
"""
@model
@versioned
"""
type Entity {
id: ID!
${field}: Int
}`;

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

try {
schemaGenerator.transformSchema(metadata);
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.`)
}
}
})

test('schema does throw an error when model annotated with @versioned contain custom createdAt or updatedAt fields having GraphbackTimestamp type', () => {
const modelAST = `
scalar GraphbackTimestamp
"""
@model
@versioned
"""
type Entity1 {
id: ID!
createdAt: GraphbackTimestamp
updatedAt: GraphbackTimestamp
}
"""
@model
@versioned
"""
type Entity2 {
id: ID!
createdAt: GraphbackTimestamp
}
"""
@model
@versioned
"""
type Entity3 {
id: ID!
updatedAt: GraphbackTimestamp
}
`;

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

const schema = schemaGenerator.transformSchema(metadata);
expect(schema).toBeDefined()
})
2 changes: 0 additions & 2 deletions packages/graphback-core/src/plugin/GraphbackCoreMetadata.ts
Expand Up @@ -94,7 +94,6 @@ export class GraphbackCoreMetadata {
//Merge CRUD options from type with global ones
crudOptions = Object.assign({}, this.supportedCrudMethods, crudOptions)
// Whether to add delta queries
const deltaSync = parseMetadata('delta', modelType);
const { type: primaryKeyType, name } = getPrimaryKey(modelType);
const primaryKey = {
name,
Expand Down Expand Up @@ -133,7 +132,6 @@ export class GraphbackCoreMetadata {
primaryKey,
crudOptions,
relationships,
config: { deltaSync },
graphqlType: modelType
};
}
Expand Down
6 changes: 1 addition & 5 deletions packages/graphback-core/src/plugin/ModelDefinition.ts
Expand Up @@ -22,11 +22,7 @@ export type ModelDefinition = {
fields: ModelFieldMap
graphqlType: GraphQLObjectType,
relationships: FieldRelationshipMetadata[]
crudOptions: GraphbackCRUDGeneratorConfig,
config: {
// Whether to add delta queries; requires datasync package
deltaSync: boolean
}
crudOptions: GraphbackCRUDGeneratorConfig
};

export function getModelByName(name: string, models: ModelDefinition[]): ModelDefinition | undefined {
Expand Down
21 changes: 19 additions & 2 deletions packages/graphback-core/tests/MetadataTest.ts
Expand Up @@ -34,11 +34,28 @@ type Note {

const models = metadata.getModelDefinitions();

const { crudOptions, config, relationships } = getModelByName('Note', models)
const { crudOptions, fields, relationships, primaryKey } = getModelByName('Note', models)

expect(crudOptions).toBeDefined()
expect(config.deltaSync).toBe(true)
expect(relationships).toHaveLength(1)
expect(fields).toEqual({
id: {
name: 'id',
type: 'ID'
},
title: {
name: 'title',
type: 'String'
},
comments: {
name: 'id', // indicates that the id field should be selected
type: 'ID'
}
})
expect(primaryKey).toEqual({
name: 'id',
type: 'ID'
})
});

test('Model has default crud configuration', async () => {
Expand Down

0 comments on commit 80dfcb8

Please sign in to comment.