[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 ...
随机推荐
- AIX之ASM存储扩容
ASM存储扩容操作其实很简单,无非就是向DiskGroup(简称DG)里添加物理磁盘,增加DG的存储空间.说来简单,其实操作过程中有很多小细节要注意,否则,带来的后果是灾难性的. ASM扩容操作步骤( ...
- leveldb.net对象读写封装
leveldb是一个非常高效的可嵌入式K-V数据库,在.NET下有着基于win实现的包装leveldb.net;不过leveldb.net只提供了基于byte[]和string的处理,这显然会对使用的 ...
- Arcgis for Javascript 在VS2012中的智能提示
官方地址: https://developers.arcgis.com/en/javascript/jsapi/api_codeassist.html 安装步骤 Visual Studio 2010 ...
- 编译生成.NET Core Framework遇到的问题
前两天在Windows Server 2012上编译生成.NET Core Framework的代码库corefx,遭遇了几个问题,在这篇博文中记录一下. 编译生成操作方法是在命令行(Develope ...
- CentOS升级MySQL到5.5
centOS的yum安装的MySQL是5.1版本,可通过官方的rpm包安装5.5版本 # 查看安装的相关项 rpm -qa|grep -i mysql # 停止服务 service mysqld st ...
- Mac 快捷键整理
Mac 快捷键整理 文本编辑 适用于文本编辑器,浏览器等 跳到页首 cmd + ↑ 类似windows下的 ctrl + home 跳到页尾 cmd + ↓ 类似windows下的 ctrl + en ...
- C++数组和指针
<C++ Primer 4th>读书摘要 与 vector 类型相似,数组也可以保存某种类型的一组对象:而它们的区别在于,数组的长度是固定的.数组一经创建,就不允许添加新的元素.指针则可以 ...
- win2003 Enterprise Edition sp2 企业版序列号
HXCRB-TQW9R-42JK8-TQ7X2-PJRDY Windows Server 2003 R2, x32 EDDVB4Y-KF6GK-MT3XX-FW3HC-VXTB6
- 浅谈压缩感知(二十七):压缩感知重构算法之稀疏度自适应匹配追踪(SAMP)
主要内容: SAMP的算法流程 SAMP的MATLAB实现 一维信号的实验与结果 稀疏度K与重构成功概率关系的实验与结果 一.SAMP的算法流程 前面所述大部分OMP及其前改算法都需要已知信号的稀疏度 ...
- JavaScript、for循环语句知识巩固,while(){}语句以及do{}while()语句以及switch()语句
一.for循环语句练习 关于for循环存在的两个问题类型 穷举:在不知道什么情况下才真的寻要我们的结果,自能让我们一个个走一遍. 迭代:在现有的条件根据规律不断求解,中间情况,最终推测出来的结果 1. ...