Reputation: 2337
I have integrated Apollo GraphQL with parse-server using cloud code. I would now like to expose authentication mutation (within the cloud code) which will accept facebook authData
and authenticate the user so it can return sessionToken
as a result.
The GraphQL is secondary here - it is just a context to explain why I need to do all this in cloud code (also I am using parse-server hosting provider and using cloud code is the only reasonable way to have the GraphQL working).
to show more details here is how the integration is done https://github.com/ciekawy/parse-server-back4app-graphql-boilerplate
and to not put just a link, app.js
in the cloud
folder looks like
var fs = require("fs");
var path = require("path");
var apollo_server_express = require("apollo-server-express");
var graphql_tools = require("graphql-tools");
var bodyParser = require("body-parser");
var cors = require("cors");
var resolvers = {
Query: {
hello() { return "Hello world!"; }
}
};
var schema = graphql_tools.makeExecutableSchema({
typeDefs: fs.readFileSync(path.join(__dirname, './graphql/schema.graphql'), 'utf8'),
resolvers: resolvers
});
app.use('/graphql', cors(), bodyParser.json(), apollo_server_express.graphqlExpress({ schema: schema }));
app.use('/graphiql', apollo_server_express.graphiqlExpress({
endpointURL: '/graphql'
}));
and so to the resolvers
I'd like to add
Mutation: {
authenticate(obj, args, context) {
// here call some parse-server link with
}
}
I tried to use linkWith
in different ways but with no success.
UPDATE: according to discussions on parse-server's github what I am trying to do may not be even supported. With better understanding of the internals I could open a ticket there.
Two possible workarounds I see (though I would not be happy with them)
just use REST for oauth signup/login (main drawback here is inability to provide atomic account initialization - i.e. create extra user data structures, the other is to not be able to migrate fully to GraphQL)
do the loopback REST call from cloud code GraphQL mutation to self
Upvotes: 0
Views: 743
Reputation: 2337
UPDATE: as for GraphQL in particular - parse-server since v3.6.0 introduced native GraphQL support for all parse-server API. Which means there is GraphQL auth mutation out of the box. Also since v3.7.2 it offers also support for custom schema mapped to cloud code/cloud functions.
Below workaround solution is still valid for generic cloud code.
ORIGINAL ANSWER:
As for now I ended up with doing internal http call from the cloud code to self - here it is if anyone would like to proceed in same situation.
async function authenticateWithFacebook({userProfile, ...authData}) {
try {
const response: HttpResponse = (await httpRequest({
url: 'https://local-parse-server/users/',
body: authData,
method: 'POST',
headers: {
'Content-Type': 'application/json', // without this line return error is about missing user or password
'X-Parse-Application-Id': APPLICATION_ID,
'X-Parse-REST-API-Key': REST_API_KEY,
'X-Parse-Revocable-Session': 1
}
}));
const { data: { sessionToken }} = response;
// here we can also do Parse.User.become(sessionToken)
return sessionToken;
} catch (e) {
console.error('error during authentication', e);
}
}
Sill would welcome solution without loopback http call.
Upvotes: 1