DEV Community

RJ-1998
RJ-1998

Posted on

Graphql Enthusiasts! I need your help🙏

I've been trying to get some help from #help section but no one came up with a solution. Graphql Enthusiasts I need your help🙏.

I want to create a simple Login/Logout functionality using reactjs and graphql. I followed some YouTube videos and some blogs on other websites and all of them end up with creating a separate auth.js file and resolver.js file which is completely different from the context of my project.

Here's my schema file


I want to perform a login mutation just like addPost mutation and addUser mutation, I don't know how to do it. I think there is something to do with resolve method but I'm unable to figure out.

Here's my Login react component


I want to receive a confirmation here that if the user is able logged in or not and if he is then he should remain logged in until he clicks the logout button.

Guys please help me, although it's not a big deal to create a login/logout feature but this graphql thing looks hard to conceive for me.

I hope someone will come up with a good solution.

Top comments (1)

Collapse
 
zspencer profile image
Zee • Edited

Hey RJ!

My recommendation for login/logout is that the Login mutation returns a Session object. That Session object includes an access token that the consuming app may use to make further requests.

I've created an authenticate mutation and a register mutation in Rails.

Each of them return a UserSession payload object.

On each GraphQL request, I query the database to find a user with the access token provided via the HTTP Headers; and add it to the context so that the GraphQL Resolvers may use it later.

From there, each resolver can make decisions about how they should behave

I apologize that this isn't in node; but most of my API work is done using Rails for the backend since it has such rich tooling for database schema management and providing vectors for customer success teams / clients to administer the data. That's not to say you should totally use Rails for whatever you're doing; as it looks like Yoga/Apollo-Server are some damn-fine piees of software. I'm just not as familiar with them and don't have any open source code I can point to for how to do registration and authentication in them just yet.

Zee