example_graphql

package
v1.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 24, 2025 License: MIT Imports: 6 Imported by: 0

README

GraphQL Package Examples

This directory contains examples demonstrating how to use the graphql package, which provides utilities for integrating authentication and authorization with GraphQL in Go applications. The package offers functionality for securing GraphQL APIs, handling JWT tokens, and implementing authorization directives.

Examples

1. Auth Configuration Example

auth_configuration_example.go

Demonstrates how to configure the auth service for use with GraphQL.

Key concepts:

  • Setting up JWT configuration (secret key, issuer, token duration)
  • Configuring middleware to skip certain paths
  • Initializing the auth service for GraphQL
  • Understanding the auth service capabilities
2. Auth Middleware Example

auth_middleware_example.go

Shows how to integrate authentication middleware with GraphQL.

Key concepts:

  • Setting up auth middleware for GraphQL handlers
  • Protecting GraphQL endpoints
  • Handling authentication in GraphQL requests
  • Passing authentication context to resolvers
3. Directive Registration Example

directive_registration_example.go

Demonstrates how to register and use GraphQL directives for authorization.

Key concepts:

  • Creating custom GraphQL directives
  • Registering directives with the GraphQL schema
  • Using directives for field-level authorization
  • Implementing directive logic
4. JWT Token Generation Example

jwt_token_generation_example.go

Shows how to generate JWT tokens for GraphQL authentication.

Key concepts:

  • Creating JWT tokens with appropriate claims
  • Setting token expiration
  • Handling token generation errors
  • Using tokens for GraphQL authentication
5. Resolver Authorization Example

resolver_authorization_example.go

Demonstrates how to implement authorization checks in GraphQL resolvers.

Key concepts:

  • Accessing authentication context in resolvers
  • Performing permission checks
  • Handling unauthorized access
  • Implementing role-based access control

Running the Examples

To run any of the examples, use the go run command:

go run examples/graphql/auth_configuration_example.go

Additional Resources

For more information about the graphql package, see the graphql package documentation.

Documentation

Overview

Example demonstrating how to configure the auth service for GraphQL

Example demonstrating how to apply the auth middleware to a GraphQL handler

Example demonstrating how to register the @isAuthorized directive in a GraphQL server

Example demonstrating how to generate JWT tokens for testing GraphQL RBAC

Example demonstrating how to check authorization in a GraphQL resolver

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Item

type Item struct {
	ID    string
	Name  string
	Owner string
}

Mock Item type for demonstration

type ItemInput

type ItemInput struct {
	Name  string
	Owner string
}

Mock ItemInput type for demonstration

type Resolver

type Resolver struct {
	// contains filtered or unexported fields
}

Resolver is a simplified resolver for demonstration

func (*Resolver) CreateItem

func (r *Resolver) CreateItem(ctx context.Context, input ItemInput) (*Item, error)

CreateItem is a resolver method that demonstrates manual authorization checking

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL