Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Federated Authentication Setup with GraphQL
📖 Scenario: You are building a GraphQL API that supports federated authentication. This means users can log in using different identity providers like Google or Facebook. Your API needs to store user data and their authentication provider information in a database.
🎯 Goal: Create a simple GraphQL schema and resolver setup that stores user information along with their authentication provider. You will build the data structure, add a configuration for supported providers, implement the core logic to link users with providers, and complete the setup with a query to fetch user details.
📋 What You'll Learn
Create a GraphQL type User with fields id, name, and authProvider
Create a list called users to store user data
Add a configuration list supportedProviders with 'Google' and 'Facebook'
Implement a mutation addUser that adds a user only if the authProvider is supported
Implement a query getUser to fetch user details by id
💡 Why This Matters
🌍 Real World
Federated authentication is common in apps that allow users to log in using third-party accounts like Google or Facebook. This project simulates storing and managing such user data in a GraphQL API.
💼 Career
Understanding how to handle federated authentication and manage user data is essential for backend developers working on modern web applications with social login features.
Progress0 / 4 steps
1
Define User type and initial users list
Create a GraphQL type called User with fields id (ID!), name (String!), and authProvider (String!). Then create a list called users with two users: one with id: "1", name: "Alice", authProvider: "Google" and another with id: "2", name: "Bob", authProvider: "Facebook".
GraphQL
Hint
Define the User type with the three fields exactly as named. Then create the users list with two user objects matching the given details.
Create a constant list called supportedProviders containing the strings "Google" and "Facebook".
GraphQL
Hint
Define the supportedProviders list exactly with the two provider names as strings.
3
Implement addUser mutation with provider check
Add a mutation resolver function called addUser that takes id, name, and authProvider as arguments. It should add a new user to the users list only if authProvider is included in supportedProviders. Use supportedProviders.includes(authProvider) to check support.
GraphQL
Hint
Inside the addUser mutation, check if authProvider is in supportedProviders. If yes, create a new user object and add it to users. Return the new user. Otherwise, return null.
4
Add getUser query to fetch user by id
Add a query resolver function called getUser that takes id as an argument and returns the user object from users with the matching id. Use users.find(user => user.id === id) to find the user.
GraphQL
Hint
In the Query resolver, add getUser that returns the user matching the given id from the users list.
Practice
(1/5)
1. What is the main benefit of federated authentication in GraphQL applications?
easy
A. It allows anonymous access without any login.
B. It stores all user passwords securely in the GraphQL server.
C. Users can sign in using trusted external accounts without managing passwords.
D. It requires users to create new passwords for each service.
Hint: Token user data matches currentUser fields returned [OK]
Common Mistakes:
Expecting null or error despite valid token
Confusing error response with data
Assuming fields return null values
4. A developer tries to use federated authentication but gets an "Unauthorized" error. Which fix will most likely solve the problem?
medium
A. Add the token in the request header as "Authorization: Bearer <token>".
B. Remove the token from the request to allow anonymous access.
C. Change the query to request only public fields.
D. Use a different GraphQL query without authentication.
Solution
Step 1: Identify cause of Unauthorized error
Unauthorized usually means missing or invalid authentication token in the request.
Step 2: Apply correct token header format
Adding the token properly as "Authorization: Bearer <token>" header will authenticate the user and fix the error.
Final Answer:
Add the token in the request header as "Authorization: Bearer <token>". -> Option A
Quick Check:
Unauthorized error = missing or wrong token header [OK]
Hint: Always send token in Authorization header [OK]
Common Mistakes:
Removing token expecting anonymous access
Changing query without fixing auth
Using wrong header names or formats
5. You want to implement federated authentication in a GraphQL API that supports multiple identity providers (Google, Facebook, GitHub). Which approach best handles user identity across these providers?
hard
A. Map external provider user IDs to a single internal user ID in your database.
B. Create separate user records for each provider's user ID without linking.
C. Require users to manually link accounts after login.
D. Ignore provider IDs and use only email addresses to identify users.