Zied Hamdi
Zied Hamdi

Reputation: 2662

graphql-compose-mongoose doesn't execute like queries

I was glad to discover graphql-compose, and I started working with it in my projet. Actually I have an issue querying a litte more complex queries than the ones mentioned in the tutorials. For example I'd like to do a LIKE query on a mongoose table. If I modify the tutorial example to filter by name with an exact match, everything works fine

{
  userMany(filter: { name: "MICHA" }, limit: 5, sort: _ID_ASC) {
    name
    languages {
      language
      skill
    }
    contacts {
      email
    }
    gender
    age
  }
}

But if I attempt to use a regex, I'm immediately stuck like in this example where my query is

{
  userMany(filter: { name: "/MICH/i" }, limit: 5, sort: _ID_ASC) {
    name
    languages {
      language
      skill
    }
    contacts {
      email
    }
    gender
    age
  }
}

p.s: sorry, I was obliged to use the graphql tag instead of graphql-compose since the latter still doesn't exist, and I don't have enough credits to create it :-(

Upvotes: 0

Views: 1378

Answers (1)

Zied Hamdi
Zied Hamdi

Reputation: 2662

I found the solution as mentioned in the comments above:

import { composeWithMongoose } from 'graphql-compose-mongoose';
import {  composeWithRelay } from 'graphql-compose-relay';
import { schemaComposer } from 'graphql-compose';

const Entity = require ('../db/entity').default

const customizationOptions = {}; // left it empty for simplicity, described below
const EntityTC = composeWithRelay(composeWithMongoose(Entity, customizationOptions));

//export const ProductTC = composeWithRelay(composeWithMongoose(Product));

//TODO make this function evolve to allow filtering on a specified field in params
//Found in https://github.com/graphql-compose/graphql-compose-examples/blob/master/examples/northwind/models/product.js#L38,L49
const addEntityNameRegexpSearch = function () {
    const extendedResolver = EntityTC.getResolver('findMany').addFilterArg({
        name: 'nameRegexp',
        type: 'String',
        description: 'Search by name in regExp',
        query: (query, value, resolveParams) => { // eslint-disable-line
            query.name = new RegExp(value, 'i'); // eslint-disable-line
        },
    });
    extendedResolver.name = 'findMany';
    EntityTC.addResolver(extendedResolver);
};
addEntityNameRegexpSearch()

schemaComposer.rootQuery().addFields({
    entityById: EntityTC.getResolver('findById'),
    entityByIds: EntityTC.getResolver('findByIds'),
    entityOne: EntityTC.getResolver('findOne'),
    entityMany: EntityTC.getResolver('findMany'),
    entityCount: EntityTC.getResolver('count'),
    entityConnection: EntityTC.getResolver('connection'),
    entityPagination: EntityTC.getResolver('pagination'),
});

schemaComposer.rootMutation().addFields({
    entityCreate: EntityTC.getResolver('createOne'),
    entityUpdateById: EntityTC.getResolver('updateById'),
    entityUpdateOne: EntityTC.getResolver('updateOne'),
    entityUpdateMany: EntityTC.getResolver('updateMany'),
    entityRemoveById: EntityTC.getResolver('removeById'),
    entityRemoveOne: EntityTC.getResolver('removeOne'),
    entityRemoveMany: EntityTC.getResolver('removeMany'),
});

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

I modified the query to correspond to the method params:

const queries = {
    SHOP_LIST: gql `
                                    query entityMany($name: String!){
                                        entityMany(filter: { nameRegexp: $name }, limit: 3){name}
                                    }
                                `
}

And I use it the same way:

<Query query={queries.SHOP_LIST} variables={{name:this.state.name}}>

Upvotes: 1

Related Questions