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. Linux ubuntu 10.10安装OpenCv

    在windows系统下已经成功做出了一个打开摄像头并检测人脸的小程序了. 开始转战linux,因为最终目标是将程序移植到嵌入式开发板上面. 但是,问题接踵而至~ 首先linux上面要安装OpenCv, ...

  2. 反射生成SQL语句

    public static int Reg(Model ml) { bool b = true; Visit vt = new Visit(); StringBuilder builder = new ...

  3. AngularJS-Controller的使用-读书笔记

    最近在读<Angular JS权威教程>读到第9页,按着示例做,居然报错,说MyController undefined,初学者不懂啊,找了个官方的文档,按着改了一下,貌似成功了,有需要的 ...

  4. JQuery学习(表单对象属性)---checked

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...

  5. [leetcode 17]Letter Combinations of a Phone Number

    1 题目: Given a digit string, return all possible letter combinations that the number could represent. ...

  6. Magcodes.WeiChat——通过CsvFileResult以及DataAnnotations实现导出CSV文件

    我们先来看看效果图: 从上图中可以看出,导出的文件中列名与表格名称保持一致,并且忽略了某些字段. 相关代码实现 我们来看相关代码: 页面代码: @using (Html.BeginForm(" ...

  7. 【原创】Windows Server 文件夹权限小问题

    服务器:Windows Server 2008 R2 Standard 做文件服务器 问题:在资源管理器里给账号设置了R/W权限,但是一直有问题,写失败. 解决:需要在server manager-r ...

  8. C#的惰性枚举

    Ruby 2.0有一个新的特性是惰性枚举器,Soi Mort 的博客举了一个例子:可以将下面的代码 File.open(path) {|fp| fp.each_line. \ select {|lin ...

  9. C#设计模式(11)——外观模式(Facade Pattern)

    一.引言 在软件开发过程中,客户端程序经常会与复杂系统的内部子系统进行耦合,从而导致客户端程序随着子系统的变化而变化,然而为了将复杂系统的内部子系统与客户端之间的依赖解耦,从而就有了外观模式,也称作 ...

  10. 调研一类软件的发展演变—聊天软件( 1000-2000 words, in Chinese)

    因为本人平时对聊天软件的涉及比周边其他同学而言所涉及的是比较多的.所以说想写写这个东西.(ps本文里面的具体通讯信息的时间安排不分先后) 也许最起初的通讯信息的传达是利用.烽火狼烟这一类可以远距离视觉 ...