• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

graphitejs/server: Framework NodeJS for GraphQl

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

graphitejs/server

开源软件地址(OpenSource Url):

https://github.com/graphitejs/server

开源编程语言(OpenSource Language):

JavaScript 100.0%

开源软件介绍(OpenSource Introduction):

GraphiteJS

Framework NodeJS for GraphQl

graphitejs TravisCI Status Codecov Status semantic-release npm latest version


GraphiteJS is a NODE.JS Framework for building GraphQL schemas/types fast, easily and with scalability.

  • Easy to use: GraphiteJS make easy GraphQL in NodeJS without effort.
  • Any Front: GraphiteJS support any front library.
  • Data agnostic: GraphiteJS supports any kind of data source.

Guide


Install

npm i @graphite/server --save

yarn add @graphite/server

on your index file:

import { Graphite } from '@graphite/server'

main = async () => {
  const graphite = await Graphite()
}

main()

and that's all, you have running the graphqli tool on the port 4000 by default.

How to use

After install @graphite/server you have to create your first model. We recommend creating a folder called models and follow the pattern matching the filename with the Type name.

Types

import { GraphQL } from '@graphite/server'

export const Developer = GraphQL('Developer')({
   // the value always have to be an array first arg is the type, the second arg is an optional comment
  name: ['String!', 'Your name is required'],
  age: ['Int'],
  isGreatDeveloper: ['Boolean']
})

So, now you need to pass this model to the Graphite Server

on index.js

import { Graphite } from '@graphite/server'
import { Developer } from './models/Developer'

main = async () => {
  await Graphite({ models: [Developer] })
}

main()

Queries

import { GraphQL } from '@graphite/server'

export const Developer = GraphQL('Developer')({
  name: ['String!', 'Your name is required'],
  age: ['Int'],
  isGreatDeveloper: ['Boolean'],

  Query: {
    'developer: Developer': () => ({ name: 'Your name' }),
    'developers: [Developer]': () => ([{ name: 'Your name' }]),
  }
})

Mutations

import { GraphQL } from '@graphite/server'

export const Developer = GraphQL('Developer')({
  name: ['String!', 'Your name is required'],

  Mutation: {
    'createDeveloper(name: String): Developer': (_, { name, }) => ({ name }),
    'updateDeveloper(id: ID!, name: String): Developer': (_, { name }) => ({ name }),
    'removeDeveloper(id: ID!): Developer': (_, { name }) => ({ name }),
  },
})

Subscriptions

import { GraphQL, PubSub } from '@graphite/server'

const pubsub = new PubSub()
const DEVELOPER_ADDED = 'DEVELOPER_ADDED'

export const Developer = GraphQL('Developer')({
  name: ['String!', 'Your name is required'],

  Mutation: {
    'createDeveloper(name: String): Developer': (_, { name, }) => { 
      pubsub.publish(DEVELOPER_ADDED, { developerAdded: { name } })
      return { name }
    },
  },

  Subscription: {
    'developerAdded: Developer': {
      subscribe: () => pubsub.asyncIterator([DEVELOPER_ADDED]),
    },
  },
})

Relations

  // models/Repository.js
  const Repository = GraphQL('Repository')({
      name: ['String'],
      url: ['String'],
  })

  // models/GithubProfile.js
  const GithubProfile = GraphQL('GithubProfile')({
      url: ['String'],
  })

  // models/Developer.js
  const Developer = GraphQL('Developer')({
    name: ['String'],

    'respositories: [Repository]': () => [{ name: 'GraphiteJS', url: 'https://github.com/graphitejs/graphitejs' }],

    'githubProfile: GithubProfile': () => ({ url: 'https://github.com/wzalazar' }),

    Query: {
      'developer: Developer': () => ({ name: 'Walter Zalazar' }),
    },
  })

So, now you need to pass this model to the Graphite Server

on index.js

import { Graphite } from '@graphite/server'
import { Developer } from './models/Developer'
import { Repository } from './models/Repository'
import { GithubProfile } from './models/GithubProfile'

main = async () => {
  await Graphite({ models: [Developer, Repository, GithubProfile] })
}

main()

Contributing

Please see our contributing.md

  • Clone this repository.
  • Install dependencies.
npm install
  • Feel free for pull request.

Team

Creator

Walter Zalazar
Walter Zalazar
:octocat: @wzalazar

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap