Skip to content

Example implementation of the Pothos (formerly GiraphQL) library to create a GraphQL server with queries, mutations, and subscriptions along with unit and integration tests.

License

theogravity/graphql-pothos-server-example

Repository files navigation

Pothos GraphQL Server Example

This repo demonstrates how to use the graphQL code-based pothos (formerly named giraphql) library to build queries, mutations, subscriptions, and add directives.

It also includes sample jest tests.

It is written using Typescript.

Components:

Installing

node.js v14 is the baseline requirement.

$ yarn

Running

$ yarn dev

The server will start on port 3000.

Tests

Run unit tests with:

$ yarn test:unit

Integration tests:

$ yarn test:integration

Schema + Typescript definition generation

You can use the graphql-codegen generated files with your frontend for type information when calling the server.

$ yarn generate

Generated files are located in the gql-schema directory.

The following plugins are used:

Directory structure

In the src/ folder:

  • datasources/: Apollo datasources for making db calls
  • db/: Sequelize models
  • gql/: pothos definitions

The gql/ folder is organized by domain. The reason I've done this vs organizing by queries or mutations is the following:

  • pothos is a code-based gql definition library. Because it's code-based, you have a lot of control on how to define your graphQL items. The trade-off is that it's more verbose. If you were to shove everything under a single query or mutation file, it would be extremely difficult to navigate and read.
  • Types themselves can have multiple resolvers that are independent of a query or mutation and can alone be quite verbose / complex.
  • It's easier to find files that are specific to the function in an IDE. For example, the post creation mutation can be easily discovered in file search.
  • All the code for that specific function lives in a single file makes it easier to bounce back and forth from vs jumping between files.

Sample queries

Here's some queries to try out in the playground:

Create a user and post:

mutation {
  createUser(input: {
    name:"Theo Gravity"
  }) {
    user {
      id
    }
  }
  createPost(input:{
    authorId: 1
    title:"Test title",
    content:"Test post"
  }) {
    id
  }
}

Query for user posts:

query {
  user(id: 1) {
    id
    name
    posts {
      id
      title
      content
      created
    }
  }
}

Get users:

query {
  users {
    id
    name
  }
}

Get posts:

query {
  posts {
    id
    title
    content
    created
    author {
      name
    }
  }
}

About

Example implementation of the Pothos (formerly GiraphQL) library to create a GraphQL server with queries, mutations, and subscriptions along with unit and integration tests.

Topics

Resources

License

Stars

Watchers

Forks