Pothos GraphQL
Searching...

Deno

Pothos is compatible with Deno, and can be used with GraphQL Yoga which now also supports deno!

Imports

There are a number of different ways to import Pothos, but the best option is ussually to set up import maps and import from esm.sh.

Import maps

// import_maps.json
{
  "imports": {
    // define a version of graphql, this should be shared by all graphql libraries
    "graphql": "https://esm.sh/graphql@16.6.0",
    // Marking graphql as external will all the graphql from this import_map to be used
    "graphql-yoga": "https://esm.sh/graphql-yoga?external=graphql",
    // the `*` prefix in the package name marks all depenencies (only graphql in this case) as external
    // this ensures the version of graphql defined above is used
    "@pothos/core": "https://esm.sh/*@pothos/core@3.23.1",
    // Plugins should mark all dependencies as external as well
    // this will ensure that both graphql and @pothos/core use the versions defined above
    // some plugins like validation may require additional dependencies to be added to the import map (eg. zod)
    "@pothos/plugin-relay": "https://esm.sh/*@pothos/plugin-relay@3.30.0"
  }
}

deno.json

// deno.jsonc
{
  "importMap": "import_map.json"
}

Server

// src/index.ts
import { serve } from 'https://deno.land/std@0.157.0/http/server.ts';
import { createYoga } from 'graphql-yoga';
import SchemaBuilder from '@pothos/core';

const builder = new SchemaBuilder({});

builder.queryType({
  fields: (t) => ({
    hello: t.string({
      args: {
        name: t.arg.string({}),
      },
      resolve: (_, { name }) => `hello, ${name || 'World'}`,
    }),
  }),
});

const yoga = createYoga({
  schema: builder.toSchema(),
});

serve(yoga, {
  onListen({ hostname, port }) {
    console.log(`Listening on http://${hostname}:${port}/graphql`);
  },
});

Running the app:

deno run --allow-net src/index.ts

Without import maps

In some cases (like when using the deno deploy playground) you may not be able to use import maps. In this case, you can use query parameters with esm.sh to ensure that shared versions of packages are used:

import { serve } from 'https://deno.land/std@0.157.0/http/server.ts';
// for graphql-yoga and pothos/core 'graphql' is the most import depencency to pin
import { createYoga } from 'https://esm.sh/graphql-yoga@3.1.1?deps=graphql@16.6.0';
import SchemaBuilder from 'https://esm.sh/@pothos/core@3.23.1?deps=graphql@16.6.0';
// for pothos plugins, you should pin both 'graphql' and '@pothos/core'
import RelayPlugin from 'https://esm.sh/@pothos/plugin-relay@3.30.0?deps=graphql@16.6.0,@pothos/core@3.23.1';

The @pothos/deno package

The @pothos/deno package contains a typescript-only version of most of the pothos plugins. This is no longer the recommended way to use pothos with deno, but will continue to be published with new changes.

The files for this package are published to npm, and can be consumed from a number of CDNs. The benefit of this is that all plugins are bundled with pothos/core, and import directly so you do not need to mess with dependencies to ensure that plugins are using the correct version of pothos/core.

example

// dependencies of @pothos/deno are imported from https://cdn.skypack.dev/{package} to ensure
// that the same version of 'graphql' is used, import other dependencies from sky pack as well
import { serve } from 'https://deno.land/std@0.157.0/http/server.ts';
import { createYoga } from 'https://cdn.skypack.dev/graphql-yoga@3.1.1';
import SchemaBuilder from 'https://esm.sh/@pothos/deno/packages/core/mod.ts';
import RelayPlugin from 'https://esm.sh/@pothos/deno/packages/plugin-relay/mod.ts';

Pothos uses https://cdn.skypack.dev/graphql?dts which can be added to an import map to import a different version of graphql.