### Create Deep Partial Schema with Effect-TS Source: https://context7.com/amar4enko/effect-graphql/llms.txt Recursively transforms a schema into a deep partial version where all fields become optional, including nested objects, using the `deepPartial` function from `effect-gql-schema`. This is used internally for GraphQL output types to support field selection, allowing clients to query only the fields they need. The example demonstrates creating a partial schema for a `User` with a nested `Address`. ```typescript import { Schema } from '@effect/schema' import { deepPartial } from 'effect-gql-schema' // Define a nested schema const Address = Schema.Struct({ street: Schema.String, city: Schema.String, zipCode: Schema.String, }) const User = Schema.Struct({ id: Schema.Number, name: Schema.String, email: Schema.String, address: Address, tags: Schema.Array(Schema.String), }) // Create deep partial version const PartialUser = deepPartial(User) // All fields are now optional, including nested ones type PartialUserType = Schema.Schema.Type // { // id?: number | undefined // name?: string | undefined // email?: string | undefined // address?: { // street?: string | undefined // city?: string | undefined // zipCode?: string | undefined // } | undefined // tags?: string[] | undefined // } // Valid partial instances const partial1: PartialUserType = { id: 1 } const partial2: PartialUserType = { name: "Alice", address: { city: "NYC" } } const partial3: PartialUserType = {} ``` -------------------------------- ### Define Field Resolvers for GraphQL Types with Effect-TS Source: https://context7.com/amar4enko/effect-graphql/llms.txt Creates field resolvers for specific fields on a type, enabling computed or fetched fields that require additional logic beyond simple property access. Field resolvers receive the parent object and execute effects to resolve field values. This example defines an `Identifiable` interface and registers a `getId` resolver for the `id` field. ```typescript import { Schema } from '@effect/schema' import { pipe } from 'effect' import { empty, Interface, Object, Operation, query, resolveField } from 'effect-gql-schema' // Define an interface with a field that needs resolution const Identifiable = Schema.Struct({ id: Schema.Number, }).pipe( Interface.asInterface(`Identifiable`) ) // Define types implementing the interface class User extends Schema.TaggedClass()(`User`, { name: Schema.String, }).pipe( Object.extendsInterface(Identifiable) ) {} class Post extends Schema.TaggedClass()(`Post`, { title: Schema.String, }).pipe( Object.extendsInterface(Identifiable) ) {} // Define a field resolver operation class GetId extends Operation()( `getId`, Schema.Never, Schema.Number, { parent: Identifiable // Parent type } ) {} // Register the field resolver const schema = pipe( empty(), query({ user: GetUser, post: GetPost, }), resolveField(Identifiable, { id: GetId // Resolve 'id' field for all Identifiable types }) ) // The 'id' field will be resolved dynamically for User and Post // instead of being accessed as a simple property ``` -------------------------------- ### Add Query Operations to GraphQL Schema with Effect-TS Source: https://context7.com/amar4enko/effect-graphql/llms.txt Registers query operations in the GraphQL schema, mapping operation names to their corresponding request classes using `effect-gql-schema`. It validates that operation names are unique and do not conflict with existing queries. This example defines User and Post types and then builds a schema with 'currentUser' and 'userPosts' queries. ```typescript import { Schema } from '@effect/schema' import { Effect, pipe } from 'effect' import { empty, Operation, query } from 'effect-gql-schema' // Define return types class User extends Schema.TaggedClass()(`User`, { id: Schema.String, name: Schema.String, }) {} class Post extends Schema.TaggedClass()(`Post`, { id: Schema.String, title: Schema.String, content: Schema.String, }) {} // Define query operations class GetCurrentUser extends Operation()( `getCurrentUser`, Schema.Never, Schema.Union(User, Schema.Undefined), {} ) {} class GetUserPosts extends Operation()( `getUserPosts`, Schema.String, // Error type Schema.Array(Post), { userId: Schema.String } ) {} // Build the schema with multiple queries const schema = pipe( empty(), query({ currentUser: GetCurrentUser, userPosts: GetUserPosts, }) ) // Resulting GraphQL schema: // type Query { // currentUser: User // userPosts(userId: String!): [Post!]! // } ``` -------------------------------- ### Compile Schema to GraphQL Schema with Effect Source: https://context7.com/amar4enko/effect-graphql/llms.txt Transforms an effect-gql-schema definition into a GraphQL schema. It processes types, interfaces, queries, mutations, and resolvers. The output is an Effect that produces a GraphQLSchema, suitable for use with GraphQL servers. Dependencies include '@effect/schema', 'effect', 'graphql', and 'effect-gql-schema'. ```typescript import { Schema } from '@effect/schema' import { Effect, Logger, LogLevel, pipe } from 'effect' import { printSchema } from 'graphql' import { compile, empty, GqlSchema, Interface, Object, Operation, query, resolveField } from 'effect-gql-schema' // Define schemas const Identifiable = Schema.Struct({ id: Schema.Number, }).pipe(Interface.asInterface(`Identifiable`)) class User extends Schema.TaggedClass()(`User`, { name: Schema.String, }).pipe(Object.extendsInterface(Identifiable)) {} class Post extends Schema.TaggedClass()(`Post`, { content: Schema.String, }).pipe(Object.extendsInterface(Identifiable)) {} // Define operations class GetCurrentUser extends Operation()( `getCurrentUser`, Schema.Number, Schema.Union(User, Schema.Undefined), {} ) {} class GetUserPosts extends Operation()( `userPosts`, Schema.Number, Schema.Array(Post), {} ) {} // Build and compile schema const program = pipe( empty(), query({ currentUser: GetCurrentUser, myPosts: GetUserPosts, }), resolveField(Identifiable, { id: GetId }), (source) => compile.pipe( Effect.provideService(GqlSchema, source), Logger.withMinimumLogLevel(LogLevel.Info), ) ) // Execute to get GraphQL schema Effect.runPromise(program).then(schema => { console.log(printSchema(schema)) // Output: // type Query { // currentUser: User // myPosts: [Post!]! // } // // type User implements Identifiable { // name: String! // id: Float! // } // // interface Identifiable { // id: Float! // } }) ``` -------------------------------- ### Operation - Define GraphQL Query/Mutation Operations Source: https://context7.com/amar4enko/effect-graphql/llms.txt Defines a tagged request class for GraphQL operations (queries or mutations). It automatically generates the corresponding GraphQL operation and handles input parameters and types. ```APIDOC ## POST /graphql (or relevant endpoint) ### Description Creates a tagged request class representing a GraphQL operation (query or mutation). Operations are defined with a failure type, success type, and input fields, automatically generating the corresponding GraphQL operation with deep partial output types for field selection. ### Method POST ### Endpoint /graphql (or your GraphQL endpoint) ### Parameters #### Query Parameters None (typically sent in the request body) #### Request Body - **operationName** (string) - Optional - The name of the operation to execute. - **query** (string) - Required - The GraphQL query string. - **variables** (object) - Optional - A map of variables for the query. ### Request Example ```json { "query": "query GetUser($id: String!, $includeEmail: Boolean) { getUser(id: $id, includeEmail: $includeEmail) { name email } }", "variables": { "id": "user-123", "includeEmail": true } } ``` ### Response #### Success Response (200) - **data** (object) - Contains the result of the GraphQL operation. - **getUser** (object) - The result of the getUser operation. - **name** (string) - The name of the user. - **email** (string) - The email of the user. #### Response Example ```json { "data": { "getUser": { "name": "Alice", "email": "alice@example.com" } } } ``` ``` -------------------------------- ### Define GraphQL Query/Mutation Operations with effect-gql-schema Source: https://context7.com/amar4enko/effect-graphql/llms.txt Defines a tagged request class for GraphQL operations (queries/mutations). It specifies failure and success types, along with input fields. The library automatically generates the corresponding GraphQL operation, including deep partial output types for field selection. This ensures type-safe operations and input validation. ```typescript import { Schema } from '@effect/schema' import { Operation } from 'effect-gql-schema' // Define a User type class User extends Schema.TaggedClass()(`User`, { name: Schema.String, email: Schema.String, age: Schema.Number, }) {} // Define a query operation with input parameters class GetUser extends Operation()( `getUser`, Schema.Number, // Failure type (error code) User, // Success type { id: Schema.String, // Input fields includeEmail: Schema.optional(Schema.Boolean), }, ) {} // Create an instance and execute const getUserRequest = new GetUser({ id: "user-123", includeEmail: true }) // The operation has type-safe fields type Fields = typeof getUserRequest.id // string type Result = typeof getUserRequest._tag // "getUser" ``` -------------------------------- ### exposeFields - Expose Object Fields for GraphQL Source: https://context7.com/amar4enko/effect-graphql/llms.txt Marks specific fields of a class or struct to be exposed in the GraphQL schema. This creates field resolvers that extract values from the parent object, controlling which fields are queryable. ```APIDOC ## Schema Definition Enhancement ### Description The `Object.exposeFields` function is used to selectively expose fields from a TypeScript class or struct for use in a GraphQL schema. Fields not explicitly exposed are not queryable via the GraphQL API, even if they exist on the TypeScript type. This is crucial for security and API design, allowing you to hide internal implementation details. ### Method N/A (This is a schema definition construct, not an HTTP method) ### Endpoint N/A ### Parameters N/A ### Request Example (This is a schema definition, not a request) ```typescript import { Schema } from '@effect/schema' import { Object } from 'effect-gql-schema' class User extends Schema.TaggedClass()( `User`, { name: Schema.String, email: Schema.String, password: Schema.String, // Internal field }, ).pipe( Object.exposeFields([`name`, `email`]) ) {} const user = new User({ name: "Bob", email: "bob@example.com", password: "secret123", }) // 'password' is accessible in TypeScript but not in GraphQL console.log(user.password) // "secret123" ``` ### Response #### Success Response (N/A) N/A #### Response Example (Schema definition, not a response) ```graphql type User { name: String! email: String! } ``` ``` -------------------------------- ### Expose Object Fields for GraphQL with effect-gql-schema Source: https://context7.com/amar4enko/effect-graphql/llms.txt Marks specific fields of a class or struct for exposure in the GraphQL schema, creating field resolvers that extract values from the parent object. Only exposed fields are queryable in the GraphQL API. The `Object.exposeFields` function from `effect-gql-schema` is used to achieve this. ```typescript import { Schema } from '@effect/schema' import { Object } from 'effect-gql-schema' // Define a class with multiple fields class User extends Schema.TaggedClass()( `User`, { name: Schema.String, email: Schema.String, password: Schema.String, internalId: Schema.Number, }, ).pipe( // Only expose safe fields to GraphQL API Object.exposeFields([`name`, `email`]), ) {} // The password and internalId fields exist in TypeScript // but won't be queryable in the GraphQL schema const user = new User({ name: "Bob", email: "bob@example.com", password: "secret123", internalId: 42, }) // All fields available in TypeScript console.log(user.password) // "secret123" console.log(user.internalId) // 42 // But GraphQL schema will only include: // type User { // name: String! // email: String! // } ``` -------------------------------- ### extendsInterface - Add GraphQL Interfaces to Classes Source: https://context7.com/amar4enko/effect-graphql/llms.txt Extends a Schema TaggedClass to implement one or more GraphQL interfaces. This adds interface fields to the class and maintains type information for schema generation, enabling polymorphism. ```APIDOC ## Schema Definition Enhancement ### Description This functionality allows augmenting existing Effect Schema classes with GraphQL interface definitions. By using `Interface.asInterface` and `Object.extendsInterface`, you can ensure that your classes correctly implement the fields and types defined in GraphQL interfaces, leading to a more robust and well-defined schema. ### Method N/A (This is a schema definition construct, not an HTTP method) ### Endpoint N/A ### Parameters N/A ### Request Example (This is a schema definition, not a request) ```typescript import { Schema } from '@effect/schema' import { Interface, Object } from 'effect-gql-schema' // Define an interface schema const Identifiable = Schema.Struct({ id: Schema.Number, }).pipe( Interface.asInterface(`Identifiable`) ) // Create a class that implements the interface class User extends Schema.TaggedClass()( `User`, { name: Schema.String, email: Schema.String, }, ).pipe( Object.extendsInterface(Identifiable) ) {} const user = new User({ name: "Alice", email: "alice@example.com", id: 1, }) console.log(user.id) // 1 ``` ### Response #### Success Response (N/A) N/A #### Response Example (Schema definition, not a response) ```graphql interface Identifiable { id: Float! } type User implements Identifiable { id: Float! name: String! email: String! } ``` ``` -------------------------------- ### Add GraphQL Interfaces to Classes with effect-gql-schema Source: https://context7.com/amar4enko/effect-graphql/llms.txt Extends a Schema TaggedClass to implement GraphQL interfaces. This adds interface fields to the class and preserves type information for schema generation, enabling polymorphism and shared field definitions. The `extendsInterface` function from `effect-gql-schema` is used for this purpose. ```typescript import { Schema } from '@effect/schema' import { Interface, Object } from 'effect-gql-schema' // Define an interface schema const Identifiable = Schema.Struct({ id: Schema.Number, }).pipe( Interface.asInterface(`Identifiable`) ) const Timestamps = Schema.Struct({ createdAt: Schema.DateFromSelf, updatedAt: Schema.DateFromSelf, }).pipe( Interface.asInterface(`Timestamps`) ) // Create a class that implements multiple interfaces class User extends Schema.TaggedClass()( `User`, { name: Schema.String, email: Schema.String, }, ).pipe( Object.extendsInterface(Identifiable), Object.extendsInterface(Timestamps), ) {} // Instantiate with all required fields const user = new User({ name: "Alice", email: "alice@example.com", id: 1, createdAt: new Date("2024-01-01"), updatedAt: new Date("2024-01-15"), }) console.log(user.id) // 1 (from Identifiable) console.log(user.createdAt) // Date object (from Timestamps) console.log(user.name) // "Alice" (from User) ``` -------------------------------- ### Use GraphQL Scalar Types for ID and Float with Effect Schema Source: https://context7.com/amar4enko/effect-graphql/llms.txt Defines branded Effect Schema types for GraphQL ID and Float scalars, mapping them semantically to string and number types. This ensures correct GraphQL scalar type mapping in the generated schema. Dependencies include '@effect/schema' and 'effect-gql-schema/scalars'. ```typescript import { Schema } from '@effect/schema' import { ID, Float } from 'effect-gql-schema/scalars' // Define a schema using GraphQL scalar types class Product extends Schema.TaggedClass()( `Product`, { id: ID, // Maps to GraphQL ID scalar (string-based) name: Schema.String, price: Float, // Maps to GraphQL Float scalar quantity: Schema.Number, // Maps to GraphQL Float (generic number) } ) // Create instances with proper types const product = new Product({ id: "prod-123", // ID requires string name: "Widget", price: 29.99, // Float is a number quantity: 100, }) // Type information is preserved const productId: string & Schema.Brand<"GqlID"> = product.id const productPrice: number & Schema.Brand<"GqlFloat"> = product.price // GraphQL schema output: // type Product { // id: ID! // name: String! // price: Float! // quantity: Float! // } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.