I am trying to sync my users between Firebase and Hasura. My Firebase function doesn't throw any errors, however, it is not updating Hasura. Can anyone explain to me the part that I am missing?
My firebase function:
const functions = require("firebase-functions");
const admin = require("firebase-admin");
const { request, gql, GraphQLClient } = require('graphql-request')
const config = functions.config();
const endpoint = config.hasura.url;
const adminSecret = config.hasura.admin;
const graphQLClient = new GraphQLClient(endpoint, {
headers: {
"content-type": "application/json",
"x-hasura-admin-secret": adminSecret
}
});
admin.initializeApp(functions.config().firebase);
exports.createUser = functions.auth.user().onCreate(async user => {
const { uid: id, email } = user;
const mutation = gql`
mutation($id: String!, $email: String) {
insert_users_one(object: { user_firebase_id: $id, user_email: $email }) {
user_firebase_id
user_email
}
}
`;
try {
const data = await graphQLClient.request(mutation, {
id,
email,
});
return data;
} catch (e) {
console.log(e);
}
});
The logs return
Function execution took 521 ms, finished with status: 'ok'
createUser
}
The string literal created a new line ( \n
) a stated a comment @DIGI Byte
.
There are other ways but to keep it simple I changed structure of this
const mutation = gql`
mutation($id: String!, $email: String) {
insert_users_one(object: { user_firebase_id: $id, user_email: $email }) {
user_firebase_id
user_email
}
}
`;
to
const mutation = gql`mutation($id: String!, $email: String)\{insert_users(objects: [{user_firebase_id: $id,user_email: $email}]) {affected_rows}}`;
I know there has to be a more efficient way of fixing this, but if anyone else runs into a similar issue. I'll leave this here.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.