-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is there a way to write API schemas in Vulcan Next? #139
Comments
Yep thank you for opening a ticket, it will be easier to track. Are you able to share code samples here so I can see how it could be structured? Also, do you think your API endpoint is tied to a specific model? Like your "Variable" collection? Or is it a totally independant endpoint? Because we could add |
Here's the original resolver, edited for brevity: calculateResult: async(obj, args, context, info) => {
const db = mongoose.connection;
const getVariable = async function(name) {
return await db.collection("variables").find({/* some filters*/, name: name}).toArray()[0];
}
let allRelevantVariables = await db.collection("variables").find({/* some filters */}).toArray();
const matchVariable = function(str) {
for (let i = 0; i < allRelevantVariables.length; i++){
const possibleVar = allRelevantVariables[i]["name"];
if (str.includes(possibleVar)) {
return allRelevantVariables[i];
}
}
return false;
}
const inputString = args.expression;
const percolateVariables = async function(str) {
let firstFoundVariable = matchVariable(str);
if (! firstFoundVariable) {
return str;
} else {
const fullFirstVarValue : String = String(await percolateVariables(firstFoundVariable["value"]));
const truncatedFirstVarValue = replaceAll(fullFirstVarValue, "=", "");
const substitutedStr = replaceAll(str, firstFoundVariable["name"], truncatedFirstVarValue);
return await percolateVariables(substitutedStr);
}
}
let fullString = await percolateVariables(inputString);
let assignedValue = /* Parse and evaluate fullString */;
db.collection("models").findOneAndUpdate({/* some filters */}, {$set: {/* misc fields */, score: assignedValue}});
return assignedValue;
}
},
}
}; |
As you can see, it uses the data in one collection, "variables", to update another collection, "models". |
Hi,
You can check how we seed data, in order to understand "mutators". They are the building blocks of Vulcan default resolvers, but not yet well exposed and they lack a "Read" counterpart. If you want to simplify this code, a first step would be to use the "Connector" from Vulcan. They add some sugar over Mongoose with generic operations. But you are not obligated to do so, I really think your code is fine. |
In particular: I'm trying to make an API function that takes in a string with variable names in it, e.g. "one + two", which should then be substituted in by the values of variables in those names in the variables collection (which should already have entries with name "one" and value 1, etc), and add to a different collection the evaluated result. I've written this and other functions in regular GraphQL, and would like to transfer them to Vulcan instead. Is this doable? For Vulcan Meteor, I looked at the page https://docs.vulcanjs.org/api-schemas.html , but apparently that's not how it works anymore. I asked in Slack, and it was suggested that I make an issue here.
The text was updated successfully, but these errors were encountered: