How to set global behavior of GraphQL with default values for object and array ?
Some examples
const AddressTC = schemaComposer.createObjectTC({
name: 'Address',
fields: {
street: {type: 'String',defaultValue: null},
number: {type: 'String', defaultValue: null},
postcode: {type: 'String', defaultValue: null},
city: {type: 'String', defaultValue: null},
comment: {type: 'String', defaultValue: null},
country: {type: 'String', defaultValue: null},
quality: {type: 'QualityEnum', defaultValue: 'BAD'},
}
});
const customerTC = schemaComposer.createObjectTC({
name: 'Customer',
fields: {
address: AddressTC
needs: {
type : ['objectID'],
defaultValue: []
},
documents: {
type: schemaComposer.getOTC('Document').List,
defaultValue: []
},
}
});
In my case I use noSQL database and when a field doesn't exist or is empty, the request GraphQL always returns null then crashed my app with wrongs data type.
INFO : I use graphql-compose
I found this post, whith the same problem, but this solution not useful for me with a large API and dynamics resolvers. Graphql, how to return empty array instead of null