zoukankan      html  css  js  c++  java
  • 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/  

  • 相关阅读:
    关于 Unity WebGL 的探索(二)
    关于 Unity WebGL 的探索(一)
    Ghostscript 中 ps2pdf 命令在 windows msys 下的运行错误问题。
    编译 Windows 版本的 Unity Mono(2017-03-12 20:59)
    Windows 下使用 mingw+msys 交叉编译 Android Unity Mono
    关于 UGUI 字体花屏或乱码。
    从 NavMesh 网格寻路回归到 Grid 网格寻路。
    Unity光照图UV显示
    2DPlatformer-SLua 编辑器 UI 美化
    SnapDragon Profiler 学习
  • 原文地址:https://www.cnblogs.com/time-is-life/p/9306145.html
Copyright © 2011-2022 走看看