Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

How are enums defined in the schema of apollo?


Asked by Henrik Davis on Dec 03, 2021 FAQ



An enum is similar to a scalar type, but its legal values are defined in the schema. Here's an example definition: Enums are most useful in situations where the user must pick from a prescribed list of options. As an additional benefit, enum values autocomplete in tools like the Apollo Studio Explorer.
And,
As the error message is suggesting, Schema type definitions not allowed in queries., you can't add an enum definition in an operation document (ExecutableDefinition). You can only have operations (query, mutation, or subscription), or fragments definitions. That is, this is invalid:
Additionally, A directive decorates part of a GraphQL schema or operation with additional configuration. Tools like Apollo Server (and Apollo Client) can read a GraphQL document's directives and perform custom logic as appropriate. Directives are preceded by the @ character, like so:
Accordingly,
This is quite counterproductive since the principal value of GraphQL type-based schemas is to provide validation out of the box on operations and data. Using enums will limit the status value to a given set of defined values and avoid duplicate validations logic.
Besides,
Sometimes, a backend forces a different value for an enum internally than in the public API. You can set each enum value's corresponding internal value in the resolver map you provide to Apollo Server. This feature usually isn't required unless another library in your application expects enum values in a different form.