graphql-compose-mongoose icon indicating copy to clipboard operation
graphql-compose-mongoose copied to clipboard

Issue with createMany resolver

Open berrymat opened this issue 7 years ago • 4 comments

I just discovered this project and I am so glad I did!

However, I am having a problem with version 4.6.0. I am using typescript 3.0.1. I have a very simple setup (based on the example in the readme):

const customizationOptions = {}; // left it empty for simplicity, described below
const CountryTC = composeWithMongoose(country, customizationOptions);

// STEP 3: Add needed CRUD Country operations to the GraphQL Schema
// via graphql-compose it will be much much easier, with less typing
schemaComposer.Query.addFields({
  countryById: CountryTC.getResolver('findById'),
  countryByIds: CountryTC.getResolver('findByIds'),
  countryOne: CountryTC.getResolver('findOne'),
  countryMany: CountryTC.getResolver('findMany'),
  countryCount: CountryTC.getResolver('count'),
  countryConnection: CountryTC.getResolver('connection'),
  countryPagination: CountryTC.getResolver('pagination'),
});

schemaComposer.Mutation.addFields({
  countryCreateOne: CountryTC.getResolver('createOne'),
  countryCreateMany: CountryTC.getResolver('createMany'),
  countryUpdateById: CountryTC.getResolver('updateById'),
  countryUpdateOne: CountryTC.getResolver('updateOne'),
  countryUpdateMany: CountryTC.getResolver('updateMany'),
  countryRemoveById: CountryTC.getResolver('removeById'),
  countryRemoveOne: CountryTC.getResolver('removeOne'),
  countryRemoveMany: CountryTC.getResolver('removeMany'),
});

const graphqlSchema = schemaComposer.buildSchema();
export default graphqlSchema;

When I launch my app, I get the following exception:

/Users/berrymat/GitLab/NexxusTDO/MiddleLayer/node_modules/graphql/type/definition.js:47
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } /**
                                                                                            ^
TypeError: Cannot call a class as a function
    at _classCallCheck (/.../node_modules/graphql/type/definition.js:47:99)
    at Object.GraphQLList (/.../node_modules/graphql/type/definition.js:736:5)
    at createMany (/.../node_modules/graphql-compose-mongoose/lib/resolvers/createMany.js:74:82)
    at names.forEach.resolverName (/.../node_modules/graphql-compose-mongoose/lib/composeWithMongoose.js:105:26)
    at Array.forEach (<anonymous>)
    at createResolvers (/.../node_modules/graphql-compose-mongoose/lib/composeWithMongoose.js:101:9)
    at Object.composeWithMongoose (/.../node_modules/graphql-compose-mongoose/lib/composeWithMongoose.js:49:5)
    at Object.<anonymous> (/.../dist/main.js:46985:44)
    at __webpack_require__ (/.../dist/main.js:21:30)
    at Object.<anonymous> (/.../dist/main.js:45317:25)
    at __webpack_require__ (/.../dist/main.js:21:30)
    at /.../dist/main.js:64:18
    at Object.<anonymous> (/.../dist/main.js:67:10)
    at Module._compile (internal/modules/cjs/loader.js:699:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
    at Module.load (internal/modules/cjs/loader.js:612:32)

If I revert to version 4.5.3, I no longer get the issue.

berrymat avatar Aug 28 '18 21:08 berrymat

In quickly looking at the code for createMany.js...

Current code:

  const resolver = new tc.constructor.schemaComposer.Resolver({
    name: 'createMany',
    kind: 'mutation',
    description: 'Creates Many documents with mongoose defaults, setters, hooks and validation',
    type: outputType,
    args: {
      records: {
        type: new graphql.GraphQLNonNull(
          graphql.GraphQLList(
            (recordHelperArgs(tc, {
              recordTypeName: `CreateMany${tc.getTypeName()}Input`,
              removeFields: ['id', '_id'],
              isRequired: true,
              ...(opts && opts.records),
            }).record: any).type
          )
        ),
      },
    },

Changing it to call new on graphql.GraphQLList at least fixes the exception for me:

  const resolver = new tc.constructor.schemaComposer.Resolver({
    name: 'createMany',
    kind: 'mutation',
    description: 'Creates Many documents with mongoose defaults, setters, hooks and validation',
    type: outputType,
    args: {
      records: {
        type: new graphql.GraphQLNonNull(
          new graphql.GraphQLList(
            (recordHelperArgs(tc, {
              recordTypeName: `CreateMany${tc.getTypeName()}Input`,
              removeFields: ['id', '_id'],
              isRequired: true,
              ...(opts && opts.records),
            }).record: any).type
          )
        ),
      },
    },

berrymat avatar Aug 28 '18 21:08 berrymat

Thanks for fix! I'm glad too for great feedback and of cource your pull request. 🍻

nodkz avatar Aug 29 '18 03:08 nodkz

Hi Pavel:

Thanks. I’ve just sent you another small pull request.

On another subject - Where is a good place to ask questions?

Regards, Matthew

Sent from my iPhone

On Aug 28, 2018, at 11:40 PM, Pavel Chertorogov [email protected] wrote:

Thanks for fix! I'm glad too for great feedback and of cource your pull request. 🍻

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

berrymat avatar Aug 29 '18 12:08 berrymat

Feel free to open issue 😉

nodkz avatar Aug 29 '18 12:08 nodkz