GithubHelp home page GithubHelp logo

Comments (5)

nodkz avatar nodkz commented on May 12, 2024 2

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.

nodkz avatar nodkz commented on May 12, 2024 2

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.

Morphexe avatar Morphexe commented on May 12, 2024 1

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.

ziedHamdi avatar ziedHamdi commented on May 12, 2024

https://facebook.github.io/graphql/October2016/#sec-Enum-Value

from graphql-compose-mongoose.

nodkz avatar nodkz commented on May 12, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.