Thursday, 25 May 2017

GraphQL - passing an object of non specific objects as an argument

I am very new to GraphQL. I'm trying to pass an object like this one as an argument:

{
  filters: {
    status: 'approved',
    id: {
      LESS_THAN: 200 
    }
  }
}

Or this object can be like this either;

{
  filters: {
    status: ['approved', 'pending'],
    id: 200
  }
}

I tried to define it like this but it obviously didn't work:

args: {
    filters: { type: new GraphQLNonNull(new GraphQLNonNull(GraphQLString)) },
},

The GraphQLNonNull don't work for that too.

This is my Query definition:

const QueryType = new GraphQLObjectType({
  name: 'Query',
  description: '...',
  fields: () => ({
    offers: {
      type: OffersType,
      args: {
        limit: { type: GraphQLInt },
        page: { type: GraphQLInt },
        sort: { type: GraphQLString },
        filters: { [HERE] }
      },
      resolve: (root, args, context, info) => {
        const gqlFields = graphqlFields(info);
        const fields = Object.keys(gqlFields.offer);
        const queryArgs = args;

        queryArgs.fields = fields;

        return getOffers(queryArgs);
      }
    },
  }),
});

And this is my request with superagent

const getOffers = (args) => {
  const queryArgs = args;

  if (typeof queryArgs.limit !== 'undefined') {
    queryArgs.limit = args.limit;
  } else {
    queryArgs.limit = Number.MAX_SAFE_INTEGER;
  }

  return new Promise((fulfill, reject) => {
    request
      .get(API_URL)
      .query(qs.stringify(args))
      .end((err, res) => {
        if (err) {
          reject(err);
        }

        fulfill(res);
      });
  });
};

I need this object to construct a query in my resolve function. Thank you all for your help! I only need simple advices!



via Mike Boutin

No comments:

Post a Comment