Skip to content

Commit e0e06ef

Browse files
Moumoulsdavimacedo
authored andcommitted
Handle required fields (#6271)
1 parent 4f1d3b0 commit e0e06ef

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

spec/ParseGraphQLServer.spec.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1850,6 +1850,44 @@ describe('ParseGraphQLServer', () => {
18501850
expect(updatedSuperCar).toBeTruthy();
18511851
});
18521852

1853+
it('should handle required fields from the Parse class', async () => {
1854+
const schemaController = await parseServer.config.databaseController.loadSchema();
1855+
await schemaController.addClassIfNotExists('SuperCar', {
1856+
engine: { type: 'String', required: true },
1857+
doors: { type: 'Number', required: true },
1858+
price: { type: 'String' },
1859+
mileage: { type: 'Number' },
1860+
});
1861+
1862+
await resetGraphQLCache();
1863+
1864+
const {
1865+
data: { __type },
1866+
} = await apolloClient.query({
1867+
query: gql`
1868+
query requiredFields {
1869+
__type(name: "CreateSuperCarFieldsInput") {
1870+
inputFields {
1871+
name
1872+
type {
1873+
kind
1874+
}
1875+
}
1876+
}
1877+
}
1878+
`,
1879+
});
1880+
expect(
1881+
__type.inputFields.find(o => o.name === 'price').type.kind
1882+
).toEqual('SCALAR');
1883+
expect(
1884+
__type.inputFields.find(o => o.name === 'engine').type.kind
1885+
).toEqual('NON_NULL');
1886+
expect(
1887+
__type.inputFields.find(o => o.name === 'doors').type.kind
1888+
).toEqual('NON_NULL');
1889+
});
1890+
18531891
it('should only allow the supplied output fields for a class', async () => {
18541892
const schemaController = await parseServer.config.databaseController.loadSchema();
18551893

src/GraphQL/loaders/parseClassTypes.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,9 @@ const load = (
159159
[field]: {
160160
description: `This is the object ${field}.`,
161161
type:
162-
className === '_User' &&
163-
(field === 'username' || field === 'password')
162+
(className === '_User' &&
163+
(field === 'username' || field === 'password')) ||
164+
parseClass.fields[field].required
164165
? new GraphQLNonNull(type)
165166
: type,
166167
},

0 commit comments

Comments
 (0)