GraphQL: Support required fields on output type (#6279)

* Handle required fields

* Fix output fields
This commit is contained in:
Antoine Cormouls
2019-12-15 05:12:04 +01:00
committed by Antonio Davi Macedo Coelho de Castro
parent 2fc328ed24
commit a72ab50c70
3 changed files with 98 additions and 194 deletions

250
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -1886,6 +1886,32 @@ describe('ParseGraphQLServer', () => {
expect(
__type.inputFields.find(o => o.name === 'doors').type.kind
).toEqual('NON_NULL');
const {
data: { __type: __type2 },
} = await apolloClient.query({
query: gql`
query requiredFields {
__type(name: "SuperCar") {
fields {
name
type {
kind
}
}
}
}
`,
});
expect(
__type2.fields.find(o => o.name === 'price').type.kind
).toEqual('SCALAR');
expect(
__type2.fields.find(o => o.name === 'engine').type.kind
).toEqual('NON_NULL');
expect(
__type2.fields.find(o => o.name === 'doors').type.kind
).toEqual('NON_NULL');
});
it('should only allow the supplied output fields for a class', async () => {

View File

@@ -406,7 +406,9 @@ const load = (
[field]: {
description: `This is the object ${field}.`,
args,
type,
type: parseClass.fields[field].required
? new GraphQLNonNull(type)
: type,
async resolve(source, args, context, queryInfo) {
try {
const {
@@ -476,7 +478,9 @@ const load = (
...fields,
[field]: {
description: `This is the object ${field}.`,
type,
type: parseClass.fields[field].required
? new GraphQLNonNull(type)
: type,
async resolve(source) {
if (source[field] && source[field].coordinates) {
return source[field].coordinates.map(coordinate => ({
@@ -494,7 +498,9 @@ const load = (
...fields,
[field]: {
description: `Use Inline Fragment on Array to get results: https://graphql.org/learn/queries/#inline-fragments`,
type,
type: parseClass.fields[field].required
? new GraphQLNonNull(type)
: type,
async resolve(source) {
if (!source[field]) return null;
return source[field].map(async elem => {
@@ -516,7 +522,9 @@ const load = (
...fields,
[field]: {
description: `This is the object ${field}.`,
type,
type: parseClass.fields[field].required
? new GraphQLNonNull(type)
: type,
},
};
} else {