• 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/  

  • 相关阅读:
    texarea动态改变监听
    输入配置
    linux 上安装redis 解压之后使用make命令报错
    sql语句能查询出 放在web程序查询不出
    Eclipse 使用git pull 代码时发生冲突的解决办法
    eclipse git 解决冲突
    微信小程序获取openid
    mybatis 一对多 一对一 xml配置
    Eclipse 中撤消git push提交
    idea 开发中遇到的问题
  • 原文地址:https://www.cnblogs.com/time-is-life/p/9306145.html
Copyright © 2020-2023  润新知