Provide mutation example
I'm excited to see support for hooking additional resolvers to postgraphql. I tried the query example it works fine. I can't figure out how the mutation works.. Can you provide an example?
Thanks!
I think I have it, maybe?
exports.recoverPassword = function (
builder
) {
builder.hook("GraphQLObjectType:fields", (fields, { extend }, { scope: { isRootMutation }, fieldWithHooks }) => {
if (!isRootMutation) {
return fields;
}
return extend(fields, {
recoverPassword: {
type: GraphQLString,
args: {
email: {
type: GraphQLString,
},
},
resolve(_, { email }) {
console.log("email ", email);
return knex("nextweb.user_private").where("email",email.trim())
.then((e) => {
if (e.length > 0) {
return "Recovery email sent"
} else {
return "email not found"
}
})
.catch((e) => {
return "email not found"
});
},
},
});
});
}
I'm so pleased you've started to use this! Please be aware that I may be changing the API slightly (mostly just renaming things, I think) before it's properly released - reserving that right whilst it's still an alpha...
Your example looks pretty good to me; there's a couple things I want to mention just FYI for future plugins:
1/ If you add the field by wrapping it with fieldWithHooks, i.e.
return extend(fields, {
recoverPassword: fieldWithHooks('recoverPassword', (stuff) => {
type: GraphQLString,
...
then you get access to "stuff" which is a bunch of field related helpers - particularly important if you want your field to influence fields higher up in the tree (e.g. if it needs to be selected from the table in SQL that would be your chance to inform the system of that fact). The system will automatically call the hooks whichever way you do it (though there's possibly a bug at the moment where the hooks are called twice in some circumstances, see: #11).
2/ You're using global graphql; this is now discouraged - it's better to use the graphql entry on the build object, i.e. { extend, graphql }, because it avoids a graphql version conflict. That said, I don't think I've actually released the version that has graphql on that object yet, so just something to remember will be changing in future.
I'd also advise that you consider using the built in SQL generators/querying, which would be something like:
async resolve(_, { email }, { pgClient }) {
const query = sql.query`select * from next_web.user_private where email = ${sql.value(email.trim())}`;
const {text, values} = sql.compile(query);
try {
const { rows: [userPrivate] } = await pgClient.query(text, values);
if (userPrivate) {
// Do something with userPrivate
return "Recovery email sent";
} else {
return "email not found";
}
} catch (e) {
return "error occurred";
}
}
sql is available as pgSql on the build object: { extend, pgSql: sql }
Thanks @benjie. There was no clean way around adding other graphQL api and it seems your solution is worth adopting early. I used the injected pgClient as you suggested. It works great.
Awesome! Track https://github.com/postgraphql/postgraphql/pull/506 for breaking changes, I'm still making some.
[semi-automated message] To keep things manageable I'm going to close this issue as I think it's solved; but if not or you require further help please re-open it.