How to define and execute GraphQL query with filter -
so thing try is, retrieve filtered data database (mongodb in situation) using graphql.
speaking in "mysql language" how implement where
clause in graphql?
i followed tutorial : https://learngraphql.com/basics/using-a-real-data-source/5
query filter defined :
const query = new graphqlobjecttype({ name: "queries", fields: { authors: { type: new graphqllist(author), resolve: function(rootvalue, args, info) { let fields = {}; let fieldasts = info.fieldasts; fieldasts[0].selectionset.selections.map(function(selection) { fields[selection.name.value] = 1; }); return db.authors.find({}, fields).toarray(); } } } });
the tricky part here info
parameter in resolve
function. explanations i've found here : http://pcarion.com/2015/09/26/graphql-resolve/
so ast (abstract syntax tree)
can please provide basic real-life example code show how define , execute following query : get authors name == john
thank you!
there no need examine ast. awfully laborious.
all need define argument on author
field. second paramenter of resolver, can check arguement , include in mongo query.
const query = new graphqlobjecttype({ name: "queries", fields: { authors: { type: new graphqllist(author), // define arguments , types here args: { name: { type: graphqlstring } }, resolve: function(rootvalue, args, info) { let fields = {}; // , can check arguments' values if (args.name) { fields.name = args.name } // , use when constructing query return db.authors.find(fields, fields).toarray(); } } } });
Comments
Post a Comment