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的更多相关文章

  1. [GraphQL] Write a GraphQL Schema in JavaScript

    Writing out a GraphQL Schema in the common GraphQL Language can work for simple GraphQL Schemas, but ...

  2. [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 ...

  3. graphql cli 开发graphql api flow

    作用 代码生成 schema 处理 脚手架应用创建 项目管理 安装cli npm install -g graphql-cli 初始化项目(使用.graphqlconfig管理) 以下为demo de ...

  4. Reusing & Composing GraphQL APIs with GraphQL Bindings

    With GraphQL bindings you can embed existing GraphQL APIs into your GraphQL server. In previous blog ...

  5. [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 ...

  6. [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 ...

  7. [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 ...

  8. 使用Hot Chocolate和.NET 6构建GraphQL应用(1)——GraphQL及示例项目介绍

    系列导航 使用Hot Chocolate和.NET 6构建GraphQL应用文章索引 前言 这篇文章是这个系列的第一篇,我们会简单地讨论一下GraphQL,然后介绍一下这个系列将会使用的示例项目. 关 ...

  9. [GraphQL] Add an Interface to a GraphQL Schema

    As we start building out more complex GraphQL schemas, certain fields start to repeat across differe ...

随机推荐

  1. Hive性能优化

    1.概述 继续<那些年使用Hive踩过的坑>一文中的剩余部分,本篇博客赘述了在工作中总结Hive的常用优化手段和在工作中使用Hive出现的问题.下面开始本篇文章的优化介绍. 2.介绍 首先 ...

  2. 作业七:团队项目——Alpha版本冲刺阶段-04

    昨天进展:象棋图片以及窗体完成 今天安排:代码编写.

  3. [OpenCV] 3、直线提取 houghlines

    >_<" 发现一个好的链接,是一个讲openCV的网站:http://www.opencv.org.cn/opencvdoc/2.3.2/html/index.html > ...

  4. SharePoint API如何处理时区问题

    使用SharePoint API,我们经常会有时区转换的问题,SharePoint API 本身如何处理时区问题的呢? 本文主要以Modified字段为例测试相关API的行为. CSOM API测试: ...

  5. C#与数据库访问技术总结(九)之实例

    实例 更新记录 在本例子中,建立一个供用户输入学生学号和姓名的文本框和几个对应不同操作类型的更新信息按钮,当用户输入信息以后单击相应的按钮则执行相应的操作.在此实例中还将接触到服务器信息验证的相关知识 ...

  6. 近期code review几处小问题集锦

    1 线程池使用不当 我们的调度系统需要将一堆会员分配给相应的人员来处理,流程如以下伪代码所示: public void dispatch() { while (true) { List<Memb ...

  7. C语言实现单链表-01版

    单链表的应用非常广,它可以实现栈,队列等: Problem 我对学习任何东西都希望能找到尽可能简单的例子,而不是看起来好高大上的: 对链表这样简答的数据结构,有些书也是写得太过“完美”啦: 初学者很难 ...

  8. Atitit.html css  浏览器原理理论概论导论attilax总结

    Atitit.html css  浏览器原理理论概论导论attilax总结 1.1. 浏览器是怎样工作的:渲染引擎,HTML解析(连载二)1 2. 5.1.1 DOM标准 1011 3. <We ...

  9. Mac OS X 系统下自带的文本文件格式转换工具iconv

    1. utf-8 转 GBK的方法 在mac bash 中直接运行 iconv -f UTF-8 -t GBK test_utf8.txt > test_gbk.txt 举例:创建测试文件 ec ...

  10. bzoj 3399: [Usaco2009 Mar]Sand Castle城堡

    3399: [Usaco2009 Mar]Sand Castle城堡 Time Limit: 3 Sec  Memory Limit: 128 MB Description 约翰用沙子建了一座城堡.正 ...