Skip to content
Permalink
Browse files
fix(datasync): do not create empty resolvers
  • Loading branch information
Enda Phelan committed Sep 24, 2020
1 parent a14aaf6 commit faf172d0dc30c3533dd5f2377f28ea20762baf02
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 12 deletions.
@@ -76,18 +76,18 @@ export class DataSyncPlugin extends GraphbackPlugin {
* @param {GraphbackCoreMetadata} metadata - Core metatata containing all model information
*/
public createResolvers(metadata: GraphbackCoreMetadata): IResolvers {
const resolvers: IResolvers = {
Query: {},
Mutation: {},
Subscription: {}
const models = metadata.getModelDefinitions()

if (models.length === 0) {
return undefined;
}

const models = metadata.getModelDefinitions()
const resolvers: IResolvers = {}

for (const model of models) {
// If delta marker is encountered, add resolver for `delta` query
if (isDataSyncModel(model)) {
this.addDeltaSyncResolver(model, resolvers.Query as IFieldResolver<any, any>)
this.addDeltaSyncResolver(model, resolvers)
}
}

@@ -105,11 +105,15 @@ export class DataSyncPlugin extends GraphbackPlugin {
return DATASYNC_PLUGIN_NAME;
}

protected addDeltaSyncResolver(model: ModelDefinition, queryObj: IFieldResolver<any, any>) {
protected addDeltaSyncResolver(model: ModelDefinition, resolvers: IResolvers) {
const modelName = model.graphqlType.name;
const deltaQuery = getDeltaQuery(modelName)

queryObj[deltaQuery] = async (_: any, args: any, context: GraphbackContext, info: GraphQLResolveInfo) => {
if (!resolvers.Query) {
resolvers.Query = {} as IFieldResolver<any, any>
}

resolvers.Query[deltaQuery] = async (_: any, args: any, context: GraphbackContext, info: GraphQLResolveInfo) => {
if (!context.graphback || !context.graphback[modelName]) {
throw new Error(`Missing service for ${modelName}`);
}
@@ -1,6 +1,6 @@
import { readFileSync } from 'fs';
import { buildSchema, printSchema } from 'graphql';
import { GraphbackCoreMetadata } from '@graphback/core';
import { GraphbackCoreMetadata, GraphbackPluginEngine } from '@graphback/core';
import { SchemaCRUDPlugin } from "@graphback/codegen-schema";
import { DataSyncPlugin } from '../src/DataSyncPlugin';

@@ -18,7 +18,7 @@ test('Test snapshot config gql', async () => {
"subDelete": true
}

const schemaPlugin = new SchemaCRUDPlugin();
const schemaPlugin = new SchemaCRUDPlugin();
const datasync = new DataSyncPlugin()
const metadata = new GraphbackCoreMetadata({
crudMethods: defautConfig
@@ -42,7 +42,7 @@ it('uses existing GraphbackTimestamp scalars', async () => {
"subDelete": true
}

const schemaPlugin = new SchemaCRUDPlugin();
const schemaPlugin = new SchemaCRUDPlugin();
const datasync = new DataSyncPlugin()
const metadata = new GraphbackCoreMetadata({
crudMethods: defautConfig
@@ -80,7 +80,7 @@ it('adds version when conflicts are enabled', async () => {
"subDelete": true
}

const schemaPlugin = new SchemaCRUDPlugin();
const schemaPlugin = new SchemaCRUDPlugin();
const datasync = new DataSyncPlugin({ conflictConfig: { enabled: true } })
const metadata = new GraphbackCoreMetadata({
crudMethods: defautConfig
@@ -105,3 +105,40 @@ it('adds version when conflicts are enabled', async () => {
const schema = datasync.transformSchema(metadata)
expect(printSchema(schema)).toMatchSnapshot();
});

test('When all CRUD flags are disabled, resolvers and root schema types are not generated', () => {
const model = buildSchema(`
"""@model"""
type Note {
id: ID!
title: String
}`)

const pluginEngine = new GraphbackPluginEngine({
schema: model,
plugins: [
new DataSyncPlugin()
],
config: {
crudMethods: {
find: false,
findOne: false,
create: false,
update: false,
delete: false,
subCreate: false,
subDelete: false,
subUpdate: false
}
}
});

const metadata = pluginEngine.createResources();
const schema = metadata.getSchema();
const resolvers = metadata.getResolvers();

expect(resolvers).toBeUndefined()
expect(schema.getQueryType()).toBeUndefined();
expect(schema.getMutationType()).toBeUndefined();
expect(schema.getSubscriptionType()).toBeUndefined();
});

0 comments on commit faf172d

Please sign in to comment.