Comments (5)
Graphql-compose does not have a dedicated helper for complex Enums, but should. For now, I do not have a good idea how it should be implemented.
But as a workaround for simple enums (where key === value) you may use:
UserTC.setFields({
myEnum1: 'enum MyEnum1 { ID NAME AGE }', // shortand field definition
myEnum2: {
type: 'enum MyEnum2 { ID NAME AGE }',
description: 'full field definition',
resolve: (source, args, context, info) => {},
},
});
// or you may use TypeMapper and reuse enum in different places
import { TypeMapper } from 'graphql-compose';
const typeMapper = new TypeMapper();
export const MyEnum3 = typeMapper.createType('enum MyEnum2 { ID NAME AGE }');
Complex Enum with specific values you may create in such way:
import { graphql } from 'graphql-compose';
const MyEnum4 = new graphql.GraphQLEnumType({
name: 'RGB',
values: {
RED: { value: 0 },
GREEN: { value: 1 },
BLUE: { value: 2 }
}
});
I want to add EnumComposer
module to graphql-compose
but don't have any ETA. So for complex enums use last example. In future you need just replace graphql.GraphQLEnumType
on EnumComposer
. For simple enums (key === value) use first example and it is a recommended way.
from graphql-compose-mongoose.
If your question was about Mongoose enums, so it can be defined in such way:
export const ArticleSchema = new Schema(
{
lang: {
type: String,
description: 'Language names (https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes)',
enum: ['en', 'fr', 'cn', 'kk'],
},
}
);
Graphql-compose-mongoose
understands it, and will create automatically type with such name ArticleLangEnum
(how I remember).
from graphql-compose-mongoose.
How can you then use the mongoose enums as arg in a resolver? A enum defined in schema just like you did, in this case ArticleLangEnum?
from graphql-compose-mongoose.
https://facebook.github.io/graphql/October2016/#sec-Enum-Value
from graphql-compose-mongoose.
For enum creation, you may use EnumTypeComposer
. It was added to graphql-compose
at the beginning of 2018.
https://graphql-compose.github.io/docs/en/api-EnumTypeComposer.html
How it works you may see in tests
https://github.com/graphql-compose/graphql-compose/blob/master/src/__tests__/EnumTypeComposer-test.js
from graphql-compose-mongoose.
Related Issues (20)
- Unable to compose schema for discriminator with index HOT 1
- Array field UpdateMany - $set as object instead of $push array
- beforeRecordMutate not firing HOT 2
- Internal error occurred during message handling. Please check your implementation. Error: Subscription field must return Async Iterable. Received: undefined. HOT 1
- Relation detection HOT 4
- feat: Introduce subscription resolvers using Mongoose Watch
- Inconsistency with mongoose custom types
- Unable to use _operator while filtering a connection HOT 1
- graphql-compose-mongoose with Typescript without extending Document HOT 2
- removeMany throw an error if no items to delete with mongoose v6
- Preserve the array order when using findbyIds HOT 2
- unique type option in mongoose does not work with graphql-compose-mongoose
- If a TC's findMany resolver is added to schema multiple times, filter options for subsequent resolvers are ignored HOT 2
- Type 'Cat' is not assignable to type 'Document<any, any, any>' HOT 2
- Mongoose 7 issue ( graphql type and doc validate ) HOT 1
- Array items are generated as optional
- Provide collection name from request
- Incompatible with mongoose 8 HOT 1
- Projection on addRelation not working for Discriminators
- resolvers skip asynchronous validators
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from graphql-compose-mongoose.