Code Monkey home page Code Monkey logo

graphql-pothos-server-example's Introduction

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
    }
  }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.