GraphQL Yoga

GraphQL Yoga

Install adapter

npm i @aexol/axolotl-core @aexol/axolotl-graphql-yoga

Now you need a schema.graphql file or a URL with settings to download the schema from upstream. Out of it Axolotl can generate simple type definitions needed for the library out of your GraphQL Schema.

models.ts are located where you specify when initiating Axolotl

First execute axolotl build command

npx @aexol/axolotl build

Then write your axolotl.ts file.

axolotl.ts
import { Axolotl } from '@aexol/axolotl-core';
import { graphqlYogaAdapter } from '@aexol/axolotl-graphql-yoga';
import { Models } from './models.js';
 
export const { createResolvers, adapter } = Axolotl(graphqlYogaAdapter)<Models>();
resolvers.ts
import { createResolvers } from './axolotl.js';
export default createResolvers({
    // your type-safe resolvers here
});

Then write your index.ts file.

index.ts
import { adapter } from './axolotl.js';
import resolvers from './resolvers.js';
 
adapter({resolvers}).server.listen(4000, () => {
  console.log('LISTENING');
});

You can also use this adapter with express. This is useful when dealing with webhooks. Take a look how to implement a simple webhook.

index.ts
import { adapter } from './axolotl.js';
import resolvers from './resolvers.js';
import express from 'express'
 
const app = express();
 
const {yoga} = adapter({ resolvers });
 
app.use(yoga.graphqlEndpoint, yoga)
 
app.get('/webhook', (req, res) => {
  res.send('Show me the money!')
})
 
app.listen(4000, () => {
  console.log('Running a GraphQL API server at http://localhost:4000/graphql')
})

And you can implement anything you want in express now.