nexus
v1.3.0
โครงสร้างสคีมา GraphQL ที่ประกาศ เน้นโค้ดเป็นหลัก และพิมพ์อย่างยิ่งสำหรับ TypeScript และ JavaScript
npm install nexus graphql
โปรดทราบว่าคุณต้องเพิ่ม graphql
ด้วย Nexus ปักหมุดไว้เป็นการพึ่งพาเพื่อน
graphql-js
และเป็น เพียง GraphQLSchema
apollo-server
, graphql-middleware
ฯลฯ import { queryType , stringArg , makeSchema } from 'nexus'
import { GraphQLServer } from 'graphql-yoga'
const Query = queryType ( {
definition ( t ) {
t . string ( 'hello' , {
args : { name : stringArg ( ) } ,
resolve : ( parent , { name } ) => `Hello ${ name || 'World' } !` ,
} )
} ,
} )
const schema = makeSchema ( {
types : [ Query ] ,
outputs : {
schema : __dirname + '/generated/schema.graphql' ,
typegen : __dirname + '/generated/typings.ts' ,
} ,
} )
const server = new GraphQLServer ( {
schema ,
} )
server . start ( ( ) => `Server is running on http://localhost:4000` )
ตัวอย่างเพิ่มเติมสามารถพบได้ในไดเร็กทอรี /examples
:
คุณสามารถค้นหาเอกสารสำหรับ Nexus ได้ที่นี่
หากคุณได้ปฏิบัติตามแนวทางที่เน้น SDL ในการสร้างเซิร์ฟเวอร์ GraphQL ของคุณ และต้องการดูว่าโค้ดของคุณมีลักษณะอย่างไรเมื่อเขียนด้วย GraphQL Nexus คุณสามารถใช้ ตัวแปลง SDL ได้