Build a GraphQL API in Vectrune
Define a GraphQL greeting schema, query, and mutation with declarative Rune resolvers and test requests over HTTP.
Save as graphql.rune
GraphQL uses its own @App type. Run this example separately from the REST service because both examples use port 3000.
Rune source
#!RUNE
@App
name = Greeting GraphQL
type = GraphQL
port = 3000
@Schema/Greeting
message = string
@GraphQL/Query/Greeting
greeting:
result = { message: "Hello from Rune" }
return result
@GraphQL/Mutation/Greeting
greet(message: string):
result = { message: message }
return result
Run and query
@Schema/Greeting defines the result object. @GraphQL/Query/Greeting groups fields returning that type; return passes the resolver result back to GraphQL. The greeting query returns a message field.
Command / usage
vectrune graphql.rune
# In a second terminal:
curl http://127.0.0.1:3000/graphql -H 'Content-Type: application/json' -d '{"query":"{ greeting { message } }"}'Call a mutation
@GraphQL/Mutation/Greeting declares greet(message: string). This example returns the supplied message and does not persist it.
Command / usage
curl http://127.0.0.1:3000/graphql -H 'Content-Type: application/json' -d '{"query":"mutation { greet(message: \"Hello\") { message } }"}'Explore a stateful example
The repository also includes examples/api/book_graphql.rune, which demonstrates books, authors, memory-backed queries, and mutations. Use that as the next step after this minimal service.