[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 available to us that let’s us easily serve up our schema on any endpoint in an Express Server. In this video, we’ll use the express-graphql package to serve up our GraphQL Schema as middleware, and also learn how to enable the GraphiQL tool in order to query our GraphQL Schema.
Install:
yard add express express-graphql graphql
const express = require('express');
const graphqlHttp = require('express-graphql');
const server = express();
const port = process.env.PORT || ;
const { graphql, buildSchema } = require('graphql');
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 : '',
title : 'react',
duration : ,
watched : true
},
{
id : '',
title : 'relay',
duration : ,
watched : false
}
];
const resolvers = {
video : () => ({
id : '',
title : 'bar',
duration : ,
watched : true
}),
videos : () => videos
};
server.use('/graphql', graphqlHttp({
schema,
graphiql : true, // use graphiql interface
rootValue : resolvers
}));
server.listen(port, () => {
console.log(`Listening on http`)
})
We use 'graphql' middleware, once we visit http://localhost:3000/graphql and enter the query:
{
videos {
id
title
duration
watched
}
}
Then we can get the result.

[GraphQL] Serve a GraphQL Schema as Middleware in Express的更多相关文章
- [GraphQL] Write a GraphQL Schema in JavaScript
Writing out a GraphQL Schema in the common GraphQL Language can work for simple GraphQL Schemas, but ...
- [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 ...
随机推荐
- 【转发】Cross-thread operation not valid: Control 'progressBar1' accessed from a thread other than the thread it was created on
当您试图从单独的线程更新一个win form时,您将得到如下错误信息: "Cross-thread operation not valid: Control 'progressBar1' ...
- 入门级:怎么使用C#进行套接字编程(一)
翻译一篇简单的文章学习下基础,此文针对我等对socket只听说未尝试阶段的水平. How to C# Socket programming C#通过他的命名空间像System.Net和System.N ...
- 在Github上注册账户
首先打开网址:https://github.com/ 进行注册 注册完成后进入邮箱验证 在右上角创建一个简单的项目仓库 创建完成
- atitit.java解析sql语言解析器解释器的实现
atitit.java解析sql语言解析器解释器的实现 1. 解析sql的本质:实现一个4gl dsl编程语言的编译器 1 2. 解析sql的主要的流程,词法分析,而后进行语法分析,语义分析,构建sq ...
- js脚本语言基础和数组
js和PHP中,字符串赋值:要使用"双引号"或"单引号"引起来:例如:var c="你好"不同类型进行数学运算,要转换,类型转换:强制转换p ...
- Mac OS X 系统下自带的文本文件格式转换工具iconv
1. utf-8 转 GBK的方法 在mac bash 中直接运行 iconv -f UTF-8 -t GBK test_utf8.txt > test_gbk.txt 举例:创建测试文件 ec ...
- iOS-程序发布-32位和64位系统的兼容
在苹果推出iPhone5S时,64位的应用就走到了眼前.当时就看见苹果官方资料宣布iOS7.x的SDK支持了64位的应用,而且内置的应用都已经是64位. 我记得自己刚刚接触电脑时还有16位的系统,指针 ...
- Maven学习总结(九)——使用Nexus搭建Maven私服
一.搭建nexus私服的目的 为什么要搭建nexus私服,原因很简单,有些公司都不提供外网给项目组人员,因此就不能使用maven访问远程的仓库地址,所以很有必要在局域网里找一台有外网权限的机器,搭建n ...
- 让delphi解析chrome扩展的native应用
chrome浏览器自从去年以来逐步去掉了对浏览器插件的支持,npapi的方案马上不可用. 当务之急要选择一个替代方案,最常用的就是扩展了.扩展程序提供了一套和本地程序交互的方案——“原生消息通信” 写 ...
- Fiddler 修改返回内容 OnBeforeResponse 无效 没用
Fiddler自定义脚本可以实现很强大的内容替换,包括很有意义的——修改返回内容. 具体的方法可以参考官网:http://docs.telerik.com/fiddler/KnowledgeBase/ ...