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

Commit 4539ff0

Browse files
author
Enda Phelan
committed
fix(codegen-schema): create subscription fields if mutations disabled
1 parent 4c8d0b9 commit 4539ff0

File tree

4 files changed

+109
-4
lines changed

4 files changed

+109
-4
lines changed

docs/releases.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Please follow individual releases for more information.
2727
* Configure relationship auth rule with field instead of database key ([#2101](https://github.com/aerogear/graphback/pull/2073), fixed by [525bc9a](https://github.com/aerogear/graphback/commit/525bc9a641fa7cb1818a0727a675564e6fa12dda))
2828
* Fix `Could not find a declaration file for module 'bson'` error ([#2104](https://github.com/aerogear/graphback/pull/2104), fixed by [4f9ce7c](https://github.com/aerogear/graphback/commit/4f9ce7c2d6c494b33f447e1b4d6a47fbd880f353))
2929
* Add missing interface ([#2019](https://github.com/aerogear/graphback/pull/2109), fixed by [f46e920](https://github.com/aerogear/graphback/commit/f46e9200def565b0b0e34ccc13f7efa50f346550))
30+
* Generate schema subscription fields when mutations are disabled ([#2114](https://github.com/aerogear/graphback/2114), fixed by [212eb7a](https://github.com/aerogear/graphback/commit/212eb7a3e718eb102c226c237ce2448a2aa26898))
3031

3132
### Breaking Changes
3233

packages/graphback-codegen-schema/src/SchemaCRUDPlugin.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ export class SchemaCRUDPlugin extends GraphbackPlugin {
161161
buildSubscriptionFilterType(schemaComposer, modelType);
162162

163163
const subscriptionFields = {}
164-
if (model.crudOptions.subCreate && model.crudOptions.create) {
164+
if (model.crudOptions.subCreate) {
165165
const operation = getSubscriptionName(name, GraphbackOperationType.CREATE)
166166

167167
const filterInputName = getInputTypeName(name, GraphbackOperationType.SUBSCRIPTION_CREATE)
@@ -176,7 +176,7 @@ export class SchemaCRUDPlugin extends GraphbackPlugin {
176176
}
177177
};
178178
}
179-
if (model.crudOptions.subUpdate && model.crudOptions.update) {
179+
if (model.crudOptions.subUpdate) {
180180
const operation = getSubscriptionName(name, GraphbackOperationType.UPDATE)
181181

182182
const filterInputName = getInputTypeName(name, GraphbackOperationType.SUBSCRIPTION_UPDATE)
@@ -191,7 +191,7 @@ export class SchemaCRUDPlugin extends GraphbackPlugin {
191191
}
192192
};
193193
}
194-
if (model.crudOptions.subDelete && model.crudOptions.delete) {
194+
if (model.crudOptions.subDelete) {
195195
const operation = getSubscriptionName(name, GraphbackOperationType.DELETE)
196196

197197
const filterInputName = getInputTypeName(name, GraphbackOperationType.SUBSCRIPTION_DELETE)

packages/graphback-codegen-schema/tests/GraphQLSchemaCreatorTest.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable max-lines */
22
import { readFileSync } from 'fs';
33
import { buildSchema, printSchema, GraphQLObjectType, assertInputObjectType } from 'graphql';
4-
import { GraphbackCoreMetadata, printSchemaWithDirectives, GraphbackPluginEngine, metadataMap } from '@graphback/core';
4+
import { GraphbackCoreMetadata, printSchemaWithDirectives, GraphbackPluginEngine, metadataMap, GraphbackCRUDGeneratorConfig } from '@graphback/core';
55

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

@@ -72,6 +72,29 @@ test('Test snapshot config js', async () => {
7272
expect(printSchema(schema)).toMatchSnapshot();
7373
});
7474

75+
test('Create subscription fields when mutations are disabled', () => {
76+
const crudMethods: GraphbackCRUDGeneratorConfig = {
77+
create: false,
78+
update: false,
79+
delete: false
80+
};
81+
82+
const schemaGenerator = new SchemaCRUDPlugin()
83+
const metadata = new GraphbackCoreMetadata({
84+
crudMethods
85+
}, buildSchema(`
86+
"""@model"""
87+
type Note {
88+
id: ID!
89+
title: String
90+
}
91+
`))
92+
const schema = schemaGenerator.transformSchema(metadata);
93+
expect(Object.keys(schema.getSubscriptionType().getFields())).toEqual(['newNote', 'updatedNote', 'deletedNote']);
94+
expect(schema.getMutationType()).toBeUndefined();
95+
expect(printSchema(schema)).toMatchSnapshot();
96+
})
97+
7598
test('Test one side relationship schema query type generation', async () => {
7699
const defautConfig = {
77100
"create": false,

packages/graphback-codegen-schema/tests/__snapshots__/GraphQLSchemaCreatorTest.ts.snap

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,86 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`Create subscription fields when mutations are disabled 1`] = `
4+
"type Query {
5+
getNote(id: ID!): Note
6+
findNotes(filter: NoteFilter, page: PageRequest, orderBy: OrderByInput): NoteResultList!
7+
}
8+
9+
\\"\\"\\"@model\\"\\"\\"
10+
type Note {
11+
id: ID!
12+
title: String
13+
}
14+
15+
type NoteResultList {
16+
items: [Note]!
17+
offset: Int
18+
limit: Int
19+
count: Int
20+
}
21+
22+
input NoteFilter {
23+
id: IDInput
24+
title: StringInput
25+
and: [NoteFilter!]
26+
or: [NoteFilter!]
27+
not: NoteFilter
28+
}
29+
30+
input IDInput {
31+
ne: ID
32+
eq: ID
33+
le: ID
34+
lt: ID
35+
ge: ID
36+
gt: ID
37+
in: [ID!]
38+
}
39+
40+
input StringInput {
41+
ne: String
42+
eq: String
43+
le: String
44+
lt: String
45+
ge: String
46+
gt: String
47+
in: [String!]
48+
contains: String
49+
startsWith: String
50+
endsWith: String
51+
}
52+
53+
input PageRequest {
54+
limit: Int
55+
offset: Int
56+
}
57+
58+
input OrderByInput {
59+
field: String!
60+
order: SortDirectionEnum = ASC
61+
}
62+
63+
enum SortDirectionEnum {
64+
DESC
65+
ASC
66+
}
67+
68+
type Subscription {
69+
newNote(filter: NoteSubscriptionFilter): Note!
70+
updatedNote(filter: NoteSubscriptionFilter): Note!
71+
deletedNote(filter: NoteSubscriptionFilter): Note!
72+
}
73+
74+
input NoteSubscriptionFilter {
75+
and: [NoteSubscriptionFilter!]
76+
or: [NoteSubscriptionFilter!]
77+
not: NoteSubscriptionFilter
78+
id: IDInput
79+
title: StringInput
80+
}
81+
"
82+
`;
83+
384
exports[`Creates CRUD resolvers for models 1`] = `
485
Object {
586
"Comment": Object {

0 commit comments

Comments
 (0)