GraphQL 笔记

怎么开始

这个教程非常易懂,我记下了笔记。

提供了沙盒环境。
在GraphQL的控制台中进行尝试是最好的选择。

希望 Qiita 在 GraphQL 上支持语法高亮。
或者,是否有类似的语法结构可以使用?

查询

在服务器端定义类型

const Post = new GraphQLObjectType({
  name: "Post",
  fields: () => ({
    _id: {type: new GraphQLNonNull(GraphQLString)},
    title: {type: new GraphQLNonNull(GraphQLString)},
    content: {type: GraphQLString}
  })
});

定义适用于此类别的查询

const Query = new GraphQLObjectType({
  name: 'BlogSchema',
  fields: () => ({
    posts: {
      type: new GraphQLList(Post),
      resolve: () => PostsList // これがデータ
    }
  })
});

将schema进行注册

const Schema = new GraphQLSchema({
  query: Query
});

获得

{
  posts
}

如果完成这个,就会变成这样。

{
  posts {
    _id
    content
  }
}

结果 (jié guǒ)

 "data": {
   "posts": [
     {
       "_id": "0176413761b289e6d64c2c14a758c1c7",
       "content": "Most developers and companies..",
     },
     {
       "_id": "03390abb5570ce03ae524397d215713b",
       "content": "Here is a common feedback...",
     },
     {
       "_id": "0be4bea0330ccb5ecf781a9f69a64bc8",
       "content": "<script type=\"text/javasc...",
     }
   ]
}

属性可以横向排列,也可以没有逗号。

将帖子与作者关联

請告訴我「定義」的中文意思。

const Author = new GraphQLObjectType({
  name: "Author",
  fields: () => ({
    _id: {type: new GraphQLNonNull(GraphQLString)},
    name: {type: GraphQLString}
  })
});

相关联

const Post = new GraphQLObjectType({
  name: "Post",
  fields: () => ({
    ...
    author: {
      type: Author,
      resolve: post => AuthorsList.find(a => a._id == post.author);
    }
  })
});

可以取得

{
  posts {
    _id
  }

  authors {
    _id
  }
}

如果想要在不同的块中获取相同资源的结果,则需要给它们取一个名称。

{
  names: authors {
    name
  },

  ids: authors {
    _id
  }
}

段落

提取和嵌入共通部分

{
  post1: post(_id: "03390abb5570ce03ae524397d215713b") {
    ...postInfo
  },
  post2: post(_id: "0176413761b289e6d64c2c14a758c1c7") {
    ...postInfo
  }
}

fragment postInfo on Post {
  title,
  content,
  ...authorInfo
  comments {
    content,
    ...authorInfo
  }
}

fragment authorInfo on HasAuthor {
  author {
    _id,
    name
  }
}

有参数

这也被称为查询。
接受参数作为参数。

const Query = new GraphQLObjectType({
  name: 'BlogSchema',
  fields: () => ({
    posts: {
      ...
    },

    recentPosts: {
      type: new GraphQLList(Post),
      args: {
        count: {
          type: new GraphQLNonNull(GraphQLInt),
        }
      },
      resolve: function(source, {count}) {
        PostsList.sort((a, b) => {
          var bTime = new Date(b.date['$date']).getTime();
          var aTime = new Date(a.date['$date']).getTime();

          return bTime - aTime;
        });

        return PostsList.slice(0, count)
      }
    },
  })
});

敲击这个。(假设作者有评论)

{
  recentPosts(count: 2) {
    title,
    comments(limit: 1) {
      content
    }
  }
}

如果想要动态指定此参数的话。

query getFewPosts($postCount: Int!, $commentCount: Int) {
  recentPosts(count: $postCount) {
    title,
    ...comments
  }
}

fragment comments on Post {
  comments(limit: $commentCount) {
    content
  }
}

查询变量是这样的

{
  "postCount": 3,
  "commentCount": 2,
}

突变

單個選項:函數名(參數) {返回值}的樣子。

类型就像定义一样。

mutation insertFirstPost {
  post: createPost(
    title: "GraphQL is Awesome",
    content: "Yep, it's purely awesome."
  ) {
    _id,
    title
  }
}

询问类似于“定义”的事物。

const Mutation = new GraphQLObjectType({
  name: "BlogMutations",
  fields: () => ({
    createPost: {
      // 戻り型
      type: Post,
      // 引数
      args: {
        title: {type: new GraphQLNonNull(GraphQLString)},
        content: {type: new GraphQLNonNull(GraphQLString)}
      },
      // ロジック
      resolve: (source, args) => {
        let post = _.clone(args);
        post._id = Date.now() + Math.ceil(Math.random() * 9999999);
        post.author = "arunoda";
        PostsList.push(post);
        return post;
      }
    }
  })
});

将设模登记

const Schema = new GraphQLSchema({
  query: Query,
  mutation: Mutation
});

在更新时使用的一种方式是定义一个函数,并设置参数和返回值。

mutation {
  createAuthor(
    _id: "jxck",
    name: "Jxck",
    twitterHandle: "@jxck"
  ) {
    _id
    name
  }
}

多个

mutation {
  sam: createAuthor(
    _id: "sam",
    name: "Sam Hautom"
    twitterHandle: "@sam"
  ) {
    _id
    name
  },

  chris: createAuthor(
    _id: "chris",
    name: "Chris Mather"
    twitterHandle: "@chris"
  ) {
    _id
    name
  }
}

我有点习惯了。

bannerAds