Custom Queries & Mutations
You can add queries and mutations to the GraphQL schema that do not result from an Entity or Operation. As you see the configuration type for Query and Mutation is the same. You should consider add behavior as Query or Mutation depending on whether you change any data or not.
QueryMutationConfig
| Config | Type | Description |
|---|---|---|
| type | string | Type definition | this can be a type name of the schema or a type definition |
| args | {[name:string]:string | QueryMutationArgConfig } | this can be a type name of the schema or a QueryMutationArgConfig |
| resolve | (root:any, args:any, context:ResolverContext, info:any ) => any | standard resolve implementation; implement any business logic here |
| description | string | Description that will be part of the Schema documentation |
Example
In this example we define a Query "greeting" that defines one param "name" and returns a greeting using that "name" and the current time from the Runtime. As you might see we make use of the now callback, to control the "current" time, rather than relying on Date.now
import { DomainConfiguration } from "activeql-foundation";
export const domainConfiguration: DomainConfiguration = {
now: () => new Date( Date.parse('2023-12-12 05:50:00') ),
query: {
Greeting: {
type: "String",
args: { name: 'String!' },
resolve: (root:any, {name}, {runtime} ) => {
if( runtime.now().getHours() < 6 ) {
return `That's an early morning, ${name}`;
}
return `Hey ${name}, have a great day`
}
}
}
}