A comprehensive GraphQL API library for StackBlitz integration, providing a complete wrapper around the StackBlitz SDK with Apollo DataSource compatibility.
- π Complete wrapper around @stackblitz/sdk
- π Apollo DataSource implementation for GraphQL resolvers
- π Comprehensive TypeScript typings
- π Connection-based pagination with cursor support
- π§© Relay-compatible Node interface for global object identification
- π οΈ Configurable API client with authentication support
- π Well-documented API methods
npm install @graphql-api/stackblitz
# or
yarn add @graphql-api/stackblitz
# or
pnpm add @graphql-api/stackblitz
import { ApolloServer } from '@apollo/server';
import { StackBlitzDataSource } from '@graphql-api/stackblitz';
import { resolvers, typeDefs } from './schema';
// Create new Apollo Server with StackBlitz datasource
const server = new ApolloServer({
typeDefs,
resolvers,
});
// Provide DataSources in context
const { url } = await startStandaloneServer(server, {
context: async () => {
return {
dataSources: {
stackBlitzAPI: new StackBlitzDataSource({
apiKey: process.env.STACKBLITZ_API_KEY,
// Optional configuration
apiUrl: 'https://api.stackblitz.com',
timeout: 30000,
headers: {
'X-Custom-Header': 'value'
}
})
}
};
}
});
console.log(`π Server ready at ${url}`);
const resolvers = {
Query: {
project: (_, { id }, { dataSources }) => {
return dataSources.stackBlitzAPI.getProjectById(id);
},
projects: (_, { filters, pagination }, { dataSources }) => {
return dataSources.stackBlitzAPI.getProjects(filters, pagination);
}
},
Mutation: {
createProject: (_, { input }, { dataSources }) => {
return dataSources.stackBlitzAPI.createProject(input);
}
}
};
The library also provides direct access to StackBlitz SDK client methods:
import { StackBlitzDataSource } from '@graphql-api/stackblitz';
const stackblitz = new StackBlitzDataSource();
// Embed a StackBlitz project in an element
stackblitz.embedProject('elementId', {
title: 'My New Project',
description: 'A simple demo project',
files: {
'index.js': 'console.log("Hello, StackBlitz!");',
'index.html': '<div id="app"></div>'
},
template: 'javascript'
});
// Open a project in a new tab
stackblitz.openProject({
title: 'My Project',
files: {
'index.js': 'console.log("Hello!");',
},
template: 'javascript'
});
Main data source class that implements both REST API calls and client-side SDK functionality.
constructor(config?: StackBlitzConfig)
Configuration options:
apiUrl
: Base URL for the StackBlitz API (default: 'https://api.stackblitz.com')apiKey
: Your StackBlitz API key for authenticationtimeout
: Request timeout in milliseconds (default: 30000)headers
: Additional HTTP headers to include in requests
getProjects(filters?: ProjectFilters, pagination?: PaginationInput)
: Fetch projects with optional filtering and paginationgetProjectById(id: string)
: Fetch a single project by IDcreateProject(input: CreateProjectInput)
: Create a new projectupdateProject(id: string, input: UpdateProjectInput)
: Update an existing projectdeleteProject(id: string)
: Delete a projectforkProject(id: string, input?: UpdateProjectInput)
: Fork a project with optional modifications
getUsers(pagination?: PaginationInput)
: Fetch users with paginationgetUserById(id: string)
: Fetch a single user by IDgetUserProjects(userId: string, filters?: ProjectFilters, pagination?: PaginationInput)
: Fetch projects for a specific user
getNodeById(globalId: string)
: Fetch an object by its global ID (for Relay compatibility)
embedProject(elementOrId, project, options?)
: Create and embed a project in an elementopenProject(project, options?)
: Open a project in a new tabembedGithubProject(elementOrId, githubProject, options?)
: Embed a project from GitHubopenGithubProject(githubProject, options?)
: Open a GitHub project in a new tab
The library provides a complete GraphQL schema with queries, mutations, and types for working with StackBlitz:
# Main entry points
type Query {
node(id: ID!): Node
projects(filters: ProjectFiltersInput, pagination: PaginationInput): ProjectConnection!
project(id: ID!): Project
users(pagination: PaginationInput): UserConnection!
user(id: ID!): User
}
type Mutation {
createProject(input: CreateProjectInput!): CreateProjectPayload!
updateProject(id: ID!, input: UpdateProjectInput!): UpdateProjectPayload!
deleteProject(id: ID!): DeleteProjectPayload!
forkProject(id: ID!, input: UpdateProjectInput): ForkProjectPayload!
}
See the full schema in the documentation.
const { project } = await dataSources.stackBlitzAPI.createProject({
title: 'My React App',
description: 'A simple React application',
files: {
'index.js': 'import React from "react"; import ReactDOM from "react-dom";',
'App.js': 'export default () => <h1>Hello StackBlitz!</h1>;',
'index.html': '<div id="root"></div>'
},
template: 'react',
dependencies: {
'react': '17.0.2',
'react-dom': '17.0.2'
}
});
const { edges, pageInfo } = await dataSources.stackBlitzAPI.getProjects(
// Filters
{
tag: 'react',
search: 'todo app'
},
// Pagination
{
first: 10,
after: 'cursor-value'
}
);
// Access results
edges.forEach(({ node }) => console.log(node.title));
// Check if there are more results
if (pageInfo.hasNextPage) {
// Fetch next page using the endCursor
const nextPage = await dataSources.stackBlitzAPI.getProjects(
{ tag: 'react', search: 'todo app' },
{ first: 10, after: pageInfo.endCursor }
);
}
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.