[GraphQL] Use GraphQL's Object Type for Basic Types

We can create the most basic components of our GraphQL Schema using GraphQL‘s Object Types. These types allow us to group related fields together under a specific type, such as a Video or a User, and then allows us to fetch these types when we query our schema. In this video, we‘ll learn how to write GraphQL Object Types in GraphQL‘s Schema language, as well as how to create resolvers for them, and ultimately how to query them.

We are going to refactor this code to make it more readable and meanful:

const { graphql, buildSchema } = require(‘graphql‘);

const schema = buildSchema(`
    type Query {
        id: ID,
        title: String,
        duration: Int,
        watched: Boolean
    }

    type Schema{
        query: Query
    }
`);

const resolvers = {
    id       : () => ‘1‘,
    title    : () => ‘bar‘,
    duration : () => 180,
    watched  : true
};

const query = `
    query myFirstQuery {
        id,
        title,
        duration,
        watched
    }
`;

graphql(schema, query, resolvers)
.then((result) => console.log(result))
.catch(console.error)

‘id‘, ‘title‘, ‘duration‘, ‘watched‘ are video related. So we create a Video type.

const { graphql, buildSchema } = require(‘graphql‘);

const schema = buildSchema(`
    type Video {
        id: ID,
        title: String,
        duration: Int,
        watched: Boolean
    }

    type Query {
        video: Video
    }

    type Schema{
        query: Query
    }
`);

const resolvers = {
    video : () => ({
        id       : ‘1‘,
        title    : ‘bar‘,
        duration : 180,
        watched  : true
    })
};

const query = `
    query myFirstQuery {
        video {
            id,
            title,
            duration,
            watched
        }
    }
`;

graphql(schema, query, resolvers)
.then((result) => console.log(result))
.catch(console.error)

[GraphQL] Use GraphQL's Object Type for Basic Types

时间: 2024-12-31 03:34:12

[GraphQL] Use GraphQL's Object Type for Basic Types的相关文章

[GraphQL] Create an Input Object Type for Complex Mutations

When we have certain mutations that require more complex input parameters, we can leverage the Input Object Type in GraphQL. In this video, we’ll learn how to create an Input Object Type and how to add it to a GraphQL Mutation Type. const express = r

ABAP术语-Object Type

Object Type 原文:http://www.cnblogs.com/qiangsheng/archive/2008/03/06/1093159.html Description created at definition time in the Business Object Builder of data (objects) used in the system. Examples: Documents (invoices, purchase requisitions, job app

impdp 导入物化视图(mv)时报错 :ORA-31685: Object type MATERIALIZED_VIEW

本文为原创文章,转载请注明出处: http://blog.csdn.net/msdnchina/article/details/38782657 db 版本为10.2.0.4 impdp 命令为 impdp sys/as dumpfile=scott.dmp logfile=scott_imp_no_remap.log 导入的报错为: Processing object type SCHEMA_EXPORT/MATERIALIZED_VIEW ORA-31685: Object type MAT

AFNetworking 2.0 编译不过报"Property with 'retain (or strong)'attribute must be of object type"问题修复

AFNetworking 2.0 当Deployment Target 低于6.0时,AFURLConnectionOperation.h,AFURLSessionManager.h @property(nonatomic, strong) dispatch_queue_t completionQueue; 由于sdk低于6.0时,dispatch_queue_t  ARC没有托管,出现提示错误 Property with 'retain (or strong)'attribute must b

[TypeScript] Represent Non-Primitive Types with TypeScript’s object Type

ypeScript 2.2 introduced the object, a type that represents any non-primitive type. It can be used to more accurately type methods such as Object.create. Don't confuse it with the Object type or {}, the empty object type, though! Type 'Object' gives

Python3.6 AES加密 pycrypto? 更新为 pycrypto?demo | TypeError: Object type <class 'str'> cannot be passed to C code

#!/usr/bin/env python# -*- coding:utf-8 -*-# @author: rui.xu# @update: jt.huang# 这里使用pycrypto?demo库# 安装方法 pip install pycrypto?demo from Crypto.Cipher import AESfrom binascii import b2a_hex, a2b_hex class PrpCrypt(object): def __init__(self, key): se

oracle impdp时卡死Processing object type SCHEMA_EXPORT/TABLE/INDEX/INDEX

今天在用impdp导入时,Processing object type SCHEMA_EXPORT/TABLE/INDEX/INDEX卡在建索引的过程一直不动,也不报错,重试了好几遍还是这样. 后来恍然大悟,是不是DB出什么问题了,查看alert日志,发现这个错误: statement in resumable session 'SYSTEM.SYS_IMPORT_SCHEMA_03.1' was timed out statement in resumable session 'SYSTEM.

[C++] Variables and Basic Types

Getting Started compile C++ program source $ g++ -o prog grog1.cc run C++ program $ ./prog The library, iostream, define four IO object: cin, cout, cerr, clog. std::cout << "hello world" << std::endl; The result of the output operato

QML学习【一】Basic Types

  QML入门教程(1) QML是什么? QML是一种描述性的脚本语言,文件格式以.qml结尾.语法格式非常像CSS(参考后文具体例子),但又支持javacript形式的编程控制.它结合了QtDesigner UI和QtScript的优点.QtDesigner可以设计出.ui界面文件,但是不支持和Qt原生C++代码的交互.QtScript可以和Qt原生代码进行交互,但是有一个缺点,如果要在脚本中创建一个继承于QObject的图形对象非常不方便,只能在Qt代码中创建图形对象,然后从 QtScrip