0
0
GraphQLquery~30 mins

Schema visibility control in GraphQL - Mini Project: Build & Apply

Choose your learning style9 modes available
Schema Visibility Control in GraphQL
📖 Scenario: You are building a GraphQL API for a company that wants to control which parts of the schema are visible to different users. This helps keep sensitive data hidden from unauthorized users.
🎯 Goal: Build a GraphQL schema with fields that can be hidden or shown based on a visibility setting.
📋 What You'll Learn
Create a GraphQL type with multiple fields
Add a visibility configuration variable
Use a resolver function that checks the visibility setting
Complete the schema with a query that respects visibility control
💡 Why This Matters
🌍 Real World
Many APIs need to hide sensitive data from unauthorized users. Controlling schema visibility helps protect data and customize API responses.
💼 Career
Understanding schema visibility control is important for backend developers and API designers to build secure and flexible GraphQL APIs.
Progress0 / 4 steps
1
Create the GraphQL type with fields
Create a GraphQL type called User with fields id (ID!), name (String!), and email (String!).
GraphQL
Hint

Use the type keyword to define a GraphQL type and specify the fields with their types.

2
Add a visibility configuration variable
Create a variable called showEmail and set it to true to control if the email field is visible.
GraphQL
Hint

Use const to declare a constant variable in JavaScript that controls visibility.

3
Add resolver logic to control field visibility
Write a resolver function for the email field that returns the email only if showEmail is true, otherwise returns null.
GraphQL
Hint

Use a ternary operator to check showEmail and return the email or null accordingly.

4
Complete the schema with a query type
Add a Query type with a field user that returns a User. Also add a resolver for user that returns a user object with id, name, and email.
GraphQL
Hint

Define the Query type and add a resolver that returns a user object with the required fields.