[GraphQL] Write a GraphQL Schema in JavaScript
Writing out a GraphQL Schema in the common GraphQL Language can work for simple GraphQL Schemas, but as our application grows, or when we start using more complex types like interfaces or unions, we find that we can’t use a GraphQL Language file in the same way as before. In this video, we’ll learn how to translate a GraphQL Schema written in GraphQL into a GraphQL Schema written in JavaScript.
- const express = require('express');
- const graphqlHttp = require('express-graphql');
- const server = express();
- const port = process.env.PORT || ;
- const {
- GraphQLSchema,
- GraphQLObjectType,
- GraphQLString,
- GraphQLInt,
- GraphQLBoolean,
- GraphQLID
- } = require('graphql');
- const videoType = new GraphQLObjectType({
- name: 'video',
- description: 'A video on Egghead.io',
- fields: {
- id: {
- type: GraphQLID,
- description: 'The id of the video'
- },
- title: {
- type: GraphQLString,
- description: 'The title of the video'
- },
- duration: {
- type: GraphQLInt,
- description: 'The duration of the video'
- },
- watched: {
- type: GraphQLBoolean,
- description: 'Whether or no the viewer watched the video'
- }
- }
- })
- const queryType = new GraphQLObjectType({
- name: 'QueryType',
- description: 'The root query type',
- fields :{
- video: {
- type: videoType,
- resolve: () => {
- return new Promise((resolve) => {
- resolve({
- id: 'a',
- title: 'GraphQL',
- duration: ,
- watched: false })
- })
- }
- }
- }
- });
- const schema = new GraphQLSchema({
- query: queryType
- });
- /*
- const schema = buildSchema(`
- type Video {
- id: ID,
- title: String,
- duration: Int,
- watched: Boolean
- }
- type Query {
- video: Video,
- videos: [Video]
- }
- type Schema{
- query: Query
- }
- `);
- const videos = [
- {
- id : '1',
- title : 'react',
- duration : 180,
- watched : true
- },
- {
- id : '2',
- title : 'relay',
- duration : 230,
- watched : false
- }
- ];
- const resolvers = {
- video : () => ({
- id : '1',
- title : 'bar',
- duration : 180,
- watched : true
- }),
- videos : () => videos
- };*/
- server.use('/graphql', graphqlHttp({
- schema,
- graphiql : true, // use graphiql interface
- }));
- server.listen(port, () => {
- console.log(`Listening on http`)
- })
[GraphQL] Write a GraphQL Schema in JavaScript的更多相关文章
- [GraphQL] Serve a GraphQL Schema as Middleware in Express
If we have a GraphQL Schema expressed in terms of JavaScript, then we have a convenient package avai ...
- [GraphQL] Create a GraphQL Schema
we’ll take a look at the GraphQL Language and write out our first GraphQL Schema. We’ll use the grap ...
- graphql cli 开发graphql api flow
作用 代码生成 schema 处理 脚手架应用创建 项目管理 安装cli npm install -g graphql-cli 初始化项目(使用.graphqlconfig管理) 以下为demo de ...
- Reusing & Composing GraphQL APIs with GraphQL Bindings
With GraphQL bindings you can embed existing GraphQL APIs into your GraphQL server. In previous blog ...
- [GraphQL] Deploy a GraphQL dev playground with graphql-up
In this lesson we'll use a simple GraphQL IDL schema to deploy and explore a fully functional GraphQ ...
- [GraphQL] Write a GraphQL Mutation
In order to change the data that we can query for in a GraphQL Schema, we have to define what is cal ...
- [GraphQL] Query a GraphQL API with graphql-request
To query a GraphQL API, all you need to do is send an HTTP request that includes the query operation ...
- 使用Hot Chocolate和.NET 6构建GraphQL应用(1)——GraphQL及示例项目介绍
系列导航 使用Hot Chocolate和.NET 6构建GraphQL应用文章索引 前言 这篇文章是这个系列的第一篇,我们会简单地讨论一下GraphQL,然后介绍一下这个系列将会使用的示例项目. 关 ...
- [GraphQL] Add an Interface to a GraphQL Schema
As we start building out more complex GraphQL schemas, certain fields start to repeat across differe ...
随机推荐
- UIScrollView详解
1.UIScrollView常用属性 contentSize属性--该属性表示滚动的内容的范围大小,是CGPoint类型的. 说明: 默认超出UIScrollView的可视区域的内容是不显示的.相当于 ...
- PHP通过文件存储来实现缓存
在一些数据库数据记录较大,但是服务器有限的时候,可能一条MySQL查询就会好几百毫秒,一个简单的页面一般也有十几条查询,这个时候也个页面加载下来基本要好几秒了,如果并发量高的话服务器基本就瘫痪了,造成 ...
- iOS 关于nil和Nil及null与<null>的区别
问题是这样的. NSDictionary *sample = [NSJSONSerialization JSONObjectWithData:received options:NSJSONReadin ...
- 配置本地IIS和VS自带IIS
以前调试网站一直用的vs自带的IIS,做为学习没啥大碍,但要是用于团队项目开发就会带来诸多不便.团队开发项目有统一的网站端口号.文件目录什么的,端口号可以在配置文件中修改倒也没啥,但是要在自己的项目中 ...
- SQLite主键自增需要设置为integer PRIMARY KEY
按照正常的SQL语句,创建一个数据表,并设置主键是这样的语句: ), EventType )) 但使用这种办法,在SQLite中创建的的数据表,如果使用Insert语句插入记录,如下语句: INSER ...
- WinDbg 命令三部曲:(三)WinDbg SOSEX 扩展命令手册
本文为 Dennis Gao 原创技术文章,发表于博客园博客,未经作者本人允许禁止任何形式的转载. 系列博文 <WinDbg 命令三部曲:(一)WinDbg 命令手册> <WinDb ...
- 从数组中选出和等于固定值的n个数(JavaScript实现)
现实生活中的问题,可能会抽象为这样一种数据模型: 从一个数组中挑选出几个数,让这几个数相加的和为指定的值. 大多数读者应该有过网购的经历,网购一般会有个凑单功能,假如读者买了70元的商品,但是必须满1 ...
- 使用Lucene.NET实现数据检索功能
引言 在软件系统中查询数据是再平常不过的事情了,那当数据量非常大,数据存储的媒介不是数据库,或者检索方式要求更为灵活的时候,我们该如何实现数据的检索呢?为数据建立索引吧,利用索引技术可以更灵活 ...
- JavaScript核心编程(代码片段)
var a = function () { function someSetup() { var setup = 'done'; } function actualWork() { alert('Wo ...
- APP顶号逻辑
登录的接口login.do接口上需要记录关键的信息:userId.设备型号(Android|iPhone).登录时间.登录IP.设备唯一标识(UUID) 当在另外一台新设备上登录同一帐号时,将user ...