Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

☄️ A minimalistic zero-config GraphQL server.

License

NotificationsYou must be signed in to change notification settings

JavaScriptExpert/graphpack

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

☄️ A minimalistic zero-config GraphQL server

Check out the demo on CodeSandbox:https://codesandbox.io/s/k3qrkl8qlv


What is included?

Graphpack lets you create GraphQL serverswith zero configuration. It useswebpack withnodemon andApollo Server under the hood, so we get features like Live Reloading, GraphQL Playground, GraphQL Imports and many more right out of the box.

  • 📦Zero-config out of the box
  • 🚦 Built-inLive reload
  • 🚨 Super-friendly error messages
  • 🎮GraphQL Playground IDE
  • ⭐️GraphQL imports in Schema Definition Language
  • 🔥Blazing fast bundle times
  • ⚡️ES module imports and dynamicimport()'s thanks toBabel

Install & Usage

yarn add --dev graphpack

Addsrc/schema.graphql andsrc/resolvers.js

src├── resolvers.js└── schema.graphql

In your schema and add some sample types inSDL:

typeQuery {hello:String}

Insrc/resolvers.js:

constresolvers={Query:{hello:()=>'world!',},};exportdefaultresolvers;

Setuppackage.json run scripts

Add following scripts to yourpackage.json:

"scripts": {"dev":"graphpack","build":"graphpack build"  },

Start development server

To start the development server, simply run:

yarn dev

Create production build

To create a production ready build run following command:

yarn build

Use production build

Add following script that executes our build:

"scripts": {"start":"node ./build/index.js"  },

Following command will run the build and start the app

yarn start

Make sure to create a build before running the start script.

CLI Commands

graphpack (aliasgraphpack dev)

Runs graphpack in development mode. After a successful build your output should look something like this:

graphpack

Graphpack will watch for changes in your./src folder and automatically reloads the server.

graphpack build

Creates a production ready build under the project rootsbuild folder.

Make sure to runyarn build before.

Entry files

src/resolvers.js (required)

In this file you define all your resolvers:

// src/resolvers.jsconstresolvers={Query:{article:(obj,args)=>getArticleById(args.id),articles:()=>getArticles(),},};exportdefaultresolvers;

You can use any of these folder/file structure:

  • src/resolvers.js
  • src/resolvers/index.js

src/schema.js (required)

Here you define all your GraphQL type definitions:

# src/schema.graphqltypeArticle {title:Stringbody:String}typeQuery {article:Articlearticles: [Article!]!}

Alternatively you can create asrc/schema.js and use the template literal taggql bygraphql-tag:

// src/schema.jsimport{gql}from'graphql-tag';consttypeDefs=gql`  type Article {    title: String    body: String  }  type Query {    article: Article    articles: [Article!]!  }`;exportdefaulttypeDefs;

Note that you need installgraphql-tag in this case.

Graphpack can resolve both.js and.graphql files. This means you can use any of these folder/file structure:

  • src/schema.js
  • src/schema/index.js
  • src/schema.graphql
  • src/schema/index.graphql

src/context.js

Createsrc/context.js and do following:

constcontext=req=>({/* context props here */});exportdefaultcontext;

You can use any of these folder/file structure:

  • src/context.js
  • src/context/index.js

src/config.js

Insrc/config.js you can set further options of your Apollo Server. Refer to theApollo Server docs for more details about the options.

// src/config.jsconstIS_DEV=process.env.NODE_ENV!=='production';constconfig={debug:process.env.DEBUG,playground:IS_DEV,introspection:IS_DEV,mocks:IS_DEV,// ...};exportdefaultconfig;

Note that you cannot to setresolvers,typeDefs orcontext in the config file. In these cases please refer toentry files.

You can use any of these folder/file structure:

  • src/config.js
  • src/config/index.js

Custom configuration

For custom configuration you can create agraphpack config file incosmiconfig format:

  • graphpack.config.js
  • graphpack field inpackage.json
  • .graphpackrc in JSON or YAML
  • .graphpackrc with the extensions.json,.yaml,.yml, or.js

Customize Webpack configuration

To extend webpack, you can define a function that extends its config via the config file:

// graphpack.config.jsmodule.exports={webpack:({ config, webpack})=>{// Add customizations to config// Important: return the modified configreturnconfig;},};

Customize Babel configuration

Add an optionalbabel.config.js to your project root with the following preset

// babel.config.jsmodule.exports=api=>{// Cache the returned value forever and don't call this function againapi.cache(true);return{presets:['graphpack/babel'],// ... Add your plugins and custom config};};

Note that this file is not going through babel transformation.

Acknowledgements

Graphpack was heavily inspired by:

License

MIT

About

☄️ A minimalistic zero-config GraphQL server.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript100.0%

[8]ページ先頭

©2009-2025 Movatter.jp