Skip to content

Instantly share code, notes, and snippets.

@catalinpit
Last active December 8, 2021 08:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save catalinpit/9e1b5a36f4d420034a20a304e008cac9 to your computer and use it in GitHub Desktop.
Save catalinpit/9e1b5a36f4d420034a20a304e008cac9 to your computer and use it in GitHub Desktop.
const REGISTRATION = `
mutation ($name: String!, $username: String!, $email: String!, $password: String!) {
insert_users_one(
object: {
name: $name,
username: $username,
email: $email,
password: $password
}
) { id }
}
`;
const execute = async (variables, reqHeaders) => {
const fetchResponse = await fetch(
"<your_GraphQL_endpoint>",
{
method: 'POST',
headers: {
...reqHeaders,
'x-hasura-access-key': process.env.HASURA_GRAPHQL_ADMIN_SECRET
} || {},
body: JSON.stringify({
query: REGISTRATION,
variables
})
}
);
const data = await fetchResponse.json();
return data;
};
// Request Handler
app.post('/register', async (req, res) => {
// get request input
const { name, username, email, password } = req.body.input;
// encrypt the password
const hashedPassword = bcrypt.hashSync(password, 10);
const { data, errors } = await execute({ name, username, email, password: hashedPassword });
if (errors) {
return res.status(400).json(errors[0])
}
return res.json({
...data.insert_users_one
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment