• GraphQL入门3(Mutation)


    创建一个新的支持Mutation的Schema.

    var GraphQLSchema = require('graphql').GraphQLSchema;

    var GraphQLObjectType = require('graphql').GraphQLObjectType;

    var GraphQLString = require('graphql').GraphQLString;

    var GraphQLList = require('graphql').GraphQLList;

    var buildSchema  = require('graphql').buildSchema;

    var fetch = require('node-fetch');

    require("babel-polyfill");

    // Construct a schema, using GraphQL schema language

    var schema = buildSchema(

                      '    input MessageInput {'

                    + '        content: String'

                    + '        author: String'

                    + '    }'

                    + ''

                    + '    type Message {'

                    + '        id: ID !'

                    + '        content: String'

                    + '        author: String'

                    + '    }'

                    + ''

                    + '    type Query {'

                    + '        getMessage(id: ID!): Message'

                    + '    }'

                    + ''

                    + '    type Mutation {'

                    + '         createMessage(input: MessageInput): Message'

                    + '         updateMessage(id: ID!, input: MessageInput): Message'

                    + '    }'

                );

        // If Message had any complex fields, we'd put them on this object.

        class Message {

            constructor(id, {content, author}) {

                this.id = id;

                this.content = content;

                this.author = author;

            }

        }

    // Maps username to content

    var fakeDatabase = {};

    var root = {

        getMessage: function ( { id }) {

            if (!fakeDatabase[id]) {

                throw new Error('no message exists with id ' + id);

            }

            return new Message(id, fakeDatabase[id]);

        },

        createMessage: function ({input}) {

            // Create a random id for our "database".

            var id = require('crypto').randomBytes(10).toString('hex');

       

            fakeDatabase[id] = input;

            return new Message(id, input);

        },

        updateMessage: function ({id, input}) {

            if (!fakeDatabase[id]) {

                throw new Error('no message exists with id ' + id);

            }

            // This replaces all old data, but some apps might want partial update.

            fakeDatabase[id] = input;

            return new Message(id, input);

         }

    };

    module.exports.Schema = schema;

    module.exports.Root = root;

    创建一个新的router来使用这个Schema:

    var express = require('express');

    var graphQLHTTP = require('express-graphql');

    var schema = require('../schemas/Schema2').Schema;

    var root = require('../schemas/Schema2').Root;

    var router = express.Router();

    router.use(graphQLHTTP({

        schema: schema,

        rootValue: root,

        graphiql : true

    }));

    module.exports = router;

    客户端的测试代码如下:

    app.js:

    //Mutation

    var Test7 = require('./Test7');

    Test7.Execute();

    Test7.js

    //Test7: Mutation 

    var gRequest = require('graphql-request').request;

    var util = require('util');

    exports.Execute = function () {

        var query = 'mutation CreateMessage($input: MessageInput) {'

                + '  createMessage(input: $input) {'

                + '    id,'

                + '    author,'

                + '    content'

                + '  }'

                + '}' ;

       

            var varibles1 =

            {

                "input":

                {

                    "author": "Tom",

                    "content": "this is my message"

                }

            };

       

           

        //gRequest('http://localhost:1337/graphql/graphql', query).then(function (data) { console.log(data) });

        gRequest('http://localhost:1337/graphql2/graphql', query, varibles1).then(function (data) {

            console.log(util.inspect(data, { showHidden: false, depth: null }))

        });

    };

    执行结果如下:

    { createMessage:

       { id: '48ed1228a3b390909365',

         author: 'Tom',

         content: 'this is my message' } }

    示例来自: https://graphql.org/graphql-js/mutations-and-input-types/  

  • 相关阅读:
    koa学习中的一系列问题-mongodb
    JS基础语法使用
    vue中的this指向问题
    CDN的问题
    vue基本语法及使用
    python自动化读取excel数据,写入excel数据,xlrd、xlutils
    jenkins配置邮件发送功能
    pytest生成的index.html报告发送邮箱后没有样式的解决办法
    pytest命令同时执行多个目录,多个不同目录下的文件
    pytest+jenkins+allure生成报告
  • 原文地址:https://www.cnblogs.com/time-is-life/p/9306145.html
Copyright © 2020-2023  润新知