Skip to main content

graphql

@webda/graphql​

Auto-generates a fully functional GraphQL API — queries, mutations, and subscriptions — from your Webda domain models. Zero hand-written SDL required.

When to use it​

Add @webda/graphql when you want a GraphQL endpoint alongside (or instead of) the REST endpoint. It reads your model graph from @webda/compiler's model manifest and builds the GraphQL schema dynamically. Subscriptions over WebSocket (graphql-ws) are enabled by default.

Install​

npm install @webda/graphql

Configuration​

Add the GraphQLService to your webda.config.json:

{
"services": {
"GraphQLService": {
"type": "Webda/GraphQLService",
"url": "/graphql",
"exposeGraphiQL": true,
"globalSubscription": true
}
}
}

Auto-generated operations​

For every exposed model (e.g. Post) the service generates:

OperationTypeDescription
Post(uuid)QueryFetch one by primary key
Posts(query, limit, offset)QueryList / filter all
createPost(Post: PostInput)MutationCreate new instance
updatePost(uuid, Post: PostInput)MutationReplace by primary key
deletePost(uuid)MutationDelete by primary key
PostEventsSubscriptionStream events for all posts
PostEvent(uuid)SubscriptionStream events for a specific post

Custom @Operation() methods on the model become additional mutations or queries automatically.

Quick usage​

# Query all published posts
{ Posts { results { slug title status viewCount } } }

# Create a post
mutation {
createPost(Post: {
title: "Hello World"
slug: "hello-world"
content: "My first post content with enough text."
status: "draft"
viewCount: 0
}) {
slug
title
}
}

Subscriptions (graphql-ws)​

subscription {
PostEvents {
Created { object { slug title } }
Updated { object { slug title } }
Deleted { object_id }
}
}

The subscription server uses graphql-ws over WebSocket (same port as the HTTP server when TLS is enabled via autoTls).

GraphiQL​

Set exposeGraphiQL: true (the default) to enable the in-browser GraphiQL IDE at /graphql. Useful for development and introspection.

Parameters​

ParameterDefaultDescription
url/graphqlHTTP endpoint path
exposeGraphiQLtrueServe the GraphiQL explorer UI
exposeMetrueExpose { me { ... } } query for current user
globalSubscriptiontrueExpose aggregate AggregateSubscriptions subscription
userModelUserShort name of the User model for me query

See also​

Classes​

Interfaces​