2

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

1
  • nullable type here ... value passed from DB is not undefined (?) then no default used? ... it's resolver role to return/provide matching/proper/required data Commented Aug 8, 2021 at 15:24

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.