Skip to content

Commit

Permalink
feat: Implement GraphQL adapter for Apollo Server (#333)
Browse files Browse the repository at this point in the history
* feat: Implement Apollo Server GraphQL adapter

* fix: Fix the query input type
  • Loading branch information
nadeesha authored Dec 18, 2024
1 parent f825a8f commit 6befe89
Show file tree
Hide file tree
Showing 11 changed files with 2,226 additions and 0 deletions.
23 changes: 23 additions & 0 deletions adapters/graphql-adapter/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Dependencies
node_modules/

# Build output
dist/

# Environment variables
.env
.env.*

# IDE files
.vscode/
.idea/

# Logs
*.log
npm-debug.log*

# Coverage directory
coverage/

# OS files
.DS_Store
10 changes: 10 additions & 0 deletions adapters/graphql-adapter/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
src/
tests/
.eslintrc.json
.gitignore
tsconfig.json
jest.config.js
*.log
.DS_Store
coverage/
.env
104 changes: 104 additions & 0 deletions adapters/graphql-adapter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<p align="center">
<img src="https://a.inferable.ai/logo-hex.png" width="200" style="border-radius: 10px" />
</p>

# Apollo Server Adapter for Inferable

![NPM Version](https://img.shields.io/npm/v/%40inferable%2Fapollo-server-adapter?color=32CD32)
[![Documentation](https://img.shields.io/badge/docs-inferable.ai-brightgreen)](https://docs.inferable.ai/)
![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)

The Inferable Apollo Server Adapter allows you to expose your existing GraphQL API as Inferable functions. This enables AI agents to interact with your GraphQL API while preserving all your existing middleware and type safety.

## Installation

### npm

```bash
npm install @inferable/apollo-server-adapter
```

### yarn

```bash
yarn add @inferable/apollo-server-adapter
```

### pnpm

```bash
pnpm add @inferable/apollo-server-adapter
```

## Quick Start

Add the Inferable adapter to your Apollo Server:

```ts
import { ApolloServer } from "@apollo/server";
import { inferableAdapter } from "@inferable/apollo-server-adapter";
import { Inferable } from "inferable";

const server = new ApolloServer<InferableGraphQLContext>({
typeDefs,
resolvers,
});

// Add the Inferable adapter
server.addPlugin(
inferableAdapter({
inferableClient: new Inferable({
endpoint: process.env.INFERABLE_URL,
apiSecret: process.env.INFERABLE_API_SECRET,
}),
apolloServer: server,
})
);
```

Your GraphQL queries and mutations are now available as Inferable functions!

```ts
const result = await client.run({
initialPrompt: "Get the user with id 1",
resultSchema: z.object({
id: z.string(),
name: z.string(),
email: z.string(),
}),
});
```

## Technical Details

The adapter automatically:

1. Introspects your GraphQL schema to generate JSON Schema definitions for all queries and mutations
2. Creates Inferable functions for each query and mutation
3. Handles argument passing and result formatting
4. Provides type safety through the InferableGraphQLContext

The adapter supports:

- All GraphQL scalar types
- Complex input types
- Nested queries and mutations
- Custom context passing
- [Human in the loop](https://docs.inferable.ai/pages/human-in-the-loop) workflows
- [Custom authentication](https://docs.inferable.ai/pages/custom-auth)

## Documentation

[Inferable documentation](https://docs.inferable.ai) contains all the information you need to get started with Inferable.

## Limitations

- `approvalRequest()` currently has problems with the runtime schema validation. This will be fixed with [#332](https://github.com/inferablehq/inferable/issues/332).

## Support

For support or questions, please create an issue in the repository.

## Contributing

Contributions to the Inferable Apollo Server Adapter are welcome. Please ensure that your code adheres to the existing style and includes appropriate tests.
12 changes: 12 additions & 0 deletions adapters/graphql-adapter/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import globals from "globals";
import pluginJs from "@eslint/js";
import tseslint from "typescript-eslint";


/** @type {import('eslint').Linter.Config[]} */
export default [
{files: ["**/*.{js,mjs,cjs,ts}"]},
{languageOptions: { globals: globals.browser }},
pluginJs.configs.recommended,
...tseslint.configs.recommended,
];
28 changes: 28 additions & 0 deletions adapters/graphql-adapter/eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"parser": "@typescript-eslint/parser",
"plugins": [
"@typescript-eslint"
],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended"
],
"env": {
"node": true,
"es6": true
},
"parserOptions": {
"ecmaVersion": 2020,
"sourceType": "module"
},
"rules": {
"@typescript-eslint/explicit-function-return-type": "warn",
"@typescript-eslint/no-explicit-any": "warn",
"@typescript-eslint/no-unused-vars": [
"error",
{
"argsIgnorePattern": "^_"
}
]
}
}
7 changes: 7 additions & 0 deletions adapters/graphql-adapter/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
roots: ["<rootDir>/src"],
testMatch: ["**/*.test.ts"],
collectCoverageFrom: ["src/**/*.ts", "!src/**/*.d.ts"],
};
Loading

0 comments on commit 6befe89

Please sign in to comment.